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.
Related
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 have dozens of routes like the following list
Route::group([
'where' => ['aNumber' => '.*'],
], function () {
Route::get('air/{aNumber}/tools', 'AirController#tools');
Route::post('air/{aNumber}/handled', 'AirController#handled');
Route::post('air/{aNumber}/notHandled', 'AirController#notHandled');
Route::get('air/{aNumber}/act', 'AirController#act');
Route::post('air/{aNumber}/sendAct', 'AirController#sendAct');
Route::get('air/{aNumber}', 'AirController#show');
});
the {anumber} parameter could be like these 23-349493/4 While this kind of parameter will produce some conflicts with other routes, So for example if we are going to call air/28-23422/sendAct then instead of calling #sendAct route, it'll call #show.
because laravel thinks that /sendAct is part of the parameter. Well, I can fix this problem by adding more where not(Regex) on each of the routes and define the logic that every route should follow, But do you have a better solution for this problem?
No, you are wrong, Laravel will choose show since air/28-23422/sendAct won't hit sendAct because sendAct has as supposed method POST, and not GET.
Instead of:
Route::post('air/{aNumber}/sendAct', 'AirController#sendAct');
try writing
Route::get('air/{aNumber}/sendAct', 'AirController#sendAct');
// ^^^
Or use ^[^\/]* as where clause of the group for aNumber
In \Illuminate\Foundation\Auth\VerifiesEmails.php, line 39 fails with $request->route('hash'), but passes with $request->get('hash'). I am not sure if this is a bug, but I see nothing I have done that would somehow break this function specifically right here. I have not modified my VerificationController.php file from the core either.
The $request->route('id') above works, but the passed ID in this route is not a parameter, but directly in the path whereas the hash is affixed as ?hash=myhash.
For reference, here is my URL: http://localhost:8000/email/verify/8edd16a5-ad04-4782-b0fe-33f0f482d080?expires=myexpiryhere&hash=myhashhere&signature=mysignaturehere
Can anyone explain to me how to get this working? Obviously modifying the vendor files is not an option. I posted this in the Laravel issues here, but was directed out with the suggestion that maybe I forgot a route parameter. The URL is generated by the framework, so I've no idea what parameter I could be forgetting.
This is my bad, but thanks to Chin Leung for asking the right question.
My current routes are:
Route::get('email/verify', [VerificationController::class, 'show'])->name('verification.notice');
Route::get('email/verify/{id}', [VerificationController::class, 'verify'])->name('verification.verify');
Route::post('email/resend', [VerificationController::class, 'resend'])->name('verification.resend');
I specify them myself because I do not use the default namespacing in my routes files, I import the controllers directly to make refactoring easier. It looks like a new param was added to the verification.verify route later on, so it should now be email/verify/{id}/{hash}
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 )
I've got laravel set up on a domain on a linux host and I have a WAMP local host set up.
The only route that works is the root, when ever I try go to another route such as domain.com/account I get a "Controller method not found." error.
In my routes.php file I have:
Route::controller('','LoginController');
Route::controller('account', 'AccountController');
In my LoginController, I have just two methods. getIndex and postIndex.
After a couple of hours Googling with no results and playing around with the routes file amongst things, still nothing worked.
I tried adding the below route which didn't work either.
Route::any('hello', function(){
return 'hello!';
});
However, I then commented out my Route::controller('','LoginController'); line and the other routes started working!
I then changed it to Route::controller('login','LoginController'); and this and the other routes still worked. I then changed it to Route::any('','LoginController#getIndex'); and the root and other routes still worked. However, doing it this way, when I cliked the login button on my page nothing happened.
So my question really is, is there something wrong with doing Route::controller('','LoginController');? As everything else seems to 'work'
Laravel save an internal collection of registered routes in the $routes member of the Router class. When dispatching a request, a process of picking each element from this collection and test with current request will be executed to find out which route will be handle. This process is affected by the order of your route registering statements.
When testing each route with the current request, the picked route will be compiled and have a regex pattern. This pattern will be use to check with the current URI by the preg_match function as you can see at this line in Laravel source.
When using Route::controller a special route will be add to your routes collection. If your input is Route::controller($uri, $controller) then this special routes will have a regex pattern as ^/$uri/?P<_missing>(.*)$ and it tells Laravel that this request belong to a missing method of the $controller controller class.
In your case, you have set the value of $uri to an empty string which cause the regex of the special route to be ^/?P<_missing>(.*)$ (setting $uri with the string / cause the same effect). Well, this regex will match every URI. So, the internal route looking up process will abort when look to this special route. This is the reason while the exception has been thrown.
You should not use an empty string or the/ string when register with the Route::controller method like the way you did. Instead, use Route::resource or explicit calls (Route::get, Route::post, ...) to handle your top level routes.
I have not tried that, but maybe you could add a "/":
Route::controller('/','LoginController');
Edit
I was able to reproduce the issue and I solved by changing the order of your route lines:
Route::controller('accounts', 'AccountController');
Route::controller('','LoginController');