what is the difference between both routing ? can anyone explain ?
Route::get('login', 'webcontroller#login');
Route::get('login', array('as' => 'login','uses'=>'webcontroller#login'));
Well. There is the flexibility of Route object (i think it belongs to Symfony)
In the first statement, you explicitly say that you what controller's action a certain address should trigger (in your case it is 'login' which triggers login() of WebController).
In the second statement, you can add an "array" of settings for the controller's method, which, in your case you have specified a name. "login", which is the name of your Route::get() rule for the address "/login", could be used any where in the system without you explicitly specifying any controller or url which gives you the ability to change whatever you like in the future, as long as you are consistent with your names.
You set a route:
Route::get("login", array('as'=>'login', 'uses'=>'LoginController#Login');
Then you can use it like:
$url = URL::route('profile');
Whil you are still able to change the url of your route:
Route::get("user/login", ...);
Without the need to change its uses of "name" within your project.
You can read about it on Laravel's official documentation:
http://laravel.com/docs/4.2/routing#named-routes
In the number 2, you usea an alias , is easy for call de route in the code:
example:
<a href=" {{ route('user.list') }} ">
< span class="glyphicons glyphicons-link"></span>
<span class="sidebar-title">Link</span>
</a>
Related
I have two routes
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
but when i access the second routes, it always gets the first routes why?
and how to fix it?
thanks for helping me
You may constrain the format of your route parameters using the where method on a route instance. The where method accepts the name of the parameter and a regular expression defining how the parameter should be constrained:
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->where(['fasilitas_id' => '[0-9]+', 'any' => '[0-9]+']);
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->where('kf_id' => '[0-9]+');
For more info Regular Expression Constraints
Another way to pass it define name route.
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->name('fasilitas.example1');
<a href="{{ route('fasilitas.example1',['fasilitas_id'=>1,'any'=>2]) }}">
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->name('fasilitas.example2');
<a href="{{ route('fasilitas.example2',['kf_id'=>1]) }}">
When hitting /fasilitas/get_kategori you trigger the first route, with get_kategori being the {fasilitas_id}.
Change the order of your routes, so /fasilitas/get_kategori gets triggered first:
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
The second segment of the first route is a wildcard, that means it could be anything.
When the second route is called in browser the second segment (/get_kategori/) is passing through the wildcard of the first route.
Changing the route order may solve the problem. But the best practice is changing the route name. Example:
Route::get('/fasilitas/something_else/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori');
Try with name . It will be more efficient
Fasilitas !
May be you tried to write facilities.
Whatever, am writing base on ur method.
Route::get('/fasilitas/{fasilitas_id}/{any}','Fasilitas_controller#detail_fasilitas')->name('detail.fasilitas');
Route::get('/fasilitas/get_kategori/{kf_id}','Fasilitas_controller#get_kategori')->name('get.kategori');
How to call at front page for two parameter and single parater ?
Have a look below .
For detail.fasilitas
Detail Fasilitas
For Single Parameter : get.kategori
Get Kategori
Try it.
Let me know its work or not .
Have fun with code.
After reading the documentation, I still only have a vague idea of what Named Routes are in Laravel.
Could you help me understand?
Route::get('user/profile', function () {
//
})->name('profile');
Route::get('user/profile', 'UserProfileController#show')->name('profile');
It says:
Once you have assigned a name to a given route, you may use the route's name when generating URLs or redirects via the global route function
I don't understand what the second part of the sentence means, about generating URLs or redirects.
What would be a generated URL in the case of profile from the above example? How would I use it?
The best resource is right here : https://laravel.com/docs/5.8/routing#named-routes
One of the common use case is in your views. Say your post request goes to a particular route, basically without named routes you can simply go like this to store a task
action="/task"
but say for example you need to update the route to /task/store , you will need to update it everywhere you use the route.
But consider you used a named route
Route::post('/task', 'TaskController#store')->name('task.store');
With named routes you can use the route like this in your view:
action="{{route('task.store')}}"
Now if you choose to update your route, you only need to make the change in the routes file and update it to whatever you need.
Route::post('/task/now/go/here', 'TaskController#store')->name('task.store');
If you need to pass arguments to your routes, you pass it as arguments to route helper like this:
route('task.edit', 1), // in resource specific example it will output /task/1/edit
All of the view examples are given you use blade templating.
After adding a name to a route, you can use the route() helper to create urls.
This can now be used in your application.
For instance, in your blade templates this may look like:
{{ route('profile') }}
This will use the application url and the route path to create a url.
this is how it looks it:
named route sample name('store');:
Route::get('/store-record','YourController#function')->name('store');
store is the named route here. to call it use route('store')
defining another type of route. this is not named route:
Route::get('/store-record','YourController#function')
you can access this route using {{ url('/store-record') }}
hope this helps
I have the followning method in my controller:
public function showQualityResult($qualityData) {
return $qualityData;
}
When clicked on a link , i want that method to be invoked , so i have the following in my view file:
Submited Quality Check
Also, I have the following route setup:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult');
But having the below line of code:
Submited Quality Check
Does't really work , i get the following error in the frontEnd:
Now how can i solve this problem , and why am i getting this error of Route not defined , even though i have the route defined ??
route() helper uses route name to build URL, so you need to use it like this:
route('quality-result.show', session('quality-data'));
And set a name for the route:
Route::get('/showQualityResult', ['as' => 'quality-result.show', 'uses' => 'QualityCheckController#showQualityResult']);
Or:
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult')->name('quality-result.show');
The route function generates a URL for the given named route
https://laravel.com/docs/5.3/routing#named-routes
If you don't want to use route names, use url() instead of route()
The route() helper looks for a named route, not the path.
https://laravel.com/docs/5.3/routing#named-routes
Route::get('/showQualityResult', 'QualityCheckController#showQualityResult')
->name('showQualityResult');
Should fix the issue for you.
In my case I had simply made a stupid mistake.
Further down in my code I had another named route (properly named with a unique name) but an identical path to the one Laravel told me it couldn't find.
A quick update to the path fixed it.
I've been reading everywhere but couldn't find a way to redirect and include parameters in the redirection.
This method is for flash messages only so I can't use this.
return redirect('user/login')->with('message', 'Login Failed');
This method is only for routes with aliases my routes.php doesn't currently use an alias.
return redirect()->route('profile', [1]);
Question 1
Is there a way to use the path without defining the route aliases?
return redirect('schools/edit', compact($id));
When I use this approach I get this error
InvalidArgumentException with message 'The HTTP status code "0" is not valid.'
I have this under my routes:
Route::get('schools/edit/{id}', 'SchoolController#edit');
Edit
Based on the documentation the 2nd parameter is used for http status code which is why I'm getting the error above. I thought it worked like the URL facade wherein URL::to('schools/edit', [$school->id]) works fine.
Question 2
What is the best way to approach this (without using route aliases)? Should I redirect to Controller action instead? Personally I don't like this approach seems too long for me.
I also don't like using aliases because I've already used paths in my entire application and I'm concerned it might affect the existing paths if I add an alias? No?
redirect("schools/edit/$id");
or (if you prefer)
redirect("schools/edit/{$id}");
Just build the path needed.
'Naming' routes isn't going to change any URI's. It will allow you to internally reference a route via its name as opposed to having to use paths everywhere.
Did you watch the class Illuminate\Routing\Redirector?
You can use:
public function route($route, $parameters = [], $status = 302, $headers = [])
It depends on the route you created. If you create in your app\Http\Routes.php like this:
get('schools/edit/{id}', 'SchoolController#edit');
then you can create the route by:
redirect()->action('SchoolController#edit', compact('id'));
If you want to use the route() method you need to name your route:
get('schools/edit/{id}', ['as' => 'schools.edit', 'uses' => 'SchoolController#edit']);
// based on CRUD it would be:
get('schools/{id}/edit', ['as' => 'schools.edit', 'uses' => 'SchoolController#edit']);
This is pretty basic.
PS. If your schools controller is a resource (CRUD) based you can create a resource() and it will create the basic routes:
Route::resource('schools', 'SchoolController');
// or
$router->resource('schools', 'SchoolController');
PS. Don't forget to watch in artisan the routes you created
I'm trying to create a route with an array of aliases, so when I call whois or who_is in the url it goes to the same route.
Then I don't need to keep repeating the code every time, changing only the alias.
I tried the code below.
Variables in the routes:
$path = 'App\Modules\Content\Controllers\ContentController#';
$aliases['whois'] = '(quemsomos|who_is|whois)';
Routes:
Route::get('{whois}', array('as' =>'whois', 'uses' => $path.'getWhois'))->where('whois', $aliases['whois']);
this one works as well
Route::get('{whois}', $path.'getWhois')->where('whois', $aliases['whois']);
Typing in the url my_laravel.com/whois or my_laravel.com/who_is or my_laravel.com/quemsomos will send me to $path.'getWhois' (which is correct).
But when I try to call it in the html on blade...
Who we are
The reference link goes to my_laravel.com//%7Bwhois%7D
How could I call route('whois') on my blade.php and make it work like when I type it on the url ?
I would like to use the route function` in my blade, so I can keep a pattern.
During route generation using the route function, Laravel expects you to set the value of the route parameter. You are leaving the parameter whois empty so the parameter capturing {whois} will not be replaced and results in the %7B and &7D for the accolades.
So in order to generate a route you will need to define what value you'd like to use for whois; {{ route('whois', ['whois'=>'whois']) }} for instance.