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

后端 未结 2 2033
我在风中等你
我在风中等你 2021-02-06 04:36

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

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 04:42

    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).

    Edit: For Laravel 6+ you can instead add the binding to the bindings array in AppServiceProvider:

    public $bindings = [
        \Vendor\Package\Controllers\Auth\LoginController::class =>
            App\Controllers\Auth\LoginController::class,
        // other bindings
    ]
    

提交回复
热议问题