I am getting 404 not found error while the route exisits - laravel

I am using Laravel to build my todo app.
In the web.php routes file i have added this route:
Route::put('/tasks/changecat', 'TaskController#changeCat');
and i am calling this route from a form in a .blade.php file like so:
<form action="tasks/changecat" id="change-cat-form" class="d-none" method="POST">
#method('PUT')
#csrf
<input type="text" name="task" id="task-input">
<input type="text" name="category" id="category-input">
</form>
But when i try to submit the form on the browser i get 404 not found
I tried to use postman and i have included the csrf token in the headings, i get a 200 ok but i get redirected to the login page.
what do you think it's causing the problem?

I fixed this when i changed the method to PATCH.
Route::patch('/tasks/changecat', 'TaskController#changeCat');
It turns out that i should use PATCH because i needed to change a part of the resource and not all of it.
Using PATCH will change the 'updated_at' column value automatically too.

Related

Laravel page expired 419 only sometimes

I have a view in my Laravel application that has a form with the #csrf inside. The problem is that I faced the 419 Page expired exception only sometimes. When I refreshed the page, the login page appeared and after login, a few times the form submission was done successfully but again, 419 occurred.
I saw the other topics. All of them told that the solution is adding the #CSRF directive to the form.
But in my special case, the #CSRF exists.
Help me please.
Update
Here is a part of my source code. It's a simple html form with the #CSRF inside it.
<form method="POST" action="/settingUrl">
#csrf
<input type="text" name="myfield" />
<input type="submit" value="save" />
</form>

Difference b/w method="POST" and #method('PUT') in Laravel

I am use a form with attribute method="POST" to update the record in laravel. But updating of record is not working. Then after putting #method('PUT') inside the form, record updating is working fine.
I just want to know about #method('PUT') and when to use it in form of Laravel application.
Not Working
<form action="{{ route('student.update',$studentData->id) }}" method="POST">
...
</form>
Working Fine
<form action="{{ route('student.update',$studentData->id) }}" method="POST">
#csrf
#method('PUT')
...
</form>
The use of #method(...) is called form method spoofing in Laravel and is a requirement because HTML forms do no support PUT, PATCH or DELETE for the method attribute.
The value of the #method is sent as part of the form request and used by Laravel to determine how to process the form submission.
What does your web.php file look like ?
If you're using Route::resource('student', SomeController::class)
POST method will hit your student.create route, while PUT will hit your student.update route.
You can check your routes and their respective methods in detail by running php artisan route:list in your console
See if you are using resource routes for CRUD operation then it will include GET, POST, PUT and DELETE methods so when you are inserting data for first time using form that time you will use POST method and when you are doing update operation that time you have to use #method('put') because resource route will support only put method, you can update using POST also but you have to make separate route for that like we make general post route Route::post(...)

Laravel - 419 issues when Submit Form

I have problems when submitting a form in Laravel application. It reported 419 error.
My code:
<form action="login" method="POST">
<input id="csft_pass" type="hidden" name="_token" value="{{ csrf_token() }}">
.....
</form>
I tried fixing it:
<form action="login" method="POST">
#csrf
.....
</form>
But still, error 419
With the above code still running normally, suddenly there was an error today
I tried many ways like php artisan cache:clear but still not solve the issue.
My Laravel version: 5.8
UPDATE: I tried a lot of solutions on stackoverflow but still can't solve it. I think that because the application's session has something wrong
After form tag use csrf_field.
{{ csrf_field() }}
And if you are using ajax you may pass csrf token on meta tag like.
<meta name="csrf-token" content="{{ csrf_token() }}">
You can use the csrf_field helper to generate the token field:
<form method="POST" action="/login">
#csrf
...
</form>
OR
<input type="hidden" name="_token" value="{{ csrf_token() }}">
It doesn't work, then Refresh the browser cache and now it might work.
Why required: Refresh the browser cache
When we update our application, a browser may still use old files. If you don’t clear your cache, Old files can access problems when you apply.
For more details open link :- Error - 419 Sorry, your session has expired
With the above code still running normally, suddenly there was an error today
This makes me suspect the error occurs only when the form was opened for more than two hours (that's the default of lifetime in config/session.php) before submitting.
If that's the case, you could set a value of more than 120 minutes as lifetime or do something in frontend to keep the session alive, such as some custom JavaScript (for single forms) as described in the selected answer to this thread or Laravel Caffeine (for whole apps)
Replace these lines
<input type="hidden" name="_token" value="{{ csrf_token() }}">

Laravel 5.8 method not supported error using register route

Lavavel 5.8 version. I'm getting route method error while using "register" Route.
<form action="{{url('register')}}" method="get" class="at-formtheme at-formcategories">
#csrf
EDIT
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/','EmployeeController#index');
Route::post('signup-detail','EmployeeController#signup_detail');
Route::post('store-employee','EmployeeController#store_employee');
Route::post('store-interest','EmployeeController#store_interest');
Laravel uses a POST request for the default auth scaffolding. Change your form HTML tag to this:
<form action="{{url('register')}}" method="POST" class="at-formtheme at-formcategories">
I prefer to use named roads rather than the url (for greater maintainability)
<form action="{{route('register')}}" method="POST" class="at-formtheme at-formcategories">
Actually i was using two post method for submit forms (one post request send data on second form then submit) and use return back() method in my controller so the exception was through again and again.

CSRF error in laravel

I have a problem in Laravel . when over and over submit Form with post method and somtimes I get error and see expire error that related to CSRF
anybody knows how can I manage this error that display not in site and instead of redirect to any page else ?
Laravel makes it easy to protect your application from cross-site request forgery (CSRF).
Just add #csrf blade directive inside the form to avoid getting csrf token error.
<form method="POST" action="/profile">
#csrf
...
</form>
The directive puts something like this
<input type="hidden" name="_token" value="CzK6peomC6Pnnqdm4NsxpdGSH6v1evDnbN12oL" >
Read more about it in the laravel documentation here https://laravel.com/docs/5.6/csrf
Regarding the expiration of the token I think you might want to handle the error this way https://gist.github.com/jrmadsen67/bd0f9ad0ef1ed6bb594e
Also, there's a package which helps the forms keep awake.
https://github.com/GeneaLabs/laravel-caffeine
I hope that helps.
Laravel 5 using Blades templates, it's easy.
Add csrf toke in your blade file
{{ csrf_token() }}
If you are using Laravel 5.6 then you need to add something like this in your code of the form
#csrf
Check in detail about: CSRF Laravel

Resources