I'm starting from the blade password.blade.php which is loaded from the route
Route::get('password/email', 'Auth\PasswordController#getEmail')
->name('auth.password.email');
In the blade I have a form that I'm trying to post to the route:
Route::post('password/change', 'User\UserController#password')
->name('user.password.change');
Like so:
{!! Form::open(array('method' => 'POST', 'url' => route('user.password.change'))) !!}
<div class="form-group">
<label for="email">Email Address</label>
<input type="email" class="form-control" name="email" id="email" value="{{ old('email') }}">
</div>
<button type="submit" class="btn btn-primary btn-block">Send Email</button>
{!! Form::close() !!}
However, it seems like it's still posting to the 'password/email' route that gets the starting blade, if that makes sense.
How can I make sure that it posts to the user.password.change route?
route() must not be returning a URL
If you're not posting to the correct route, then route must be returning false or null. This would imply that you're not passing all the parameters necessary, or that there is a problem with the route in question. The question becomes, "Why?"
Routes Cache
One possibility is that the route cache needs to be updated. This can be done with the artisan command:
php artisan route:cache
Expected Passing User
The routes in the Question make it seems like this isn't the case, but if your UsersController function requires a User $user, you need to make sure your routes and form reflect that.
Assuming you're using an older version of Laravel or the Laravel collective. The issue is in your form open. You're not passing in the user for which you want to change the password, if your using Auth::user() you might not need to pass that parameter (Also, make sure the route checks that you're not changing the password on another account)
{!! Form::open(array('method' => 'POST', 'route' => ['user.password.change,'user'=>$user->id]])) !!}
OR
{!! Form::open(array('method' => 'POST', 'url' => 'user/' . $user->id . 'password/change'])) !!}
Avoid the Whole Mess
Also, unless you have a strong reason not to, you shouldn't need to make your own routes for authentication. Laravel has an auth package available you can add to any project that is running Laravel 5.4+.
Related
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() }}
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 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 :)
I'm updating a Laravel 5.1 site to Laravel 5.2.39. I had a lot of issues with the "approved" upgrade process (in particular the middleware) so I ended up creating a new Laravel 5.2 site and just moving over my views, models and controllers from the old site. It seems to be working, except when I submit any form I get a token mismatch exception:
TokenMismatchException in VerifyCsrfToken.php line 67:
My forms have the token (using {{ csrf_field() }}) in them:
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', old('name'), ['placeholder' => 'Name', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email') !!}
{!! Form::email('email', old('email'), ['placeholder' => 'Email', 'class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone') !!}
{!! Form::text('phone', old('phone'), ['placeholder' => 'Phone', 'class' => 'form-control']) !!}
</div>
{{ csrf_field() }}
<div class="form-group">
{!! Form::submit('Submit', ['class' => 'btn btn-default btn-small']) !!}
<input type="reset" class="btn btn-primary btn-small" />
</div>
and I have cleared my cookies and also tried setting the config/session.php lifetime to a higher number to ensure it's not timing out.
This is running on a homestead box.
Any ideas what to try?
EDIT: here's the route in question
Route::resource('contact', 'ContactController');
EDIT2: here's the route file. I don't believe it is required to wrap the routes in the web middleware in Laravel 5.2-- however, I've tried it both ways with the same error.
Route::group(['middleware' => ['web']], function () {
Route::get('/', 'AccountController#index');
Route::resource('account', 'AccountController');
Route::resource('contact', 'ContactController');
});
Dumping the $request->session()->token() in the controller's create action shows the current session token on the page with the form. This is the same token that is added to the form (checked with view source).
But when it gets to the Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::tokensMatch() class, I dump the session token and it's different than the form token-- meaning these will never match. Not sure what to expect here.
This is really annoying, but after everything else it appears this was a weird error caused by multiple Laravel sites on one Homestead box. I had set the session key differently and had cleared caches, but it still wasn't working.
Destroying the homestead box and rebuilding it appears to have fixed this issue. Thanks for all the comments and attempts at finding a solution.
Place your route within web Middleware. or in App\http\kernel copy all routes from web to protected $middleware array.
As long as you have your Form:open declaration, you no longer need the separate csrf_field line. Form::open should take care of it.
Are you using the laravel collective form package? The old illuminate package is no longer maintained.
As the previous user said, all routes also must be wrapped in the web middleware group, which also could be the problem.
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).