简体   繁体   English

我可以使用Laravel 5中间件来允许包覆盖应用程序路由吗?

[英]Can I use Laravel 5 Middleware to allow packages to override app routes?

I would like to be able to override the routes defined in app/Http/routes.php with a route in a package. 我希望能够使用包中的路由覆盖app / Http / routes.php中定义的路由。

For example, in app/Http/routes.php I might have this: 例如,在app / Http / routes.php中,我可能会这样:

Route::get('/search/{type?}',['as' => 'search','uses' => 'SearchController@search']);

I want to be able to define this in /vendor/author/package/src/Http/routes.php: 我希望能够在/vendor/author/package/src/Http/routes.php中定义它:

Route::get('/search/properties', ['as' => 'properties','uses' => 'PropertyController@search']);

The app/Http/routes.php file is loaded first so the route in their is used, not the package. 首先加载app / Http / routes.php文件,以便使用它们中的路由,而不是包。

In Laravel 4 I would do this using App::before or App::after, giving them a priority. 在Laravel 4中,我会使用App :: before或App :: after来执行此操作,为他们提供优先级。

Like so in the package routes: 像包裹路线一样:

App::before(function() {
    Route::get('/search/properties', ['as' => 'properties','uses' => 'PropertyController@search']);
});

I don't know how to achieve this in Laravel 5. I found this https://mattstauffer.co/blog/laravel-5.0-middleware-filter-style , but don't know how to use that to do what I want. 我不知道如何在Laravel 5中实现这一点。我发现这个https://mattstauffer.co/blog/laravel-5.0-middleware-filter-style ,但不知道怎么用它来做我想要的。

Edit: The Laravel 4 way of doing this would allow this priority to be set per route, so I'm not just loading all of the package routes before the app. 编辑:Laravel 4执行此操作的方法将允许为每个路由设置此优先级,因此我不只是在应用程序之前加载所有包路由。

You should be able to change the order in which routes are registered by changing the order of service providers in config/app.php . 您应该能够通过更改config/app.php中服务提供商的顺序来更改路由的注册顺序。

Currently it probably looks somewhat like this: 目前它可能看起来像这样:

'providers' => [
    // ...
    'App\Providers\RouteServiceProvider',
    // ...
    'Vendor\Package\PackageServiceProvider',
],

Now just change the order so the package is loaded first: 现在只需更改顺序,以便首先加载包:

'providers' => [
    // ...
    'Vendor\Package\PackageServiceProvider',  // register package routes first
    'App\Providers\RouteServiceProvider',
    // ...
],

To just prioritize specific routes you can (ab)use the service providers register() method. 要优先考虑特定路由,您可以(ab)使用服务提供者register()方法。 I don't really like method but it works and I couldn't find anything better... 我真的不喜欢方法,但它有效,我找不到更好的东西......

When the service providers are loaded the register() method of every provider is called. 加载服务提供程序时,将调用每个提供程序的register()方法。 After that (and in the same order) the boot() method. 之后(并以相同的顺序) boot()方法。 That means independent of the order of your providers the register() method in your package will always be called before the boot() method in the RouteServiceProvider . 这意味着独立于提供者的顺序,包中的register()方法将始终在RouteServiceProviderboot()方法之前RouteServiceProvider This could look somewhat like this: 这可能看起来像这样:

class PackageServiceProvider extends ServiceProvider {
    public function boot(){
        // register the regular package routes
    }

    public function register(){
        // register route "overrides"
        // for example like this: (obviously you could also load a file)
        app('router')->get('/search/properties', ['as' => 'properties','uses' => 'PropertyController@search']);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM