Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i override a Laravel Nova controller?

Because Laravel Nova global search ignores policies, i want to override it's controller. Somehow this doesn't work and the global search defaults to Laravel\Nova\Http\Controllers\SearchController.

What i have so far:

app/config.php


    //..
    'providers' => [

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

(please note the order of the providers)

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();
    }
}

Versions

  • Laravel Version: 5.8.24
  • Nova Version: 2.0.6
  • PHP Version: 7.2.10

This does not work. The route nova-api/search still points to Laravel\Nova\Http\Controllers\SearchController. What am i doing wrong?

like image 332
Peer Avatar asked Nov 01 '25 04:11

Peer


1 Answers

I had the same problem, here's how I solved it.

In AppServiceProvider.php added the following code to the boot method:

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

Here's the description of how it works

like image 156
user3486769 Avatar answered Nov 03 '25 17:11

user3486769



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!