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
Related
I am beginner at laravel. I want to use laravel collective (on laravel 9 version). And actually i want to use action. I write it in my blade:
{!! Form::open(['action' => 'PostsController#store', 'method' => 'POST']) !!}
{!! Form::close() !!}
and when i open it in browser it gives me error Action PostsController#store not defined.
i do not know. help me. i try to googled it but did not find. also i tried [PostsController::class] but not worked. i tried everything what i know
Best Solution - Using named routes
From documentation of Laravel Collectives you may come to know that you can use named routes in your code. So, we better use named route.
{!! Form::open(['route' => 'route.name', 'method' => 'POST']) !!}
{!! Form::close() !!}
Alternatives
You could either string including the namespace of controller with it to work
{!! Form::open(['action' => 'App\Http\Controllers\PostController#store', 'method' => 'POST']) !!}
{!! Form::close() !!}
or you could modify your RouteServiceProvider file and change the controller namespace there. protected $namespace = 'App\\Http\\Controllers'; this approach allows you to use 'ControllerName#method' controllers created in App\Http\Controllers directory.
The other way around is just call the route name.
<form action="{{ route('your.route.name') }}">
...
</form>
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 use Form::open(['action' => 'Controller#method']); in blade to pass data from view to controller. Then I got an error:
Action App\Http\Controllers\Controller#method not defined.
That's right since my Controller is at this address:
App\Modules\Somethings\Controllers\
So, how can I do to fix it.
Thank you.
You can fix it by using routes and not controller#method.
In your routes.php define a resource pointing at your controller
Route::resource('posts', 'PostController', ['before' => 'csrf']);
Then you can use the URL::route() method to get the right route.
{!! Form::open(['url' => URL::route('posts.update', [$post->id]), 'method' => 'put', 'files' => false]) !!}
To see all your routes and how they are aliased run the following artisan command:
L5.x
php artisan route:list
L4.x
php artisan routes
you can try this way.
first create a route for this in routes.php file
Route::any('first/second/{id}', [
'uses' => 'App\Modules\Somethings\Controllers#method'
]);
Then use that routes url in form
{!! Form::open(['url' => 'first/second/5', 'method' => 'put']) !!}
May be the file name is problem. Laravel is basically provide default Controller also. Why not you change the name and try with that new name or full path.
You will get routes file from routes folder also and use laravel 5.3
You can use
Form::model
instead.
Example:
Form::model($user, ['method' => 'PATCH', 'action' => ['UserController#update', $user->id], 'files' => true])
// do you form stuff
Form::close()
// routes.php
Route::resource('/image', 'ImageController');
Route::get('/create', 'ImageController#create');
Route::post('/store', 'ImageController#store');
// create.blade.php
{!! Form::open(array('url' => '/store', 'method'=>'POST')) !!}
.......
{!! Form::close() !!}
Here if i don't write these two lines (Route::get('/create', 'ImageController#create'); Route::post('/store', 'ImageController#store');)
The resource routing of create and store does not work and show some errors.
Why this happens? Thanks in advance.
When creating resource route you don't have to create individual routes. Because all RESTfull default routes will be created for you automatically.
You just need following route
Route::resource('image', 'ImageController');
then change form as below
{!! Form::open(array('route' => array('image.store'))) !!}
.......
{!! Form::close() !!}
Read More