简体   繁体   中英

Whoops, looks like something went wrong. LARAVEL ERROR

I wanted to make a basic login/logout in Laravel. So I created a new folder under resources/views called auth and then I made a new file login.blade.php inserted this into it:

   <html>
<body>
<form>
<input type="text" name="email" placeholder="email" size="40"><br><br>
<input type="password" name="password" placeholder="password" size="40"><br><br>
<input hidden name="_token" value="{{csrf_token}}">
<input type="submit" value="send"> 
</form>
</body>
</html>

After that I edited the web.php like this:

    Route::get('/', function () {
    return view('welcome'); });

Route::get('/home', function () {
    return view('welcome'); });

Route::get('/login', function () {
    return view('auth.login'); });

Route::post('/login','Auth\LoginController@login');

Route::post('/logout','Auth\LoginController@logout');

So it should work fine because everything makes sense but whenever I goto login url, I see this error message:

ErrorException in 6c95db2d362954448afd30aa9a2bf2cb0fc31937.php line 6: Use of undefined constant csrf_token - assumed 'csrf_token' (View: G:\\xampp\\htdocs\\o2architect\\root\\laravel\\resources\\views\\auth\\login.blade.php)

So can anyone tell me whats going on here ?!

Change it:

<input hidden name="_token" value="{{csrf_token}}">

to

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

and try again.

Try below, Hope this work for you!

<html>
<body>
<form>
<input type="text" name="email" placeholder="email" size="40"><br><br>
<input type="password" name="password" placeholder="password" size="40"><br><br>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" value="send"> 
</form>
</body>
</html>

The csrf_token is a helper function of Laravel so you will have to call it with parentheses.

Just Change

<input hidden name="_token" value="{{csrf_token}}">

to

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

Recommendation

You can use {!! csrf_field() !!} {!! csrf_field() !!} which will create hidden input field with the CSRF Token.

In the code above the form that you have shown is a GET Request form, you will have to change it to action="POST" and GET request forms work without CSRF Token also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM