Why does not work POST routing in Laravel - laravel

I use the standard authentication mechanism in Laravel.
Route path is:
Route::post('auth/register', 'Auth\AuthController#postRegister');
HTML form is:
<form method="POST" action="/auth/register">
When I submit form I get 404 error.
But path for GET methos works:
Route::get('auth/register', 'Auth\AuthController#getRegister');

Use Laravel's helper function url() to generate an absolute URL. In your case the code would be:
<form method="POST" action="{{ url('auth/login') }}">
You could also check out the laravelcollective forms package. These classes were removed from the core after L4. This way you could build HTML forms using PHP only:
echo Form::open(['url' => 'auth/login', 'method' => 'post'])

you can use {{ URL::route('register') }} function in the action method. In This case the method will be:
action="{{ URL::route('register') }}"
and your route file will be:
`
Route::get('auth/register',array('as' =>'register' ,'uses' => 'Auth\AuthController#getRegister'));
Route::post('auth/register', array('as' =>'register' ,'uses' =>'Auth\AuthController#postRegister'));
`

Related

getting errors The POST method

I have a problem with my edit page. When I submit I get this error:
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The GET method is not supported for this route. Supported methods:
POST
.
I have no clue where it comes from as I am pretty new to Laravel.
web.php
Route::post('/admin/add_reseller','ResellerController#addReseller');
Controller.php
public function addReseller(){
return view ('admin.resellers.add_reseller');
}
add_reseller.blade.php
<form class="form-horizontal" method="post" action="" name="addreseller" id="addreseller">
{{ csrf_field() }}
Tip:
First of all, I would use named routes, that means you add ->name('someName') to your routes. This makes the use of routes way easier and if you decide that your url doens't fit you don't need to change it everywhere you used the route.
https://laravel.com/docs/7.x/routing#named-routes
e.g. Route::post('/admin/add_reseller',
'ResellerController#addReseller')->name('admin.reseller');
Problem:
What I see is, that you lack the value for the action attribute in your <form>. The action is needed, so that the right route is chosen, if the form gets submitted.
Solution:
I guess you just have to add action="{{route('admin.reseller')}}" for the attribute, so that the request goes the right route.
<form class="form-horizontal" method="post" action="" name="addreseller" id="addreseller">
Your action in the form is empty, you need to add the corresponding route there.
I'd suggest you to use named routes.
https://laravel.com/docs/7.x/routing#named-routes
In web.php
Route::post('/admin/add_reseller','ResellerController#addReseller')->name('admin.add.reseller');
and then in your blade file you could refer to the route using the route() function by passing the name of the route as an argument
<form class="form-horizontal" method="post" action="{{route('admin.add.reseller')}}" name="addreseller" id="addreseller">

update method issue(The POST method is not supported) in laravel

I want to create update method and this is the code:
Route::get("/allProducts/edit/{id}","AllproductController#edit")->name('Allproduct.edit');
Route::post("/allProducts/update/{id}","AllproductController#update")->name('Allproduct.update');
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
action="{{ route('allProducts.update' , [ 'id'=>$allproduct->id ]) }}">
{{ csrf_field()}}
public function update(Request $request, $id)
{
$data = Allproduct::find($id);
$data->name = $request->name;
$data->save();
return redirect(route('allProducts.index'));
}
when I click on submit button it shows me :
"The POST method is not supported for this route. Supported methods: GET, HEAD, PUT, PATCH, DELETE" error!
what is the problem?
Your route names do not match.
in routes:
name('Allproduct.update');
in the form:
allProducts.update
Also, you can always check the name of the routes thanks to the console command:
php artisan route:list
if you want use method PUT:
you can change method in routes:
Route::post to Route::put
and add next in form:
<input type="hidden" name="_method" value="PUT">
OR
#method('PUT')
this is if your laravel version is 6 and if your version another, check correct way to use PUT method in forms at laravel.com/docs/6.x/routing with your version.
As said here
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
So your form should look like this:
<form class="form-horizontal tasi-form" method="post" enctype="multipart/form-data"
action="{{ route('Allproduct.update' , [ 'id'=>$allproduct->id ]) }}">
#csrf
#method('PUT')
You had a typo in your route name and it was missing the method field.
Change your route to this:
Route::put("/allProducts/update/{id}","AllproductController#update")->name('Allproduct.update');
This will work, but
i strongly recommend you read this laravel naming conventions, and then change the name of your controller and model to AppProductController, AppProduct.

MethodNotAllowedHttpException in RouteCollection

I have the MethodNotAllowedHttpException error when I try to update a data
I try to change the Form::model route to PUT and PATCH
Here's my form::model:
{!! Form::model($mission, ['route' => ['missions.update', $mission->id_missions], 'method' => 'PUT', 'class' => 'form-horizontal panel']) !!}
And here's my route:
Route::resource('missions', 'MissionsController');
I got the error mentionned above
Can somebody help me please ?
Maybe you forgot to spoof the PUT method in your form, you can do that by using blade's #method('PUT').
This is how you can implement it :
<form action="/foo/bar" method="POST">
#method('PUT')
</form>
So try also changing the form's method to POST when you use the Form::model helper because HTML forms can only be sent by GET or POST methods hence why one has to spoof the other CRUD methods.
You can read more about that here.

Laravel named route with {parametr}

Route::match(['patch','put'],'/edit/{id}', 'TestController#update')->name('update');
using route() helper in form action I expected to see
https://example.com/edit/1
And what I get using {{ route('update', $article->id) }} is https://example.com/edit?1
Any ideas how to resolve this?
Try passing the id in as an array:
route('update', ['id' => $article->id])
and make sure the form's method attribute is post as well as setting the correct _method value within the form:
<form action="{{ route('upate', ['id' => $article->id]) }}" method="post">
{{ method_field('patch') }}
</form>
I tried your example and it seems to work as expected. Going by the ? in the URL, my guess would be that it is a GET instead of POST in the form? Could you confirm that?

can I use route() to make a DELETE request in laravel

I'm using laravel and trying to delete something. Is it possible to specify the DELETE method on laravel's route()??
e.g
route('dashboard-delete-user', ['id' => $use->id, 'method'=> 'delete'])
or something like that??
EDIT:
What I meant was could I specify that in a link or a button in my blade template. Similar to this:
href="{{ route('dashboard-delete-user') }}
Yes, you can do this:
Route::delete($uri, $callback);
https://laravel.com/docs/master/routing#basic-routing
Update
If for some reason you want to use route only (without a controller), you can use closure, something like:
Route::get('delete-user/{id}', function ($id) {
App\User::destroy($id);
return 'User '.$id.' deleted';
});
No or at least I haven't figure out how to.
The only way for this to work out of the box would be to build a form to handle it. At the very minimum, you would need...
<form action="{{ route('dashboard-delete-user') }}" method="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" value="submit">Submit</button>
</form>
Or you can just create the get route which you are trying to link to and have it handle the logic. It doesn't need to be a route which only respondes to delete requests to delete a resource.
Yes you can, using a URL helper. https://laravel.com/docs/5.2/helpers#urls
There are several options to choose from.

Resources