Voyager: Wanna override TCG\Voyager\Http\Controllers@getContentBasedOnType function

Created on 11 Jan 2017  路  23Comments  路  Source: the-control-group/voyager

I wanna realize multiple upload image, so need override,

TCGVoyagerHttpControllers@getContentBasedOnType function

I don't want to Edit voyager project, how can I override in App/Http/Controllers ?

Thanks a lot!

Most helpful comment

Just wanted to add a tear-away instruction note for anyone who may come upon this page looking to create a controller to override a specific Voyager controller.

1.) Copy the desired controller into your controllers folder, such as VoyagerBreadController.php.
2.) You can then rename it, such as MyBreadController.php
3.) Inside, be sure to change the namespace to namespace App\Http\Controllers;
4.) Also, be sure to change class extension declaration to extend the Voyager controller, as it has some additional helpers (such as getSlug) already in it:

class MyBreadController extends \TCG\Voyager\Http\Controllers\Controller

5.) Add or update any methods as required.
6.) Lastly, go into your AppServiceProvider.php and add in the class declarations:

use TCG\Voyager\Http\Controllers\VoyagerBreadController;
use App\Http\Controllers\MyBreadController;

then inside of the register method, add in the bind as mentioned by @marktopper above:

public function register()
{
     $this->app->bind(VoyagerBreadController::class, MyBreadController::class);
}

All 23 comments

You can publish Voyager controllers on your local app, and edit the methods you desire.
Check Voyager docs: (https://the-control-group.github.io/voyager/docs/v0.10/#getting-started-configuration). Also, check voyager config file. It work's great.

Really nice @akazorg but
Can you share us an example? Duplicated the path. Let say we want to duplicate the tcg/voyager/src/http/controller/VoyagerMediaController.php ?

@akazorg you meaning I must copy all TCG\Voyager\Http\Controllers in my App\Http\controllers, but I just wanna edit one controller file, and edit just one method

any other pathway?

@uchsarath Copy all of Voyager controllers into App\Http\Controllers, and edit voyager config file. It works for me, but this is not I want it.

image

I wanna override like view template

image

Thanks @cloudsben and what I want is the same as you. I want to override only one controller not a whole.

Hello @uchsarath @cloudsben, all working now?
You can update just the controller you want, by writing the method you want to replace.
All voyager controllers are extending the ones in the vendor folder, so you're not overwriting them.
Hope it helps.

There was a bug here, it is fixed in master #512
Tagging a new version later today.

Then you can either overwrite controller namespace by changing the value of the config key voyager.controllers.namespace or by swapping out a single controller by adding the following to your AppServiceProvider in the register method:

$this->app->bind(VoyagerBreadController::class, MyBreadController::class);

@akazorg @marktopper Thanks a lot!

Thanks @akazorg & @marktopper this is really nice. Work well.

@marktopper I wanna override this controller in App/Http/Controllers/Controller.php

project/vendor/tcg/voyager/src/Http/Controllers/Controller.php

how can I write in AppServiceProvider鈥榮 register method? like this?

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

Thanks a lot help me!

That controller is within your own application, you can just open it at app/Http/Controllers/Controller.php.

For more information visit Laravel's Documentation.

Hello sir @marktopper, I mean I just wanna override project/vendor/tcg/voyager/src/Http/Controllers/Controller.php's getContentBasedOnType method into App/Http/Controllers/Controller.php

or cover project/vendor/tcg/voyager/src/Http/Controllers/Controller.php file into App/Http/Controllers/Controller.php

how can I write in AppServiceProvider鈥榮 register method?

Thank you very much!

You can not do that, it only works for the main controllers, not the controllers it extends.

But what would you like to overwrite form there?

@marktopper I just rewrite getContentBasedOnType method in project/vendor/tcg/voyager/src/Http/Controllers/Controller.php

for the moment, this is the only pathway!

Thank you very mush!

I only see the usage in the VoyagerSettingsController.php.

So why don't you just overwrite that controller?

Ohh, I see that it's also used in Controller.php for the method insertUpdateData.

Which is used in VoyagerRoleController.php and VoyagerBreadController.php.

@marktopper Just as you say, Controller.php@insertUpdateData method use getContentBasedOnType, I upload multiple files need rewrite getContentBasedOnType switch type == 'image', and foreach it.

But, It does not matter, I think I do not upgrade version is a good way!

We are open for pull requests that makes it possible to updates multiple files for BREAD as a new field option.

@marktopper Hi, Sir. You meaning is upload multiple files feature is already design? Actually, I'm already implementing upload multiple files features.

Voyager is not designed for uploading multiple files at once, but we would really like to see this as a core feature in Voyager. So maybe you could do a pull request for this feature instead of customizing Voyager.
Will also make it easier for you to upgrade to newer versions of Voyager without having to worry about you custom code.

Just wanted to add a tear-away instruction note for anyone who may come upon this page looking to create a controller to override a specific Voyager controller.

1.) Copy the desired controller into your controllers folder, such as VoyagerBreadController.php.
2.) You can then rename it, such as MyBreadController.php
3.) Inside, be sure to change the namespace to namespace App\Http\Controllers;
4.) Also, be sure to change class extension declaration to extend the Voyager controller, as it has some additional helpers (such as getSlug) already in it:

class MyBreadController extends \TCG\Voyager\Http\Controllers\Controller

5.) Add or update any methods as required.
6.) Lastly, go into your AppServiceProvider.php and add in the class declarations:

use TCG\Voyager\Http\Controllers\VoyagerBreadController;
use App\Http\Controllers\MyBreadController;

then inside of the register method, add in the bind as mentioned by @marktopper above:

public function register()
{
     $this->app->bind(VoyagerBreadController::class, MyBreadController::class);
}

Another way to override a Controller by using the voyager:controllers command, in these controllers you can do the same that @gbrits but without change the AppServiceProvider.php

After of calling the command the controllers look at this:

<?php

namespace App\Http\Controllers\Voyager;

use TCG\Voyager\Http\Controllers\VoyagerController as BaseVoyagerController;

class VoyagerBreadController extends BaseVoyagerController
{
    //
}
  • Copy the content from the original controller in vendor/tcg/voyager to the namespace AppHttpControllersVoyager
  • Change the extends to TCGVoyagerHttpControllersController
  • Example:
  • class VoyagerBreadController extends \TCG\Voyager\Http\Controllers\Controller
  • Add or Edit the original methods

To the VoyagerBreadController result this:
````

namespace AppHttpControllersVoyager;

use IlluminateDatabaseEloquentModel;
use IlluminateHttpRequest;
use IlluminateSupportFacadesDB;
use TCGVoyagerDatabaseSchemaSchemaManager;
use TCGVoyagerEventsBreadDataAdded;
use TCGVoyagerEventsBreadDataDeleted;
use TCGVoyagerEventsBreadDataUpdated;
use TCGVoyagerEventsBreadImagesDeleted;
use TCGVoyagerFacadesVoyager;
use TCGVoyagerHttpControllersTraitsBreadRelationshipParser;

class VoyagerBreadController extends TCGVoyagerHttpControllersController
{
//// METHODS
```

Please don't comment on closed threads that have been untouched for so long.

Also, we don't recommend anyone use the voyager:controllers command as it only causes issues with upgrades.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MikadoInfo picture MikadoInfo  路  3Comments

abacram picture abacram  路  3Comments

wislem picture wislem  路  3Comments

rayqiri picture rayqiri  路  3Comments

TXRRNT picture TXRRNT  路  3Comments