我可以使用Laravel 5 Middleware允许程序包覆盖应用程序路由吗? [英] Can I use Laravel 5 Middleware to allow packages to override app routes?

查看:56
本文介绍了我可以使用Laravel 5 Middleware允许程序包覆盖应用程序路由吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用包中的路由覆盖app/Http/routes.php中定义的路由.

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:

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

我希望能够在/vendor/author/package/src/Http/routes.php中定义它:

I want to be able to define this in /vendor/author/package/src/Http/routes.php:

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

首先加载app/Http/routes.php文件,以便使用其中的路由,而不是软件包.

The app/Http/routes.php file is loaded first so the route in their is used, not the package.

在Laravel 4中,我将使用App :: before或App :: after来执行此操作,并为其赋予优先级.

In Laravel 4 I would do this using App::before or App::after, giving them a priority.

在包裹路线中也是如此:

Like so in the package routes:

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

我不知道如何在Laravel 5中实现这一目标.我找到了 https://mattstauffer.co/blog/laravel-5.0-middleware-filter-style ,但是不知道如何使用它来完成我想要的事情.

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 4的实现方式将允许为每个路由设置此优先级,因此我不只是在应用程序之前加载所有包路由.

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.

推荐答案

您应该能够通过更改config/app.php中的服务提供者的顺序来更改路由注册的顺序.

You should be able to change the order in which routes are registered by changing the order of service providers in 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',
    // ...
],


要仅确定特定路由的优先级,您可以(ab)使用服务提供商的register()方法.我不太喜欢方法,但是它可以用,我找不到更好的方法...


To just prioritize specific routes you can (ab)use the service providers register() method. I don't really like method but it works and I couldn't find anything better...

在加载服务提供程序时,将调用每个提供程序的register()方法.之后(以相同的顺序)boot()方法.这意味着与提供程序的顺序无关,总是在RouteServiceProvider中的boot()方法之前调用包中的register()方法.这可能看起来像这样:

When the service providers are loaded the register() method of every provider is called. After that (and in the same order) the boot() method. 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. 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']);
    }
}

这篇关于我可以使用Laravel 5 Middleware允许程序包覆盖应用程序路由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