Laravel: something is changing the method, what is it? - laravel

I have a simple blade form mith post method and csrf token:
<form name="login" action="{{ route('login.do') }}" method="post" autocomplete="off">
#csrf
<label>
<span class="field icon-envelope">E-mail:</span>
<input type="email" name="email" required/>
</label>
<label>
<span class="field icon-unlock-alt">Password:</span>
<input type="password" name="password_check"/>
</label>
<button class="gradient gradient-orange radius icon-sign-in" type="submit">Send</button>
</form>
and the route in routes/web.php:
Route::post('login', 'AuthController#login')->name('login.do');
when I submit the form, I get the error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods: POST.
As you can see, the route and the form are ok, but something is messing it up and changing the method. It won't even get to the controller layer.
some infos:
-Laravel 8.12
-I have no customized middleware
-The htaccess and config/cors are default
-The route doesn't end with a slash nor is duplicated
-Whenever I do an alteration I clean the caches
-The APP_URL starts with http and SESSION_SECURE_COOKIE is set to false due to it
-The APP_ENV is production
What can it be? I've read it may be something in the server that's blocking the request and changing its method, so I'm not sure if the error is laravel or server related.

public function login(Request $request){
dd($request->all());
}

Related

Why am i getting an error 404 when i run this code in laravel 9?

