Laravel route not working for get request with parameters - php

This is my url :
http://localhost:8000/password-reset?token=$2y$10$N8AX4N9XMhP4NKPCK6NtjOvwoOzeWmaQnOJkxrKg4Ul7shNhp0zdu
This is how my route looks like:
Route::get('password-reset?token={token}', 'Auth\PasswordResetController#index');
However this is not getting captured in the controller.

Change the route first part to password-reset (remove ?token={token}) and then in controller use request('token') to get the token value.

Related

laravel 6 route returing 404 not found

While Passing variable in URL and route returning only 404, not found#.
Did some google and found some result and tried some methods on youtube its also not working. Totally Beginner to laravel
room.blade.php
//In roomdetail->id its passing the value of room
Detail
web.php
//The route which I am using
Route::get('/detail/{$id}', 'DetailController#show')->name('detail.show');
DetailController.php
public function show($id)
{
dd($id);
}
}
In DD I want to get the Id so I can fetch data from the database it's showing only 404
Your route is wrong you are using $ in a param which is wrong instead of {$id} in route it should be {id}
change this
Route::get('/detail/{$id}', 'DetailController#show')->name('detail.show');
To
Route::get('/detail/{id}', 'DetailController#show')->name('detail.show');
and a quick tip as you are using named route then it would be better if you use route method to pass variable to route in view
So in your view change this:
Detail
To
Detail
Thanks
Remove '/' from URL or pass route
Detail
OR
Detail
or change your route as {id} instead of {$id}
it should be Route::get('/detail/{id}', 'DetailController#show')->name('detail.show');

Laravel how to generate urls using GET route names?

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 )

Get route pattern by route name in laravel 5.3

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"

laravel route post route with one forward slash assume it to be a get route

I have created a room resource in Laravel 5.3
if I run http://testlara.dev/api/room with POST Method it runs the store method of the RoomController (as intended)
but when I accidently put one forward slash
like http://testlara.dev/api/room/ although the method is POST but it runs the index method of the RoomController ( GET Method is assumed)
can anyone explain why is that so ?
I did the testing using POSTMAN
By requesting http://testlara.dev/api/room/ you are assuming that there is an id after the slash like this : api/room/{id} and it is an empty value
Route List

Laravel multiple route aliases

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.

Categories