Crud: [QUESTION] How can I resolve custom CrudPanel for CrudController?

Created on 25 Apr 2017  路  9Comments  路  Source: Laravel-Backpack/CRUD

Hello. I extend your CrudPanel class in order to override some traits for my purpose. But in your main CrudController it's resolved via helper function app().

public function __construct()
{
    if (! $this->crud) {
        $this->crud = app()->make(CrudPanel::class); // Here is

        $this->middleware(function ($request, $next) {
            $this->request = $request;
            $this->crud->request = $request;
            $this->setup();

            return $next($request);
        });
    }
}

I'd like to know how I can inject there another implementation of CrudPanel? And why did you not just inject it like a dependency via constructor? It'd be easier to replace it using contextual binding.

Thanks!

Most helpful comment

For anyone looking for a solution for this in Backpack v4, you can now do

$this->app->extend('crud', function () {
    return new \App\MyExtendedCrudPanel;
});

in the boot method of any of your service providers (e.g. AppServiceProvider). This is the official way of doing this now, although I couldn't find it in the documentation anywhere.

All 9 comments

One could extend the CrudController from Backpack and do this:

/*
 * Use the local version of CrudPanel (i.e. make one and put it somewhere that the autoloader
 * can find it).
 */
use App\Crud\CrudPanel as CrudPanel;

use Backpack\CRUD\app\Http\Controllers\CrudController; as BaseController;

class CrudController extends BaseController
{
}

I think this should work.

@lloy0076 No, it doesn't work. I made a little trick in constructor of child class but it's not pretty in my opinion.
namespace Admin\Backpack;

use Admin\Backpack\CrudPanel as CustomCrudPanel;
use Backpack\CRUD\app\Http\Controllers\CrudController as BaseCrudController;

class CrudController extends BaseCrudController
{
    public function __construct()
    {
        parent::__construct();

        $this->crud = app()->make(CustomCrudPanel::class);
        $this->crud->request = $this->request;
    }

}

If Backpack owners wants, I can create PR with proper solution.

p.s. Thanks for nice admin panel. I like your way of using traits

Maybe one does have to specifically override the constructor; where I have I've done:

/*
 * Use the local version of CrudPanel.
 */
use App\Crud\CrudPanel as CrudPanel;

// Other use clauses

class CrudController extends BaseController
{
// variable / slot definitions

    public function __construct()
    {
        if (! $this->crud) {
            $this->crud = app()->make(CrudPanel::class);

            // call the setup function inside this closure to also have the request there
            // this way, developers can use things stored in session (auth variables, etc)
            $this->middleware(function ($request, $next) {
                $this->request = $request;
                $this->crud->request = $request;
                $this->setup();

                return $next($request);
            });
        }
    }
// etc etc

I guess one could configure the CrudPanel::class in the config/backpack/crud.php configuration file.

Hi guys,

@lloy0076 I think that's a an excellent solution - specifying a different CrudPanel class from the config file.

I don't see a solution for using different CrudPanel classes across different CrudControllers, but I don't think that's a realistic use case - if you want to overwrite something, you usually overwrite it for all controllers. That being said, you can still do that, by overwriting the constructor (current way).

@Shandur - does this solution sound good to you too?

Cheers!

I think this thread provides a quick solution for anyone who wants to extend the CrudPanel class, so I'll close the issue. Cheers!

For anyone looking for a solution for this in Backpack v4, you can now do

$this->app->extend('crud', function () {
    return new \App\MyExtendedCrudPanel;
});

in the boot method of any of your service providers (e.g. AppServiceProvider). This is the official way of doing this now, although I couldn't find it in the documentation anywhere.

You're totally right, thank you @carlobeltrame - just mentioned this in the docs. Cheers!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

voidstate picture voidstate  路  3Comments

AlexanderWM picture AlexanderWM  路  3Comments

M0H3N picture M0H3N  路  3Comments

abewartech picture abewartech  路  3Comments

GenericHero01 picture GenericHero01  路  3Comments