Home > PHP Framework > Laravel > Laravel 5.7 cannot access routing

Laravel 5.7 cannot access routing

WBOY
Release: 2023-05-20 15:59:39
Original
586 people have browsed it

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';
Copy after login

});

You need to add the {{ csrf_field() }} method to the form to generate the CSRF token:

{{ csrf_field() }}
<!-- ... -->
Copy after login

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'
Copy after login

];

For example, let's say you want to ignore the "register" route:

protected $except = [

'register'
Copy after login

];

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template