Laravel 5.4 - How to override route defined in a package?

强颜欢笑 提交于 2019-12-18 15:51:43

问题


I have created a package in Laravel 5.4 that sets up a basic backoffice. This package contains several routes that are using controllers from within the package. What I want to be able to do is to override the package defined routes in my application in order to plug custom controllers. For example, if I have a route

        Route::get('login', [
            'as' => 'admin.login',
            'uses' => 'Auth\LoginController@showLoginForm'
        ]);

defined in my package that will use the Vendor\Package\Controllers\Auth\LoginController I want to defined a route for my application that will override that one and use the App\Controllers\Auth\LoginController.

Doing the obvious approach of defining the route in app routes files fails for the app routes files are run before the package routes file, so the package definition will prevail.

Is there any way to accomplish something of this kind?

I also tried to get the particular route in the RouteServiceProvider and use the method uses to set the controller that should be used to resolve it, like this

public function boot()
    {
        parent::boot();
        Route::get('admin.login')->uses('App\Http\Controllers\Admin\Auth\LoginController@showLoginForm');
    }

but this also fails to accomplish what is pretended.

Any clues on what I am doing wrong?


回答1:


In config/app.php in the providers array put the service provider of the package before App\Providers\RouteServiceProvider::class, and then in your web.php routes you'll be able to override it with your custom route.

EDIT For Laravel package auto discovery you can disable the package being auto discovered in your composer.json like this:

"extra": {
    "laravel": {
        "dont-discover": [
            "barryvdh/laravel-debugbar"
        ]
    }
},

In this example the barryvdh/laravel-debugbar package won't be autodiscovered, which means you would have to manually include its service provider in the config array and then you'll be able to rearrange your custom provider in the desired order.




回答2:


Another option -- which doesn't have to muck with the order of service providers -- is to add a binding for the controller. So e.g. in AppServiceProvider,

$this->app->bind(
    \Vendor\Package\Controllers\Auth\LoginController::class,
    App\Controllers\Auth\LoginController::class
);

You'll have to match controller method names, but you're doing that already in your example.

(Caveat on this answer: I haven't tested it in Laravel 5.4, but I just did this in Laravel 6.0 using the $bindings property which was added in Laravel 5.6. That said, this should be correct 5.4 syntax for doing the same thing).



来源:https://stackoverflow.com/questions/44723747/laravel-5-4-how-to-override-route-defined-in-a-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!