Adding form action in html in laravel

Shahid Rafiq picture Shahid Rafiq · Mar 11, 2015 · Viewed 148k times · Source

I am unable to pass url in views html form action tag.

<form method="post" action="??what to write here??" accept-charset="UTF-8">

I want to set it's action to WelcomeController@log_in function in WelcomeController file in controllers.

Here are my routes:

Route::get('/','WelcomeController@home');
Route::post('/', array('as' => 'log_in', 'uses' => 'WelcomeController@log_in'));
Route::get('home', 'HomeController@index');

After submitting it keeps the same url

http://localhost:8000/

And the main error line

Whoops, looks like something went wrong.

After that there is 1/1 TokenMismatchException in compiled.php line 2440:

Answer

lukasgeiter picture lukasgeiter · Mar 11, 2015

You can use the action() helper to generate an URL to your route:

<form method="post" action="{{ action('WelcomeController@log_in') }}" accept-charset="UTF-8">

Note that the Laravel 5 default installation already comes with views and controllers for the whole authentication process. Just go to /home on a fresh install and you should get redirected to a login page.

Also make sure to read the Authentication section in the docs


The error you're getting now (TokenMismatchException) is because Laravel has CSRF protection out of the box

To make use of it (and make the error go away) add a hidden field to your form:

<input name="_token" type="hidden" value="{{ csrf_token() }}"/>

Alternatively you can also disable CSRF protection by removing 'App\Http\Middleware\VerifyCsrfToken' from the $middleware array in app/Http/Kernel.php