Laravel method not allowed - php

I am getting a MethodNotAllowedException when trying to submit my form. Here are my routes
Route::group(['middleware' => 'auth', 'prefix' => 'admin'], function () {
Route::resource('user', 'UserController');
Route::get('user/destroyMe/{destroyMe}', ['as' => 'user.destroyMe', 'uses' => 'UserController#destroyMe']);
Route::get('user/changeState/{id}', ['as' => 'user.changeState', 'uses' => 'UserController#changeState']);
});
And here is the form part with storing the new user:
<div class="position-center">
<form role="form" id="tryitForm" class="form-horizontal" enctype="multipart/form-data"
method="POST" action="{{route('user.store')}}">
{!! Form::token() !!}
I've checked within route:list and I clearly have the user.store named route, and method on the route is POST. I can't figure out why am I getting the exception?
EDIT
I do have an AdminLTE for Laravel installed Link, but I've overriden its routes. Needles to say that every other route works.
EDIT 2
I tried making a manual route:
Route::post('admin/user', 'UserController#store');
and posting it to the url('admin/user') but still the same result?
EDIT 3
Clearing the cache didn't help also.
EDIT 4
After further inspection, when hitting a random route which doesn't exist, I get an error header that Sorry, the page you are looking for could not be found., but if I do any of the POST routes, I get Whoops, looks like something went wrong. (both errors trigger the same exception though).
Laravel log is empty

I have found an error causing all the trouble. Inside my form there was a line
<input name="_method" value="PUT" type="hidden">
It sneaked there by copy/pasting my edit code

Try it like this:
{!! Form::open( [ 'route' => 'user.store', 'method' => 'POST', 'files' => 'true' ] ) !!}
And do close it.
//
{{ Form::close() }}
Edit:
How about doing it this way:
In routes.php
Route::any('admin/user', 'UserController#form');
And in the controller:
public function form(){
print_r(Input::get()); die; #Hope that you are using Input.
}

Related

I am receiving the Laravel error "MethodNotAllowedHttpException in RouteCollection.php line x" while trying to POST a query form

It's just a query form. I searched for this problem and tried the many solutions I found, but none of them solved this problem.
Laravel version: 5.4.23. Using Blade template engine.
My route:
Route::post('products/search', 'ProductsController#search');
My form:
{{Form::open(['url' => ['products/search']])}}
{{Form::text('search', $search)}}
{{Form::submit('Search')}}
{{Form::close()}}
My controller:
class ProductsController extends Controller
{
...
public function search(Request $request) {
dd('This point is never reached.');
return view(...
}
}
Notes:
I tried to change the route to Route::get, but not worked. Tried to change my form method to GET, as {{Form::open(['method' => 'GET', 'url' => ['products/search']])}}, with no success.
I don't know the framework but why is the url value in the form an array?
I would have thought you'd want something like
Form::open(['url' => 'products/search'])
According to the docs, the default form method is POST so you should keep your route as Route::post. If you want to change the method to GET (which I would recommend for a search action), simply use Route::get and
Form::open(['url' => 'products/search', 'method' => 'get'])
Seems you can also use named route names or even controller methods via the action property, ie
Form::open(['action' => 'ProductsController#search'])
I think you need to change in your blade file like:
{{Form::open(['url' => ['products/search']])}}
TO
{{Form::open(['url' => 'products/search'])}}
OR you can add route name in your route like :
Route::post('products/search', 'ProductsController#search')->name('products.search');
{{ Form::open(['route' => 'products.search', 'method' => 'post']) }}
Hope this help you
So the issue might be:
Solution1. You have another route defined on top of that, something like: Route::get('products/{wildcard}', 'Controller'} in that case make sure you put all the other routes under the one you're trying to get to work.
Solution2
Go ahead and delete the controller. Then run composer dump-autoload
Then run the command on command line php artisan make:controller UserController then paste profile method that you have.
Solution3 You might have cached the routes so do: php artisan route:clear
Solution 4. change url to
{{ Form::open(array('url' => 'foo/bar')) }}
//
{{ Form::close() }}

Route [user.update] not defined

i tried these two methods..
first one : MethodNotAllowedHttpException
Route::post('/settings/{id}/update/', 'HomeController#update');
Route::match(['put','patch'], '/settings/{id}/update/','HomeController#update') use this also..
{!! Form::model($user, ['method' => 'patch','action' => ['HomeController#update',$user->id]]) !!}
another one
{!! Form::model($user, ['method' => 'patch','route' => ['user.update',$user->id]]) !!}
please explain how to use route for update default auth users.
You should give a name to the route:
Route::patch('/settings/{id}/update/', 'HomeController#update')->name('user.update');
Or:
Route::patch('/settings/{id}/update/', ['as' => 'user.update', 'uses' => 'HomeController#update']);
I think you should just be specific about the method you want to use, be it put or patch and also If i remember correctly, if you have to use patch method referencing the answer from this post: Laravel form won't PATCH, only POST - nested RESTfull Controllers, MethodNotAllowedHttpException
<form method="POST" action="patchlink">
{!! method_field('patch') !!}
. . .
</form>
The method field is required because as I understood, Laravel uses this mechanism to handle patch request.
PS: What I just tried to highlight if I understood correctly is that, there should be an extra field to handle the patch method.
Hope this helps :)

