Home  >  Article  >  PHP Framework  >  Introducing the new missing method in Laravel8 routing module

Introducing the new missing method in Laravel8 routing module

藏色散人
藏色散人forward
2021-03-18 17:08:592053browse

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 module method 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');
     });

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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete