Home >PHP Framework >Laravel >Laravel 5.7 cannot access routing
With the launch of Laravel 5.7, many users have reported problems with access routing. The occurrence of this problem may cause you to waste a lot of time during the development process. In order to help you solve this problem, this article will introduce in detail the solution to the problem that Laravel 5.7 cannot access routing.
First we need to understand some new changes in Laravel 5.7, which may cause previous code to not run properly. One of the most important changes is the addition of CSRF protection middleware by default. This middleware can ensure that the form data submitted by the user comes from your application and prevent the application from CSRF attacks.
CSRF protection middleware already exists in Laravel 5.6, but it is not enabled by default. But in Laravel 5.7, the Laravel team has enabled this middleware by default. Therefore, in Laravel 5.7, you need to add a CSRF token when submitting data using a form.
Solution:
There are two ways to solve the problem of Laravel 5.7 not being able to access routing. The first method is to add the CSRF token in the web.php file. The second method is to disable the CSRF protection middleware.
Method 1: Add the CSRF token in the web.php file
The web middleware group is used by default in RouteServiceProvider.php of the Laravel 5.7 code, therefore, add it in the web.php file The simplest way to create a CSRF token is to use the {{ csrf_field() }} method.
For example, if you have a route that handles POST requests:
Route::post('/foo', function () {
return 'Hello World';
});
You need to add the {{ csrf_field() }} method to the form to generate the CSRF token:
4d7a02db55d48fb4795b6f9a4532230c
{{ csrf_field() }} <!-- ... -->
f5a47148e367a6035fd7a2faa965022e
This method ensures that your Laravel 5.7 application can handle POST requests normally.
Method 2: Disable CSRF protection middleware
Disabling CSRF protection middleware is another way to solve the problem that Laravel 5.7 cannot access routing. While this approach may slightly reduce the security of your Laravel application, it is feasible if your application is not at risk from CSRF attacks.
To disable the CSRF protection middleware, you need to add the URI to be ignored in the $except attribute of the VerifyCsrfToken class.
Open the app/Http/Middleware/VerifyCsrfToken.php file and add the route to be ignored in the $except attribute:
protected $except = [
'your-route-to-ignore'
];
For example, let's say you want to ignore the "register" route:
protected $except = [
'register'
];
This method will ensure that your Laravel 5.7 The application can handle all requests normally.
Conclusion:
In general, the problem of not being able to access routing in Laravel 5.7 is mainly caused by the CSRF protection middleware being enabled by default. Therefore, if you encounter this problem during development, you can try these two methods to solve it. If you want to disable CSRF protection middleware, you should only use it for routes that do not involve sensitive or important data. This method may slightly reduce the security of your Laravel application, so it is recommended to use method one to ensure the security of your application.
The above is the detailed content of Laravel 5.7 cannot access routing. For more information, please follow other related articles on the PHP Chinese website!