i am a voyager beginner ,anybody can tell me what is the best way to add custom controllers under voyager admin?
thanks
Whenever we create a table in voyager, Voyager calls it datatype. And for all tables / datatypes created by us, Voyager users only one controller VoyagerBreadController.php located at vendor\tcg\voyager\src\Http\Controllers.
For example, if I create a table named brands. Laravel will use controller VoyagerBreadController.
But where are the routes which use or point to this controller. Routes are located in file vendor\tcg\voyager\routes\voyager.php. In this file, find the following lines:
try {
foreach (\TCG\Voyager\Models\DataType::all() as $dataTypes) {
Route::resource($dataTypes->slug, $namespacePrefix.'VoyagerBreadController');
}
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException("Custom routes hasn't been configured because: ".$e->getMessage(), 1);
} catch (\Exception $e) {
// do nothing, might just be because table not yet migrated.
}
In my version, these lines are between line No. 29 to 37.
As you can see, above code is fetching all our datatypes and creating a resouce route for our tables / datatypes.
Now, if I want to override this route and create a route to use my own controller for a particular action. For example, if I want to create a route for brands/create url. I can do this by simply adding following line (my route) below above code (i.e. after line 37):
Route::get('brands/create', '\App\Http\Controllers\YourController@YourAction' )->name('brands.create');
There are two ways to do this.
First is to overwrite a route to use your own controller.
Route::group(['prefix' => 'admin'], function () {
Voyager::routes();
Route::controller('menus', '\App\YourController');
});
The second is to modify the voyager.controllers.namespace to something like App\Http\Controllers\Voyager and then run php artisan voyager:controllers.
This will create all the controllers existing for Voyager inside your application in the app/Http/Controllers/Voyager folder.
Hope this helps you out.
@marktopper But there doesn't exist any controller method on Route facade
Route::controller('menus', '\App\YourController');
I am using laravel 5.4.
@itstyro
You can use:
Route::get('menus', 'YourController@yourAction');
or
Route::post('menus', 'YourController@yourAction');
or
Route::resource('menus', 'YourController');
The controller method have been removed.
@marktopper yeah ! thanks
This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.
Most helpful comment
Whenever we create a table in voyager, Voyager calls it datatype. And for all tables / datatypes created by us, Voyager users only one controller VoyagerBreadController.php located at vendor\tcg\voyager\src\Http\Controllers.
For example, if I create a table named brands. Laravel will use controller VoyagerBreadController.
But where are the routes which use or point to this controller. Routes are located in file vendor\tcg\voyager\routes\voyager.php. In this file, find the following lines:
try {
foreach (\TCG\Voyager\Models\DataType::all() as $dataTypes) {
Route::resource($dataTypes->slug, $namespacePrefix.'VoyagerBreadController');
}
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException("Custom routes hasn't been configured because: ".$e->getMessage(), 1);
} catch (\Exception $e) {
// do nothing, might just be because table not yet migrated.
}
In my version, these lines are between line No. 29 to 37.
As you can see, above code is fetching all our datatypes and creating a resouce route for our tables / datatypes.
Now, if I want to override this route and create a route to use my own controller for a particular action. For example, if I want to create a route for brands/create url. I can do this by simply adding following line (my route) below above code (i.e. after line 37):
Route::get('brands/create', '\App\Http\Controllers\YourController@YourAction' )->name('brands.create');