You may find me stupid but i am unable to understand on what basics we give url and name in our route file
Example:
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('order.getOrders')->middleware('auth');
can anyone please tell me.
and if we take url on the basics of where our file in view folder like( order->getorder blade file)
Then what if my path is layouts.site.topbar
In view instead of pages, my file is in layouts.
EDIT:
blade file
<a href="{{ route('sync.index') }}">
#if(isset($syncs))
#foreach ($syncs as $sync)
#endforeach
{{ $sync->session_date }}
#endif
</a>
controller file
class TopbarController extends Controller
{
public function index()
{ die('o');
$syncNames = Sync::select('session_date','session_time')->where('user_id',$user_id)->get();
return view('layouts.site.topbar', array(
'syncs' =>$syncNames
));
}
public function sync_finish_session() {
die('s');
$user_id = Auth::id();
$sync_date = date('M d ',strtotime("now"));
$sync_time = date('M d, Y H:i:s',strtotime("now"));
$sync = Sync::where('user_id',$user_id)->get();
if(count( $sync) > 0) {
Sync::where('user_id',$user_id)->update(['session_date'=>$sync_date,'session_time'=>$sync_time,'user_id'=>$user_id]);
}
else {
$dates = new Sync();
$dates->session_date = $sync_date;
$dates->session_time = $sync_time;
$dates->user_id = $user_id;
$dates->save();
}
return $sync;
}
}
web file
Route::post('/sync_finish_session', 'TopbarController#sync_finish_session')->name('sync_finish_session')->middleware('auth');
Route::get('/sync/index', 'TopbarController#index')->name('sync.index')->middleware('auth');
Now whats the problem its giving nothing even i put die but its not going in controller file.
I think this is more a personal preference thing than that there are rules.
The convention I use is name(<model>.<action>)
This way i can create routes like
Route::get('/users/{id}/view', 'UserController#view')->name('users.specific.view')->middleware('auth');
You just name route as you do want. There is no strict rules how to name route. You can change name('order.getOrders') to name("anyName") and use new name in templates.
As the Laravel documentaton about rounting says:
Named routes allow the convenient generation of URLs or redirects for specific routes.
So, you can use this name to generate URLs or redirects. For example:
You could put this in your web.php file:
Route::get('/image/index', 'API\SettingsController#index')->name('image.index');
And call that route like this in your view:
Le met see that index!
Where the {{ route('image.index') }} references the name you gave to it.
You can name your route(s) anything you want. If you wanted, you could call your above route "mySuperCoolRouteName":
Route::get('/order/getOrders', 'OrderController#getOrders')-
>name('mySuperCoolRouteName')->middleware('auth');
and later in a view file you can use this name as a "shorthand" to get/print the URL of that route:
To My Cool Route
will be rendered to
To My Cool Route
Related
First, i have the component file, located at resources/views/component.
game-card.blade.php
#props(['game'])
<div class = 'games'>
<a href = 'game/{{$game->id}}'> view page </a>
<p> game: {{$game->name}} </p> <br>
<p> genre: {{$game->genre}} </p> <br>
</div>
Then this component is called at my view, located in resources/views
listing.blade.php
#extends('layout')
#section('list')
<div class = 'listContainer'>
#unless(count($games) === 0)
#foreach($games as $game)
//doesn't work
<x-game-card :game = "$game"/>
#endforeach
#else
<p> 0 Games </p>
#endunless
</div>
#endsection
The variable $game is not passed in the component <x-game-card/>, i even tried to use short atribute syntax (<x-game-card :$game/>) but it still doesn't work.
If it matters, the file listing.blade.php is yielded at the file layout.blade.php, located in the same folder.
layout.blade.php
<body>
<!-- Header -->
#yield('list')
#yield('singlegame')
#include('partials._footer')
For any prop that you want to pass to your component, you need to register it on the component class. In this case, the class is probably app/View/Components/GameCard.php
On the class, you need to do something like:
class GameCard extends Component
{
public $game;
public function __construct($game)
{
$this->game = $game;
}
Source: https://laravel.com/docs/9.x/blade#passing-data-to-components
i found the root of the problem. I just can't believe that the solution is so simple. I am going to post it here so less people make the same mistake i did
First, create the class (i personally use the command php artisan make:component to do that) and update it just like Nico did.
Second, when you put the <x-component in the HTML, MAKE SURE TO NOT LEAVE ANY SPACES IN THE VARIABLE!!!
my mistake was using <x-game-card :game = "$game"/>
instead of <x-game-card :game="$game"/>
Is it possible to set a series of global properties (such as social media usernames) that are available to all page views in OctoberCMS rather than having them associated to one CMS page or Static Page at a time?
For example, being able to use {{ twitter_username }} in any template, but it wouldn't show up as a field in any page form on the backend.
UPDATE: this can be achieved by registering a Twig function using registerMarkupTags in your plugin:
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function registerMarkupTags()
{
return [
'functions' => [
'globals' => function($var) {
switch ($var) {
case 'twitter_username':
return 'mytwitterusername';
}
return null;
},
],
];
}
}
In this case, calling {{ globals('twitter_username') }} from any template prints mytwitterusername.
Hmm yes better you need to add code to life-cycle method in layouts, so now page which are using that layout will have this info already loaded.
In layout code block you can use something like this
use RainLab\Pages\Classes\Page as StaticPage;
function onStart() {
$pageName = 'static-test'; // this will be static page name/filename/title
$staticPage = StaticPage::load($this->controller->getTheme(), $pageName);
$this['my_title'] = $staticPage->viewBag['title'];
$this['twitter_username'] = $staticPage->viewBag['twitter_username'];
}
now inside your cms page you can use this variable
<h1>{{ my_title }} </h1>
<h3>{{ twitter_username }} </h3>
let me know if it you find any issues
You could also use theme config file which gives you more flexibility rather than hardcoding the values in to the code block.
https://octobercms.com/docs/themes/development#customization
Hello I have routing method:
// Show Sport
Route::get('/{id}/{name_to_url}', [
'as' => 'front.sport',
'uses' => 'FrontController#sport'
]);
and method in Controller to this routing method:
public function sport($id, $name_to_url){
// Team
if (is($id.'/'.$name_to_url)) {
$teamSport = Team::select()->where('sport_id', '=', $id)->orderBy('team_name', 'asc')->get();
}
return view('master', compact('teamSport'));
}
and query in view file:
<?php
$getLeague = League::select()->orderBy('name', 'asc')->get();
?>
#foreach($getLeague as $league)
<p class="league-star"><span class="glyphicon glyphicon-star-empty" aria-hidden="true"></span> Liga {{ $league->name }} <img src="{{ asset('flags/'.$league->flags) }}" width="auto" height="18" class="league-icon"></p>
#endforeach
I don't really know how I can write a method which will check what exists in the GET. More precisely I have front page (home) url to this home page is "www.mypage.com/", now I want create new url (www.mypage.com/football, www.mypage.com/boks ...). And when user klick on button with link for example www.mypage.com/boks on front page they will be returned ONLY records with boks_id, not football ONLY WITH BOKS.
But when the user is on home page (www.mypage.com/) on front page is returned all records from database
You don't really need to check $_GET. You just need to do this:
In your route, you will need to put a ? behind each parameters to indicate that these parameters are optional. If you don't do so, you will need to explicitly provide a route to / for the home page. In this case, we will just use optional parameters:
Route::get('/{id?}/{name_to_url?}', [
'as' => 'front.sport',
'uses' => 'FrontController#sport'
]);
Then in your controller, simply make the parameters optional, and check if they are set or not:
public function sport($id = null, $name_to_url = null){
//We first creating the builder, default to sort by team_name
$teamSportBuilder = Team::orderBy('team_name', 'asc');
//If team id is set, we add the extra condition in
if (isset($id)) {
$teamSport = $teamSportBuilder->where('sport_id', '=', $id);
}
//Query it and return data
$teamSport = $teamSportBuilder->get();
return view('master', compact('teamSport'));
}
Not an answer because #Lionel Chan were good but you should never execute SQL requests in your view files.
You shouldn't use the PHP open tag in your view files.
This is not the purpose of a Framework like Symfony/Laravel
I have a page system in Laravel - where I pass data from controller to view.
$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;
Now I passed it as follows:
return View::make('Themes.Page', $this->data);
In the view file, I access the data as follows:
{{$breadcrumb}}
What I am trying to do now is to pass this data in nested views:
$this->layout->nest('content',$page, $this->data);
(Content is the {{content}} in the view which will be replaced with $page contents. I want to pass the $this->data just as before but now I get an error:
Variable breadcrumb not defined.
Note: Laravel Version 4.2 $this->layout is set in constructor to a
template file (Themes.Page)
Actually you don't need to pass any separate data to your partial page(breadcrumb)
controller page
$this->data['title'] = $row->title;
$this->data['breadcrumb'] = $row->bc;
return View::make('idea.show',array("data"=>$this->data));
main view page
<div>
<h1>here you can print data passed from controller {{$data['title']}}</h1>
#include('partials.breadcrumb')
</div>
your partial file
<div>
<h1>here also you can print data passed from controller {{$data['title']}}</h1>
<ul>
<li>....<li>
<li>....<li>
</ul>
</div>
for more information on this you can check following links http://laravel-recipes.com/recipes/90/including-a-blade-template-within-another-template or watch this video https://laracasts.com/series/laravel-5-fundamentals/episodes/13
You should pass data as follows
return View::make('Themes.Page')->with(array(
'data'=>$this->data));
or (since you're passing only 1 variable)
return View::make('Themes.Page')->with('data', $this->data);
and further you can pass it on to nested views as by referencing $data
$dataForNestedView = ['breadcrumb' => $row->bc];
return View::make('Themes.Page', $this->data)->nest('content', 'page.content', $dataForNestedView);
In the Themes.Page view render nested view:
<div>
{{ $content }} <!-- There will be nested view -->
</div>
And in the nested page.content view you can call:
<div>
{{ $breadcrumb }}
</div>
*div tag is only for better understanding.
Okay, after intense search, I figured out that there was a bug in the Laravel version 4.2 I was using.
Laravel 5 works.
For laravel 4.2, a better option would be to pass array of data objects using View::share('data',$objectarray) while passing the data from controller.
Thanks everyone for help
I followed a tutorial on Tutsplus about creating an ecommerce website using Laravel. The problem I'm having right now is when trying to route to a subfolder. In the tutorial, the instructor included a feature where you can view products by ID. And this is how he did it:
// StoreController.php
public function getView($id) {
return View::make('store.view')->with('store', Store::find($id));
}
This piece of code seems to be passing an id from the stores table. I think when a product is clicked, that's when the id is passed
// Routes.php
Route::controller('store', 'StoreController');
Also some of the templates:
// store\index.blade.php
<h2>Stores</h2>
<hr>
<div id="stores row">
#foreach($stores as $store)
<div class="stores col-md-3">
<a href="/store/products/view/{{ $store->id }}">
{{ HTML::image($store->image, $store->title, array('class' => 'feature', 'width'=>'240', 'height' => '127')) }}
</a>
<h3>{{ $store->title }}</h3>
<p>{{ $store->description }}</p>
</div>
#endforeach
</div><!-- end product -->
So.. How it goes is when I click on a product, it leads me to domain:8000/store/view/6 where 6 is the id.
This works fine but what I want to know is how do I route through a subfolder? Let's say I want it to be like this: store/view/products/6 considering that I have a folder called products and my view.blade.php is inside that like this: store/products/view.
In my StoreController class, I tried changing this
public function getView($id) {
return View::make('store.view')->with('store', Store::find($id));
}
to this
public function getView($id) {
return View::make('store.product.view')->with('store', Store::find($id));
}
but it does not seem to work giving me nothing but a Controller Method Not Found Error.
First, the view name View::make('store.product.view') has nothing to do with the URL.
You have to change the route:
Route::controller('store/view', 'StoreController');
And then adjust the name of your method in the controller because it should be the same as the segment of the URL after store/view
public function getProducts($id) {
return View::make('store.product.view')->with('store', Store::find($id));
}
I strongly recommend you read the Laravel docs on the topic