#csrf is not working in Laravel 5.4 - php

I am following this tutorial on Laravel and I've noticed that I cannot use the #csrf command, but others can use it effectively. But, I can use the {{ csrf_field() }} for the csrf token. I would like to know the reason why. Thanks.
Here's my screenshot on using the #csrf command: Image here
Here's my index.blade.php
<form action="{{ route('contact.store') }}" method="post">
#csrf
<div class="col-md-6 mb-3">
<label for="username">Username:</label>
<input class="form-control" type="text" name="name" id="username" placeholder="Username | Email | Phone Number" required>
<div class="invalid-feedback">
Please provide a username
</div>
</div>
<div class="col-md-6 mb-3">
<label for="password">Password:</label>
<input class="form-control" type="password" name="name" id="password" placeholder="" required>
<div class="invalid-feedback">
Please provide a password
</div>
</div>
<div class="col-md-6 mb-3">
<input type="submit">
</div>
</form>

As far as I know, there is no #csrf in 5.4, you must use
{{ csrf_field() }}
Instead, #csrf is available in laravel 5.6

#csrf argument came to blade with 5.6
you need to use csrf_field() method instead #csrf
for more information check out the docs

Laravel 5.4 only available
refer to this link
https://laravel.com/docs/5.4/csrf
{{ csrf_field() }}
And Laravel 5.6 available
refer to this link
https://laravel.com/docs/5.6/csrf
#csrf

if you used to laravel framework version 5.6, 5.5 or less than version 5.5
you should write {{ csrf_field() }}, but if you used to laravel framework version 5.7, 5.8 or bigger than version 5.7 you should write #csrf
Laravel 5.5, 5.6 => write {{ csrf_field() }}
Laravel 5.7, or bigger than 5.7 => write #csrf

Related

Blank Page in Google Chrome

Try to load my own created page with using Laravel + Blade, but then the browser only displays the output a sec then disappears. I have no idea what is going wrong, everything in my code doing fine and without errors... Any thoughts???
I'm running this page using php -S 127.0.0.1:8000 -t public
This is web.php:
Route::get('/products/create', 'ProductsController#create');
This is ProductsController.php:
public function create(){
return view('products.create');
}
This is create.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
<h1>Add new product</h1>
<form action="/products" method="POST">
#csrf
<div class="container">
<div class="form-group row">
<label for="pName" class="col-md-4 col-form-label">Product name</label>
<input id="pName"
name="pName"
type="text"
class="form-control #error('pName') is-invalid #enderror"
value="{{ old('pName') }}"
autocomplete="pName"
autofocus>
</div>
<div class="form-group row">
<label for="pCat" class="col-md-4 col-form-label">Category</label>
<input id="pCat"
name="pCat"
type="text"
class="form-control #error('pCat') is-invalid #enderror"
value="{{ old('pCat') }}"
autocomplete="pCat"
autofocus>
</div>
</div>
<button class="btn btn-primary" type="submit">Submit</button>
</form>
</div>
#endsection
Here's the webpack.mix.js:
const mix = require('laravel-mix');
/*
|--------------------------------------------------------------------------
| Mix Asset Management
|--------------------------------------------------------------------------
|
| Mix provides a clean, fluent API for defining some Webpack build steps
| for your Laravel application. By default, we are compiling the Sass
| file for the application as well as bundling up all the JS files.
|
*/
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.mixbrowserSync('localhost:8000');
It seems like the #extends('layouts.app') is not working, because when I removed it, the page could run tho. Any solution to solve it or did missing some dependencies to install??
Look in your Laravel log file normally located at /storage/logs/laravel.log
And you may want to serve it with artisan instead:
php artisan serve
Local Development Server If you have PHP installed locally and you
would like to use PHP's built-in development server to serve your
application, you may use the serve Artisan command. This command will
start a development server at http://localhost:8000:
https://laravel.com/docs/master/logging
https://laravel.com/docs/master/installation

Route [customer.password.request] not defined

I am trying to get my development box to work so I can work on a development at home as not to alter and mess up the main site.
I get an error on the development box but not on the main site.
I have set everything up then copied the site via another tutorial. I was able to get all the requirements working and the site works except when I attempt to go to the log in page. I have searched the web for the method used for the route and I cannot find a good explanation of how it works so I can troubleshoot the problem.
div class="col-lg-6 col-md-8 col-sm-8 col-lg-offset-3 col-md-offset-1 col-sm-offset-2">
#if(Session::has('errors'))
<div class="alert alert-danger text-center">
#php ($errors = Session::get('errors'))
#php ($err = $errors->toArray())
{{$err['email'][0]}}
</div>
#endif
<form role="form" id="cust-login-frm" method="POST" action="{{ route('xxx') }}" >
{{csrf_field()}}
<input name="user_type" type="hidden" value="{{ Crypt::encrypt('XX') }}">
<div class="form-group">
<label>Email</label>
<input type="email" id="email" name="email" class="form-control" placeholder="Email" value="{{(Session::has('failed')) ? Session::get('failed') : ''}}">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" id="password" name="password" class="form-control" placeholder="Password">
</div>
<div class="clearfix"></div>
</form>
<div class="form-group col-lg-12 submitbtn">
<!-- <input type="hidden" name="save" value="contact"> -->
<button id="cust-login-btn" class="btn btn-primary" type="button">Sign In</button>
</div>
<center><div> Not Registrated? Register | Forgot Password? </div></center>
The issue I am having is the hyperlink towards the bottom "{{route('customer.password.request') }}" The route before it I can find and follow but this one with the dot notation I cannot. I cannot provide too many details as this is a live site. I suspect this broke due to my command composer update. The main site was not coded by me and has tons of issues. But it will work and its the same. This one is difficult as the main laravel.org does not have much on the subject. If a guru here can point me to a tutorial or page that will help me understand why I am getting the error "Route [customer.password.request] not defined. (View: /var/www/html/resources/views/customer/auth/xxx.blade.php)". Thanks community. :)
NOTE: I have removed some parts to hide real filenames with xxx.
Define the route name in web.php
Route::any('urlname','controllername#functionname')->name('customer.password.request');
Ok the answer to my question here is this.
I found out that the routes were placed in the wrong location.
The morons who did this put the routes in the vender folder file. This was changed when I did an update. So I decided to move them to the web.php and it worked.
So this issue can be closed.
Thanks.

