Home> PHP Framework> Laravel> body text

Introducing the new missing method in Laravel8 routing module

藏色散人
Release: 2021-03-19 09:01:30
forward
2026 people have browsed it

The following tutorial column will introduce you to the new missing method in the Laravel 8 routing module. I hope it will be helpful to friends in need!

Laravel version 8.26.0 and above has a new

missing()Introducing the new missing method in Laravel8 routing modulemethod in the routing module. In actual development, we often use routing Invisible binding automatically finds the corresponding data, reducing the need to write code in the Controller.

Previously, if the model did not find the corresponding data, it would automatically jump to a globally unified 404 page, which was not flexible enough. The

missing()method is here to solve this problem.

Route::get('/users/{user:slug}', [UserController::class, 'show']) ->name('user.view') ->missing(function (Request $request) { return Redirect::route('user.index'); });
Copy after login

When requesting missing.test/users/dalian, if the data whose slug is dalian cannot be found in the user table, it will jump to the home page of the user list instead of a unified 404 page. Note:

This method is only valid for invisible binding of routes. For example, if you use the

findOrFail
method in a Controller, even if the specified data is not found, It will not start the

missing
method, but will jump to a unified 404 page.

The above is the detailed content of Introducing the new missing method in Laravel8 routing module. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!