如何覆盖Laravel Nova控制器?

时间:2019-06-20 15:34:47

标签: laravel controller overriding

由于Laravel Nova全局搜索会忽略策略,因此我想覆盖它的控制器。某种程度上,这不起作用,全局搜索默认为Laravel\Nova\Http\Controllers\SearchController

我到目前为止所拥有的:

app/config.php


    //..
    'providers' => [

        //..
        App\Providers\RouteServiceProvider::class,
        App\Providers\NovaServiceProvider::class,
       //..
    ],

(请注意提供者的顺序)

routes/web.php

//..
Route::get('nova-api/search', 'SearchController@index');

app/Http/Controllers/SearchController.php


namespace App\Http\Controllers;

use Laravel\Nova\Nova;
use Laravel\Nova\GlobalSearch;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Http\Controllers\SearchController as NovaSearchController;

class SearchController extends NovaSearchController
{
    /**
     * Get the global search results for the given query.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function index(NovaRequest $request)
    {
        return (new GlobalSearch(
            $request, Nova::globallySearchableResources($request)
        ))->get();
    }
}

版本

  • Laravel版本:5.8.24
  • Nova版本:2.0.6
  • PHP版本:7.2.10

这不起作用。路线nova-api/search仍指向Laravel\Nova\Http\Controllers\SearchController。我在做什么错了?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,这就是我的解决方法。

在AppServiceProvider.php中,将以下代码添加到引导方法中:

$this->app->bind(
   NovaController::class, 
   YourController::class
);

Here's the description of how it works