In laravel 5.1, I was able to get the route path by route name, for example:
Defined Route:
Route::post('users/{user_id}/delete', 'UserController#delete')->name('user:delete');
In laravel 5.1, when I tried the below method, It gave the route without any error If I didn't pass any route parameter:
route('user:delete'); // Output: http://example.com/users/%7Buser_id%7D/delete
and then in javascript, I simply replaced %7Buser_id%7D with the user id dynamically. But laravel 5.3 is throwing error when accessing route by name that has parameters and I don't want to pass one, because the parameters are set dynamically from the javascript.
Is there any way I can access route pattern by route name like:
http://example.com/users/{user_id}/delete
Or
/users/{user_id}/delete
Thanks in advance.
You can give some route method some value, that will be then replaced in javascript. For example: route('user:delete', 'USER_ID'), then in javascript you will simply replace USER_ID.
or the better way, is to use package called "Laroute"
Related
I'm using same route name for the get & post methods in route. those routes are using for the same purpose.
ex : I'm calling to load add view form using get route
Route::get('add', 'UserController#addView')->name('user.add');
then,
I'm calling to store data in that form using post route
Route::post('add', 'UserController#store')->name('user.add');
is there any issue , If I use the same route name like this?
No, you can not use the same name for 2 different routes as is stated in the documentation if you really need to name the routes you should look for different names, but if there's no need to have named routes you can have each url with its method like:
Route::get('/your-url', 'App\Http\Controllers\UserController#addView');
Route::post('/your-url', 'App\Http\Controllers\UserController#store');
If you are making a CRUD you can have:
Route::resource('user', UserController::class);
This will create all the urls needed for a CRUD:
Actually you have same name for both routes i.e., name('user.add'). Change that.
Yes you can get issues in future. For example, I had issues after functionality update - I've updated route interface (added some fields) and new fields does not appeared in result after opening URL.
Please run command php artisan optimize to see is it all ok.
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 got this 2 routes in my routes file (web)
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
Route::post('management/special-fees/add', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
They both share the same name but one is GET and the other is POST, so far so good. But now I want to make an url in my view to open the form, for that I use the method route() like this
route('management/special-fees/add',$user->id )
but when trying to go to the url I get this route
.../management/special-fees/add?5
there is a question mark instead of a "/" so the route is invalid.
I made some tests and I figured out that happens because is trying to go to the POST route instead of the GET one if I change the POST route's url in the web file like this
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
Route::post('management/special-fees/addSSSS', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
I will in fact get this url
.../management/special-fees/addSSSS?5
So why is the route() method generating a url for the POST route over the GET one? how do I make it to choose the GET route first?
In laravel the routing is prioritized by in the order it is written in your route.php file.
In this case you're writing the Route::post last, which in turn tells Laravel that that one should have the highest priority. Try switching them and the Route::get will have the higher priority.
Like so:
Route::post('management/special-fees/addSSSS', 'Management\SpecialFeeController#store')->name('management/special-fees/add');
Route::get('management/special-fees/add/{userId}', 'Management\SpecialFeeController#create')->name('management/special-fees/add');
I may be wrong, but I think you'll have to re-think route naming. One of the problems route naming helps eliminate is redundant and complex names. For example, if you looked at route:list for Route::resource('something', 'SomethingController') it will have something.index, something.store as route names for Route::get('something') and Route::post('something').
If it's the same name, it will always resolve to the first one and will probably never hit the second route; in your case will hit the POST route and never the GET route.
?5 means 5 is an argument for your get route.
try this
url('management/special-fees/add/'.$user->id)
for get route insted of
route('management/special-fees/add',$user->id )
Within Laravel, I currently have:
Route::controllers([
'bla' => 'BlaController',
]);
But for a few specific methods within BlaController, I would like to pass parameters via the route (eg: /bla/parameter) and then be able to access that parameter within the controller.
However, declaring:
Route::get('bla/{parameter}', 'BlaController#exampleMethod');
keeps giving me a 404 not found when visiting it.
Is there some kind of conflict between using both declarations within routes.php and if so, is there a way to pass parameters to the controller for specific methods?
Edit: This is Laravel 5.1, if that makes any difference!
Can u try this ?
Route::get('/bla/{parameter}' ['uses'=>'BlaController#exampleMethod']);
http://laravel.io/forum/07-26-2014-routing-passing-parameters-to-controller
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.