Define controller method as form action

I have a following code
{!! Form::open(array('action' => 'clientController#create')); !!}
it shouts that clientController#create is not defined unless I put some code into routes.php (this is my solution)
Route::post('clientRegistration', ['uses' => 'clientController#create', 'as' => 'registration']);
Everything then works fine, but I am not sure if I have found the correct solution, do I have to create a route for every form that will be created? I don't know why Laravel could not find a controller method without registering it in routes.php file
When you are calling an action Laravel searches through it's stored route to handler associations. This is done in routes.php
If you insert
Route::post('clientRegistration', ['uses' => 'clientController#create', 'as' => 'registration']);
in your routes.php you can do any of the following
{!! Form::open(array('action' => 'registration')); !!}
{!! Form::open(array('action' => 'clientController#create')); !!}
If you only stick with
Route::post('clientRegistration', 'clientController#create');
you can still use
{!! Form::open(array('action' => 'clientController#create')); !!}
More information here: https://laravel.com/docs/5.2/routing#named-routes
No matter what, you will need to inform Laravel about your route in routes.php, because otherwise it will not know how to handle the request.
I suggest
Route::resource('client', 'ClientController');
Please read
https://laravel.com/docs/5.2/controllers#restful-naming-resource-routes

Laravel MethodNotAllowedHttpException

I am currently creating a website that, once the user arrives, they are greeted by a form with which they input their unique id and DoB. Upon entering the information and clicking submit, they are sent to the main form which has only a little information on it and the user must enter the rest. My problem arises when I try to submit the form as I keep getting the following error:
at RouteCollection->methodNotAllowed(array('POST', 'PATCH'))
Note: I do not want any variables in my routes. (ex: I want 'form/person' and not 'form/{person_id}'). Also, I have included only the relavent information regarding the errors.
gate.blade.php - (this is where the user enters their ID and date of birth):
{!! Form::open(array('action' => 'JurorsController#form', 'class' => 'form-inline')) !!}
form.blade.php - (this is the primary form the user must fill out and submit):
{!! Form::open(['url' => action('JurorsController#submit'), 'method' => 'PATCH', 'class' => 'form-inline']) !!}
routes.php:
Route::patch('jurors/form', 'JurorsController#submit');
Route::get('jurors', 'JurorsController#gate');
Route::post('jurors/form', 'JurorsController#form');
JurorController#submit
public function submit(FormSubmitRequest $request)
{
//never reaches this point nor executes submit... instead redirects to gate IF it doesn't return 'MethodNotAllowedHttpException' error.
dd($request);
}
The only time I managed to get it to not show me the 'MethodNotAllowedHttpException' exception, I instead got redirected to the gate.blade.php page. If you have any questions for me or need me to clerify on anything, leave me a comment and I will respond once I am able to.
Thanks.
It looks to me your problem is the url in our routes. You are repeating them.
Firstly I would recommend using named routes as it will give you a bit more definition between routes. I'd change your routes to
Route::put('jurors/submit',[
'as' => 'jurors.submit',
'uses' => 'JurorsController#submit'
]);
Route::get('jurors',[
'as' => 'jurors.gate',
'uses' => 'JurorsController#gate'
]);
Route::post('jurors/form', [
'as' => 'jurors.form',
'uses' => 'JurorsController#form'
]);
Also on your submit route why are you using a PATCH request. wouldn't you use a POST request with all the data in? If you do still need to use Patch then you should be using put instead in your routes.
Another way for for testing and debugging you could use any to see if it is your HTTP request which is causing the error for example
Route::any('jurors/submit',[
'as' => 'jurors.submit',
'uses' => 'JurorsController#submit'
]);
Also then you can use the name of your route in your form::open() for example
{!! Form::open(array('route' => 'jurors.form', 'class' => 'form-inline')) !!}
Hope this helps

Route not found error in Laravel 4 with form on Home page

I have made a form on home page in hello.blade.php file , with form value
{{ Form::open(array('route' => 'home/addguest',
'class' => "form-horizontal")) }}
I have defined a method named addGuest() in HomeController which adds values in database and sends email.
I am getting error on home page as
ErrorException
Route [home/addguest] not defined. (View: /var/www/laravel/app/views/hello.blade.php)
Route.php has
Route::post('Home/addGuest', array('uses' => 'HomeController#addGuest'));
Route::get('/home', function()
{
return View::make('home');
});
What value shall I define in form attribute to post this data and save that in the database?
Shall I change the form post target or shall I change the homecontroller method?
In route.php
Route::post('Home/addGuest', array('uses' => 'HomeController#addGuest'));
Should be
Route::post('Home/addGuest', array('as' => 'addguest', 'uses' => 'HomeController#addGuest'));
The "as" is the naming of the route, that you are refering to in the form. Be sure to update the Forms route to just "addguest" as well.

Categories