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 )
Related
I'm using Laravel 5.8 and I have a route like this:
Route::get("certs","CertController#index")->name('certificate.front')->middleware('auth');
Now I wanted to add another middleware to this route, so I tried this:
Route::get("certs", "CertController#index")->name('certificate.front')->middleware('prevent-back-history','auth');
Now I don't get any error and it works But I wonder is this way better or not:
Route::get("certs", "CertController#index")->name('certificate.front')->middleware(['prevent-back-history','auth']);
So which is better and correct in this case?
Note that I don't want to use Route groups and needed to specify the middleware name directly to the route.
The last code is the correct way, because if you can see in your code
Route::get("certs", "CertController#index")->name('certificate.front')->middleware(['prevent-back-history','auth']);
If you wanted to have more than specific 1 middleware, you should use an array to define the middleware, so using [''] is the correct way, in case you wanted to add more middleware into the route.
#apose7523
Both the approaches are correct and are working, so it does not matter what approach you choose
Route::get('example',controller)->middleware('first', 'second');
Route::get('example',controller)->middleware(['first', 'second']);
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.
Instead of redirecting, I'm receiving the error below. Why?
Route [moderator.products] not defined.
These are my routes in a middleware's group function:
Route::get('moderator/products', [ModeratorController::class, 'products'])
->name('moderator.products');
Route::redirect('/', route('moderator.products'));
The issue can be resolved by naming the route first without chaining it at the end.
Route::name('moderator.products')->get('moderator/products', [ModeratorController::class, 'products']);
Route::redirect('/', route('moderator.products'));
I tested them. It reidrects to the mentioned named route. All you need to do is to first call the name() then chain the get().
Not an answer, but I got around this by not using the named route, and using the url, i.e. swap route('moderator.products') for url('moderator/products').
EDIT: I did a bunch of code editing to get past my problem, so I'm not sure, but I suspect my issue was caused by naming the exact same route twice. Double check to make sure you haven't done this if you get this problem.
Hello Everyone I am posting this question because I don't find this anywhere so please try to provide solution for this problem.
Route::get('/product/{Cat_Slug}/{slug}','ProductController#SingleProductShow')>name('SingleProduct.Show');
This is my route and i want to remove 'product' from url and make my url like. www.example.com/productCat_Slug/ProductSubCat_Slug and want to access all related products but when i do this other routes effected.
Make sure your other routes (those affected by your changes) come before this route in routes/web.php, so they are "hit" first.
Route::get('/test', 'TestController#test');
Route::get('/{user}', 'TestController#user');
instead of
Route::get('/{user}', 'TestController#user');
Route::get('/test', 'TestController#test');
Try this
Route::get('/{Cat_Slug}/{Subcat_slug}','ProductController#SingleProductShow')->name('SingleProduct.Show');
After that you will be able to access the values contained in Cat_Slug and Catsub_slug in your controller and make the query you want.
You also have a syntax error close to the route name. use ->name('myRouteName') instead of >name('myRouteName')
I am trying to name my routes so I can easily generate urls instead of using the url helper function in which I have to type myself.
I am doing this:
Route::get('logout', 'PageController#logout')->name('pc');
When I do php artisan route:list I don't get this named route, instead name column is empty.
What I want is that, whatever views I target through this controller. I want to generate their urls like this.
route('pc.logout') OR route('pc.some_other_action')
So even if I change the verb to access this controller from the URL address, I don't have to change my url generator. Is this even possible in Laravel? Please also tell how to do this for resource routes.
I am using Laravel 5.3.
Thanks.
EDIT: Next day 1/11/2017
What I am expecting is that like ASP.NET MVC routing, if I tell it to generate a url with given controller and actions, it will generate the possible route to it regardless of the verb or string used to name the route.
For example
If I make a url like this
route('users.index')
Outputs /users
but if I change the string name users to u, instead of giving me error that name 'users' doesn't exist it should auto generate a url output like this:
Output: /u
This is a part of ASP.NET MVC routing system, I am expecting to see this here as well.
You should write this This will solve your problem
Route::get('logout', array('as' =>'pc.logout' ,'uses' => 'PageController#logout'));
This might solve your problem
you can define the named route as this
Route::get('/logout',[
'uses'=>'web\PageController#logout',
'as'=>'pc'
]);
Try this:
<?php
Route::get('logout',[
'uses'=>'PageController#logout',
'as'=>'pc.logout'
]);
name('pc.logout') OR name('pc.some_other_action')
You can try this one.
It is much simpler way to use:
Route::get('logout', 'PageController#logout')->name('pc.logout');
Or like this:
Route::get('logout', 'PageController#logout')->name('pc.some_other_action');
can you explain further if this is wrong.