Ad

Define Controller Method As Form Action

- 1 answer

I have a following code

{!! Form::open(array('action' => '[email protected]')); !!}

it shouts that [email protected] is not defined unless I put some code into routes.php (this is my solution)

Route::post('clientRegistration', ['uses' => '[email protected]', '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

Ad

Answer

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' => '[email protected]', 'as' => 'registration']);

in your routes.php you can do any of the following

{!! Form::open(array('action' => 'registration')); !!}
{!! Form::open(array('action' => '[email protected]')); !!}

If you only stick with

Route::post('clientRegistration', '[email protected]');

you can still use

{!! Form::open(array('action' => '[email protected]')); !!}

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

Ad
source: stackoverflow.com
Ad