34

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:

Prashant Pokhriyal
3,8474 gold badges33 silver badges42 bronze badges
asked Mar 11, 2015 at 10:40
0

13 Answers 13

41

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

answered Mar 11, 2015 at 10:44
Sign up to request clarification or add additional context in comments.

8 Comments

home page started appearing but after clicking the submit button it does not go to that page
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ //Route::get('/', 'WelcomeController@index'); Route::get('/','WelcomeController@home'); Route::post('/', array('as' => 'log_in', 'uses' => 'WelcomeController@log_in'));
//Route::post('/dashboad', array('as' => 'dashboard', 'uses' => 'WelcomeController@dashboard')); //Route::get('/logout', array('as' => 'logout', 'uses' => 'WelcomeController@logout')); //Route::get('/', 'WelcomeController@log_in'); Route::get('home', 'HomeController@index'); //Route::controllers(['auth' => 'Auth\AuthController','password' => 'Auth\PasswordController']);
here is the routes.php file in two parts
You can edit your question. Please do that and add the code properly formatted
|
22

Laravel 5.8

Step 1: Go to the path routes/api.php add:

Route::post('welcome/login', 'WelcomeController@login')->name('welcome.login');

Step2: Go to the path file view

<form method="POST" action="{{ route('welcome.login') }}">
</form>

Result html

<form method="POST" action="http://localhost/api/welcome/login">
<form>
ParisaN
2,1324 gold badges27 silver badges61 bronze badges
answered Jun 14, 2019 at 11:49

1 Comment

<form method="POST" action="localhost/api/welcome/login"> this one is bad practice ... because when you upload the project to the server you need to change the URL with reference to domain name
10

if you want to call controller from form action that time used following code:

<form action="{{ action('SchoolController@getSchool') }}" >

Here SchoolController is a controller name and getSchool is a method name, you must use get or post before method name which should be same as in form tag.

Prashant Pokhriyal
3,8474 gold badges33 silver badges42 bronze badges
answered Jul 5, 2016 at 5:32

Comments

9

1) In Laravel 5 , form helper is removed .You need to first install laravel collective .

Refer link: https://laravelcollective.com/docs/5.1/html

{!! Form::open(array('route' => 'log_in')) !!}

OR

{!! Form::open(array('route' => '/')) !!}

2) For laravel 4, form helper is already there

{{ Form::open(array('url' => '/')) }}
answered Jan 13, 2016 at 9:44

Comments

7

Use action="{{ action('WelcomeController@log_in') }}"

however TokenMismatchException means that you are missing a CSRF token in your form.

You can add this by using <input name="_token" type="hidden" value="{{ csrf_token() }}">

answered Jun 28, 2017 at 13:30

1 Comment

This answer is essentially the same as the accepted and most voted answer... with less detail. It would be better to upvote that answer than add in your own
7

For Laravel 2020. Ok, an example:

<form class="modal-content animate" action="{{ url('login_kun') }}" method="post">
 @csrf // !!! attention - this string is a must 
....
 </form>

And then in web.php:

Route::post("/login_kun", "LoginController@login");

And one more in new created LoginController:

 public function login(Request $request){
 dd($request->all());
}

and you are done my friend.

answered Feb 17, 2020 at 22:00

1 Comment

This URL helped me in Laravel-8 version - laravel.com/docs/8.x/routing#generating-urls-to-named-routes
6

In Laravel 8:

Step 1: In the blade file:

<form action="{{ route('authLogin') }}" method="post">
 @csrf
 ....
</form>

Step 2: And then in web.php:

use App\Http\Controllers\UsersController;
Route::post('login-user', [UsersController::class, 'login'])->name('authLogin');

Step 3: And in the UsersController:

public function login(Request $request){
 dd($request->all());
}

Happy to share. Thanks to ask this question.

** For more information, please see https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes

answered Dec 10, 2021 at 8:53

Comments

4
{{ Form::open(array('action' => "WelcomeController@log_in")) }}
...
{{ Form::close() }}
answered Mar 11, 2015 at 13:08

1 Comment

Anyone with a little bit of coding knowledge can understand.
4

You need to set a name to your Routes. Like this:

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

I just put name on Routes that need this. In my case, to call from tag form at blade template. Like this:

<form action="{{ route('home.index') }}" >

Or, You can do this:

<form action="/" >

Comments

3

Form Post Action :

<form method="post" action="{{url('login')}}" accept-charset="UTF-8">

Change your Route : In Routes -> Web.php

Route::post('login','WelcomeController@log_in');
answered Sep 19, 2018 at 12:41

1 Comment

In my Lumen installation the action() function mentioned by many here returned an error, but the url() function worked.
2

The following should work.

{{ Form::open( array('url' => action('WelcomeController@log_in'), 'files'=>true,'method'=>'post') ) }}
...
{{ Form::close() }}
MikeT
57.9k16 gold badges61 silver badges78 bronze badges
answered Jun 2, 2016 at 5:15

Comments

1

I wanted to store a post in my application, so I created a controller of posts (PostsController) with the resources included:

php artisan make:controller PostsController --resource

The controller was created with all the methods needed to do a CRUD app, then I added the following code to the web.php in the routes folder :

Route::resource('posts', 'PostsController');

I solved the form action problem by doing this:

  1. I checked my routing list by doing php artisan route:list
  2. I searched for the route name of the store method in the result table in the terminal and I found it under the name of posts.store
  3. I added this to the action attribute of my form: action="{{route('posts.store')}}" instead of action="??what to write here??"
Eborbob
1,9851 gold badge17 silver badges31 bronze badges
answered Aug 10, 2018 at 18:09

Comments

0

Your form is also missing '{{csrf_field()}}'

JON
1,0232 gold badges12 silver badges28 bronze badges
answered Sep 19, 2018 at 11:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.