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
Related
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.
}
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
I am beginer with Laravel, I need help please.
In my edit user page, I have 3 forms.
But I can not do in my rooter how to make it work.
My code (in Routes) :
<?php
// update role
Route::post('edit/{id}', [
'as' => 'user_post_update_role',
'uses' => 'UserController#updateRole'
]);
// update infos
Route::post('edit/{id}', [
'as' => 'user_post_update',
'uses' => 'UserController#update'
]);
// update password.
Route::post('edit/{id}', [
'as' => 'user_post_update_password',
'uses' => 'UserController#updatePassword'
]);
?>
But the last rule, Block others.
How to do ?
Thank
The only way to do this, would be to have a single route, and have the controller that handles that route identify which form was submitted.
What you're basically doing with those routes, is defining three routes that for all intents and purposes, are identical. Therefore, it'll only use the last one that was created as each new one, overwrites its predecessor.
Alternatively, you could do as it says in the comment on your post, use edit/{id} for updating user info, edit/password/{id} for updating password, and edit/role/{id} for updating the role. You could even break these out into separate methods entirely, or separate sections.
Of the many ways you can achieve this, your chosen method is unfortunately not amongst them.
Edit your route like this:
// update role
Route::post('edit/{id}/role', [
'as' => 'user_post_update_role',
'uses' => 'UserController#updateRole'
]);
// update infos
Route::post('edit/{id}/main', [
'as' => 'user_post_update',
'uses' => 'UserController#update'
]);
// update password.
Route::post('edit/{id}/password', [
'as' => 'user_post_update_password',
'uses' => 'UserController#updatePassword'
]);
So you have unique routes and don't need complicate if else statements.
Laravel is not returning the 'as' name of a specific request.
The below two examples show the output for each case (one works fine, the other does not)
The route is defined as a "resource", shows up in the route list as "companies.update" and all of the other routes work fine (except for update). Why is the update request not returning the route name?
{!! Form::model($company, ['route' => ['companies.update', $company->id], 'method' => 'patch', 'class' => 'form-horizontal']) !!}
#include('companies.form')
{!! Form::close() !!}
("update" does not return the name)
(every other route name works)
Have a look at php artisan route:list again.
There is two entries for update. One for PUT and one for PATCH.
Maybe use the method PUT to see if it shows up (but both should work when you look at the HTML).
Working on trying to get the following form's URL to populate properly. Been stumbling over this for some time so here for some help.
As you can see from the following code, I am opening the form - binding the model - and trying to set the URL dynamically. the full URL is something like {username}/account/cards/id so i need to pass it the username (which i would like to pass the authenticated user (as they would only have access to their own page) and the ID of the card they are trying to update.
{!! Form::model($card, ['method' => 'PATCH', 'action' => 'Account\CardsController#update', array(Auth::user()->username, $card->id) ]) !!}
Now this is all happening in blade (front end) so not 100% what i am doing wrong. I have tried action, url, route... I can not get anything to work for some reason. Error I am getting on this one specifically is a array to string error. But if I can't build an array how do i pass in multiple variables? So a bit confused here.
any help would be appreciated.
Thanks
Citti
This is an update to my previous answer. You can try this one:
{!! Form::model($card, ['method' => 'PATCH', 'action' => [ 'Account\CardsController#update', Auth::user()->username, $card->id] ]) !!}
You're just passing your route parameters as an option to the Form::model tag, not the route. Try:
{!! Form::model($card, ['method' => 'PATCH', 'action' => [ 'Account\CardsController#update', [Auth::user()->username, $card->id] ] ]) !!}
If you're still having trouble I would suggest you name your route and reference the named route in the action.
If I'm not mistaken, the action should be an array if you are passing variables through to the controller. Ex:
{!! Form::model($card, array('method' => 'PATCH', 'action' => array('Account\CardsController#update', Auth::user()->username, $card->id))) !!}
Not sure if you are using Larvel Collective HTML and Forms, but they are essentially the same as the Laravel version. This page: http://laravelcollective.com/docs/5.0/html#form-model-binding explains more about your particular use case.
Hope it helps.
P.S. Try adding:
{!! Form::hidden('_method', 'PATCH') !!}
...underneath the open tag instead of within it. This has to do with L5 method spoofing, and is generally necessary for anything that is not 'POST' or 'GET', I believe. (i.e., 'PUT', 'PATCH', and 'DELETE')