我如何在 Laravel 中创建一个包罗万象的路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34831175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:03:13  来源:igfitidea点击:

How do I make a Catch-All Route in Laravel

phplaravelrouting

提问by Tarek Adam

I need a laravel routes.php entry that will catch all traffic to a specific domain.com/premium-section of the site so that I can prompt people to become members before accessing the premium content.

我需要一个 laravel routes.php 条目,它将捕获到网站特定 domain.com/premium-section 的所有流量,以便我可以在访问高级内容之前提示人们成为会员。

采纳答案by Tarek Adam

  1. In app/Http/routes.php I create a route that will catch all traffic within domain.com/premium-section/anywhere/they/try/to/go and attempt to find and execute a matching function within PremiumSectionController
  2. But there aren't any matching methods, just a catch-all.

    Route::group(['as' => 'premium-section::',
                  'prefix' => 'premium-section',
                  'middleware' => ['web']],
                  function(){
                     Route::any('', 'PremiumSectionController@premiumContentIndex');
                     Route::controller('/', 'PremiumSectionController');
    
                  });
    
  1. 在 app/Http/routes.php 中,我创建了一个路由,它将捕获 domain.com/premium-section/anywhere/they/try/to/go 中的所有流量,并尝试在 PremiumSectionController 中查找和执行匹配函数
  2. 但是没有任何匹配的方法,只是一个包罗万象的方法。

    Route::group(['as' => 'premium-section::',
                  'prefix' => 'premium-section',
                  'middleware' => ['web']],
                  function(){
                     Route::any('', 'PremiumSectionController@premiumContentIndex');
                     Route::controller('/', 'PremiumSectionController');
    
                  });
    

.

.

    namespace App\Http\Controllers;

    use ...

    class PremiumSectionController extends Controller{

        public function premiumContentIndex(){
           return 'no extra parameters';
        }

        //magically gets called by laravel
        public function missingMethod($parameters = array()){
            return $parameters;
        }

    }

回答by lagbox

You could also catch 'all' by using a regex on the parameter.

您还可以通过在参数上使用正则表达式来捕获“全部”。

Route::group(['prefix' => 'premium-section'], function () {
    // other routes
    ...
    Route::get('{any}', function ($any) {
        ...
    })->where('any', '.*');
});

Also can catch the whole group if no routes are defined with an optional param.

如果没有使用可选参数定义路由,也可以捕获整个组。

Route::get('{any?}', function ($any = null) {
    ...
})->where('any', '.*');

This last one would catch 'domain.com/premium-section' as well.

最后一个也会捕获“domain.com/premium-section”。

回答by user3260365

This does the trick:

这可以解决问题:

Route::any('/{any}', 'MyController@myMethod')->where('any', '.*');

回答by Erich García

This works for me

这对我有用

// The catch-all will match anything except the previous defined routes.
Route::any('{catchall}', 'CatchAllController@handle')->where('catchall', '.*');