All Form Post with multipart/form-data returning 404 even when route exists

I have a form in my Laravel 5.7 application to allow a user to upload a CSV file for importing data. It has been working for a few weeks. However, suddenly it started returning 404 errors for all POST requests where the form had multipart/form-data as its enctype. The strange thing is that when I change it to URLEncoded* there is no 404.
I have tried several things.
Changing the route name.
Checking php artisan route:list output to verify routes exist.
Accessing the route via GET method, and I get method not allowed exception.
Clearing the cache.
Blade Form
<form action="/import/createParts/upload/" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
#csrf
<button type="submit">Upload</button>
</form>
Routes
Route::prefix('import')->group(function () {
Route::get('/createParts', 'Import\CreatePartsController#index');
Route::post('/createParts/upload', 'Import\CreatePartsController#upload');
});
Ideally, this should pass the form over to the function, and another process happens.
Make sure you set a name for your route, it's useful.
Routes
Route::prefix('import')->group(function () {
Route::post('createparts/upload', 'Import\CreatePartsController#upload')
->name('import.createparts.upload');
});
Blade
<form method="post" action="{{ route('import.createparts.upload') }}"
enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
<button type="submit">Upload</button>
</form>
Named route concept is much more easy way handling the routes.
In web.php
Route::post('import/createParts/upload', 'Import\CreatePartsController#upload')
->name('createparts.upload');
In blade
<form method="post" action="{{ route('createparts.upload') }}"
enctype="multipart/form-data">
#csrf
<div class="form-group">
<label for="csv_upload_file">Select the File</label>
<input type="file" name="csv_upload_file">
</div>
<input type="submit" value="Upload"/>
</form>

Unable to load the "Symfony\Bridge\Twig\Form\TwigRenderer" runtime

I've been working through various hurtles toward migrating an existing Symfony3 web app from a local LAMP host up to Goggle's App Engine Standard PHP Environment. I've taken hints from the appengine-symfony-starter-project to use Memcache for sessions, pre-cache the application code, and connect to a Cloud SQL MySQL database. In have the core pieces working fine.
I added a login form at /login using a TWIG template with a form in it that goes like so:
<form class="form" action="{{ path('security_login') }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}" />
<div class="form-group">
<input class="form-control" id="email" type="text" name="_email" placeholder="Email" value="{{ last_username }}"/>
</div>
<div class="form-group">
<input class="form-control" id="password" type="password" name="_password" placeholder="Password"/>
</div>
<div class="form-group">
<input type="checkbox" id="remember_me" name="_remember_me" checked>
<label for "remember_me">Remember Me</label>
</div>
<div class="form-group">
<button class="btn btn-round btn-b">Login</button>
</div>
<div class="form-group">Forgot Password?</div>
</form>
This works fine in the local DEV and TEST environments but when I push this up to GAE, I'm getting the error below:
Unable to load the "Symfony\Bridge\Twig\Form\TwigRenderer" runtime.
I've been digging for a couple hours now and haven't found a hit as to what's wrong here. Anyone have any ideas where to look?
Thanks in advance,
In my case "twig.strict_variables: true" to "twig.strict_variables: false" fixed an issue.
We encountered the same error this morning at work, in a Silex app.
This helped us: https://github.com/silexphp/Silex/pull/1571
It worked for us to change in our composer.json the version of symfony/form from ~2.8|^3.0 to ~2.8|3.3 as it appears TwigRenderer is deprecated in 3.4.
Hope this helps.

Laravel "Token mismatch" every first attempt

First: I know that this have been asked before, but that is quite awhile ago and no one had a solving answer. In this period of time, i hope someone else has figured.
Whenever i sign out of my laravel application (whether that be manually or by timeout) and i try to sign in, i get this error:
TokenMismatchException in VerifyCsrfToken.php line 67:
This is the form:
<div class="login_wrapper">
<form method="POST" action="/auth/login">
{!! csrf_field() !!}
<input type="text" name="email" placeholder="{{ trans('nosession.login_email') }}">
<input type="text" name="password" placeholder="{{ trans('nosession.login_password') }}">
<input type="checkbox" name="remember"> Remember Me
<input type="submit" class="btn okay" value="{{ trans('nosession.login_button') }}">
</form>
</div>
I'm using the default Auth feature in Laravel, only changed the table. Sign-in works on second attempt without any problems. Any ideas?
Ps. I've tried changing {!! csrf_field() !!} to { csrf_field() }, as some people suggested it.
UPDATE:
Apparently it seems like Middleware, causing the trouble. Is it necessary to use that?

Categories