I am trying to update a user, but when I hit the submit button, Laravel throws the below error:
"RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 206".
I think that the PUT method is not allowed, but I do not understand the reason. The request never reaches UserController#update.
I have configured a resource route like this:
Route::resource('backend/users', 'Backend\UsersController');
The output of php artisan route:list is:
I solved the problem like this: it must be the form's post action error;
<form method="POST" action="10.241.229.1/backend/users/{{$user->id}}"; accept-charset="UTF-8">
add the id you want update to the action.
use put method like this within form,for more https://laravel.com/docs/5.2/routing#form-method-spoofing
{{ method_field('PUT') }}
Coming a bit late on this question.
In my experience this kind of error comes for two reasons:
as Laravel docs say, 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.
if you are making the request from a HTML form, and you have the VerifyCsrfToken middleware enable, than you will need to add a hidden _token field to the form with {{ csrf_token() }} as value.
Related
$routes->get('login', 'C_auth::display_login');
$routes->add('login', 'C_auth::display_login');
What is the difference between get and add? I test them both and it seems like they do the same thing
First off i would like to thank #sauhardnc to set me on the right direction.
Yes, what #sauhardnc is correct, i tried routing a form with a post method to a route with get method and it gives an error. So if you have a form with a post method, use the post method in your route. Add would also work, but i think the method from your form and your route should be the same.
This would be your route
$routes->post('register-user','C_auth::authenticate_registration');
or
$routes->add('register-user','C_auth::authenticate_registration');
This would be your form
<form action = '<?php echo route_to('register-user') ?>' method="post" accept-charset="utf-8">
Updating existing items in DB in my application is solved by this way:
I have url like this:
http://localhost/project/public//structure/edit/about-us
In router I have set
Route::get('/structure/edit/{url}', 'StructureController#update'); //for displaying the prefilled form
Route::post('/structure/edit/{url}', 'StructureController#update'); // for saving new values
So it means, I'm building update query where url = $url . This is the main part of my view file:
{!! Form::open(['url' => URL::current()]) !!}
I don't know where to point "form action". So I¨m using the same url as current url, so router recognizes, that this is post request and I can process the update inside the same controller and select new (updated) data to my update form.
The problem is, when I update the url via form, new value will set to database. So it means, from this moment old url doesn't exists, but and my form action point to url, which doesn't exists anymore.
What can I do with that? If you know, what I mean...
To update use patch method instead of post. Write this in web.php
Route::get('/structure/edit/{id}', 'StructureController#edit');
Route::patch('/structure/{id}/update', 'StructureController#update');
You can use either action or url as a form action. Pass the structure id in the second parameter of the action
{!! Form::model($structure,['action' => ['StructionController#update',$structure->id],'method'=>'patch']) !!}
If you do what #smartrahat suggests and still got the error you posted, then can you run php artisan route:list command and show us the structure of your routes?
I got a form in laravel with Method='GET'
{{ Form::open(array('method' => 'get')) }}
I got a few input fields in there: city, skillLevel, province and category.
When i submit the form i get a awfull url like this:
http://localhost/vacatures?city=somehwhere&skillLevel=junior&province=Zeeland&category=django
but i want something like this:
http://localhost/vacatures/somewhere/junior/Zeeland/django
How do i achieve this? I've tried giving at a route attribute and a action actribute, but that didnt work out.
Maybe this topic will help you - How To Pass GET Parameters To Laravel From With GET Method ?
There are 2 nice answers, which should suit to your problem.
I'm trying to learn laravel, but I'm running into an issue with routing.
I have the following in my Routes file:
Route::get('home', function()
{
return View::make('home');
});
Which works if I type
http://localhost/laravel/public/home
However, on a another page I have a form, that when submitted should take me to that page like so:
{{ Form::open(array('url' => 'home')) }}
Now this takes me to the correct address, but throws an exception. But, if I reload the page with the same URL then the page loads correctly. So what is the issue here? Is there a problem with the way my form is set up?
Route::get is when you want to issue a get request to the page, to get the contents basically. If you want to post to a page, you need to do, in addition to your Route::get,
Route::post
Another option that people will tell you is:
Route::any
but I would advise staying away from this because the logic will probably be different for the two routes.
I have a form I need to use on multiple pages:
Controller
$emailForm = $this->get('form.factory')->createNamedBuilder('form', 'email_form')
->add('email', 'email')
->add('subject', 'text')
->add('body', 'textarea')
->getForm();
$request = $this->get('request');
if ($request->getMethod() == 'POST' && $request->request->has('email_form') ) {
$emailForm->bindRequest($request);
if ($emailForm->isValid()) {
// do stuff ...
$this->get('session')->setFlash('email_sent', "Woey, mail sent successfully!");
// Redirect on the same url to prevent double posts
return $this->redirect($this->generateUrl($this->getRequest()->attributes->get('_route')));
}
}
return $this->render('Bundle:Form:index.html.twig', array('email_form' => $emailForm->createView()));
Template
{% if app.session.getFlash('email_sent') %}
<p>{{ app.session.getFlash('email_sent') }}</p>
{% endif %}
<form action="{{ path(app.request.attributes.get('_route')) }}" method="post" {{ form_enctype(email_form) }}>
{{ form_widget(email_form) }}
<p><input type="submit" class="submit" value="Send" /></p>
</form>
It's really just standard Symfony2 form, almost like from tutorial.
I can't figure how can I efficiently use it on multiple pages (in multiple controller actions) without repeating myself (too much). So far I tried:
putting the logic into Base controller, which is parent for every controller where I want to have this form. There were 2 problems with this approach:
I couldn't figure how to redirect properly to the same page
I had to call method from parent in every action, which isn't really a problem, but I guess there has to be some more elegant way
rendering controller using embedded controllers in twig. However, I couldn't figure how to redirect properly.
So, what's the common approach to such forms?
Edit:
I'm looking for a no script solution.
I ended up using embedded controller on every page I needed, posting to different URL and saving cookie with referrer. Then I validate form, save cookie with results, redirect back to referrer and render results (errors, thank you message...). This seems to be the best option when you are dealing with scenarios where you have to think about disabled Javascript. You can easily disable creating of redundant cookies / flash sessions using simple conditions for cases when user is posting using AJAX.
The Symfony2 Forms tutorial addresses your scenario as well, see the Creating Form ClassesDocs Section in which "you'll learn how to build your form in a standalone class, which is recommended as your form becomes reusable" (Ibid.).
You should submit the data via an ajax post.
In the controller do your thing :p
Then render the form in the twig template, without extending layouts. (like it would be currently)
Then in any view you just replace the result: $('#formDiv').html(htmlReceivedBack);
I find it the easiest to just replace the whole html div again with the new rendered html; most likely you will show a success message or some form errors.
This way the user don't have to change pages just to send a message/feedback.