I was watching an old tutorial about laravel 7 whiles using laravel 9, i tried to create a HTML form like this.
<div class="card-body">
<form action="/upload" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>
then in my route(web.php) i added a code like this
route::post('/upload', function(Request $request)
{$request->image->store('images', 'public');
return 'image uploaded succesfully';
but in my webiste it tells me page the url you requested is not found on the site serve
You've defined your route using POST meaning it will only respond to POST requests.
If you try to access /upload from a web browser, that uses a GET request which you've not defined a route for. So you want to define such a route:
Route::get('/upload', function () {
return view('upload.blade.php');
});
You'll want to replace upload.blade.php with the name of your view that has your upload form in it.
Name Your Route 1st , hit this command php artisan route:clear then try..
Change the form action action="{{ route('upload') }}"
<div class="card-body">
<form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>

this form return 404 error in laravel blade.php

this is my html code
<!DOCTYPE html>
<html>
<body>
<form action="{{url('/contacts')}}" method="post">
#csrf
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="fname" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="text" id="lname" name="lname" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
and this is my web route
Route::post('/contacts', [AdminController::class, 'contacts']);
and this is my base url in .env
APP_URL=http://localhost/addressbook/public
The easiest way to work with urls to routes in Laravel is using named routes. Add the name call to your route definition.
Route::post('/contacts', [AdminController::class, 'contacts'])->name('contacts.store');
Now you can get the url to the route like this.
<form action="{{ route('contacts.store') }}" method="post">
If you need variables to the route call, it is done like this based on the url parameter naming.
route('contacts.store', ['contact' => $contact]);
Your app url looks wrong, at least i would not expect it to have public in the url. Either change your .htaccess or have a different local setup. For what it is worth, php artisan serve can serve the files locally and just needs a SQL database, for a simple development environment.
APP_URL=http://localhost/addressbook

Why I keep getting Error 419 Page expired

I am using laravel 7 and keep having this issue in my form:
<form class="form-inline " role="form" method="POST" action="{{route('auth.signup')}}">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="form-group">
<input type="email" class="form-control" id="inputEmail" name="email" value="email">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password1" name="password">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password2" name="password_confirmation">
</div>
<button type="submit" class="btn btn-primary mx-auto">Sign Up</button>
</form>
i already have changed config/session.php This line 'lifetime' => env('SESSION_LIFETIME', 1200),
Is it necessary to show route / controller they all just basic
I have also tried to remove the controller's entire code and simply dd("signed up") but i keep getting the same issues
I also went on to change my php.ini file but the problem persists.
maybe another piece of information to add is that the homepage which has the signup link to the form above also has its own form with a and its not the same token as the one in the form above
The sign up controller:
public function postSignup(Request $request)
{
$this->validate($request,[
'email'=>'required|unique:appscheduler_users|email|max:255',
'name'=>'required|max:255',
'password'=>'required|confirmed|min:6',
]);
$user=user::create([
'email'=>$request->input('email'),
'name'=>$request->input('name'),
'password'=>bcrypt($request->input('password')),
'created'=>date('Y-m-d H:i:s'),
'ip'=>$request->ip(),
]);
Auth::login($user);
if(Session::has('oldUrl')) {
$oldUrl=Session::get('oldUrl');
Session::forget('oldUrl');
return redirect()->to($oldUrl);
}
return redirect()
->route('home')
->with('info','Your Account has been created');
}
To solve this error you first need to insert one of the following commands into the form tag.
#csrf OR {{ csrf_field }}
If your problem is not resolved, do the following: and keep the csrf tag
1.Insert one of the following commands into the form tag #csrf OR {{ csrf_field }}
2.Open the .env file and change the values ​​to the "file" in the SESSION_DRIVER section.
3.Then you should reset laravel cache. type below commands in the terminal
php artisan view:clear php artisan route:clear php artisan cache:clear
php artisan config:cache
4.In the final step, unplug the project from the serve and click again on php artisan serve
I hope your problem is resolved
maybe you wanna take a look here
Post request in Laravel - Error - 419 Sorry, your session has expired

After submitting the search form, it redirects to a blank page

I have a multi-column search form inside the 'users.index' view. The form is as mentioned below:
<form method="get" action="{{route('search')}}">
<input autocomplete="off" type="text" name="name">
<input autocomplete="off" type="text" name="role">
<select name="status">
<option disabled selected>--</option>
<option value="0">inactive</option>
<option value="1">active</option>
</select>
<button type="submit">search</button>
</form>
And the route is this:
Route::get('/admin/users/search', 'Admin\Search\SearchUsersController#search')->name('search');
But whatever I write inside the search function of the controller, it returns nothing and redirects me to a blank page!
Let 's imagine the controller is something like this :
class SearchUsersController extends Controller
{
public function search(Request $request)
{
dd($request->all());
}
}
I wonder why such a thing is happening. When I change the method of the form into Post and I determine two different routes, the problem is solved but it 's a search form and logically the method should be get.
You need to use csrf_field() or <input type="hidden" name="_token" value="'.csrf_token().'"> in your form.
Eventually I found a solution. If you have resource routes in web.php file of your project, you should introduce new routes before resource ones.

The PATCH method is not supported for this route. Supported methods: GET, HEAD

<form action="{{ route('todo.edit',$todoedit->id,'edit') }}" method="POST" class="container">
#csrf
#method('PATCH')
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" name="title" value="{{$todoedit->title}}">
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" name="description" value="{{$todoedit->description}}">
</div>
<button type="submit" class="btn btn-primary form-control">Update</button>
</form>
Todo Controller:
public function edit(Request $request,$id)
{
$todo=Todo::find($id);
$todo->title=$request->title;
$todo->description=$request->description;
$todo->save();
return redirect(route('todo.index'));
}
I do not know what seems to be the problem, I am doing the CRUD, everything is working but the Update part is not working, it is giving me the error
The PATCH method is not supported for this route. Supported methods: GET, HEAD.
I have tried everything, #method('UPDATE') and PUT and everything but it does not work
Because you write your update function body in edit method in your controller. Do as below:
public function update(Request $request,$id)
{
$todo=Todo::find($id);
$todo->title=$request->title;
$todo->description=$request->description;
$todo->save();
return redirect(route('todo.index'));
}
and in your edit method just return edit view and pass $todo object to that
Method type of edit is: Get
Method type of update is: Put or Patch
You can see this types with simple run php artisan route:list in your terminal.
In Web.php May Work
Route::patch('/todo/edit/{id}', [TodoController::class, 'edit']);

Resources