john_ch
john_ch

Reputation: 111

How to route directly to a Laravel Backpack controller action

How can you bypass the CRUD:: routes (ex: Route::crud('profile', 'ProfileCrudController');) and go directly to an action such as Route::get('profile/create', 'ProfileCrudController@create');? Trying any of the routes outside of the crud:: route setup gives different errors for each route. Even simply routing directly to Route::get('profile/', 'ProfileCrudController@index'); throws errors for in_array().

Upvotes: 0

Views: 2328

Answers (1)

tabacitu
tabacitu

Reputation: 6193

Indeed, Backpack uses CrudControllers to load routes. So if your CrudControllers are not error-free, other routes will also suffer, and show errors. This is one downside of loading routes in the controller - your controllers should not have PHP errors. I'd recommend you debug the in_array() problem you have, and fix it. But if you can't do that, or would rather stay away from the way Backpack loads its routes, there's the solution below.

What Route::crud('profile', 'ProfileCrudController'); does is that it calls CrudController::setupRoutes, which in turn calls all methods on that CrudController that look something like this - setupOperationNameRoutes() - methods whose only purpose are to load the route for an operation (ex: setupCreateRoutes, setupUpdateRoutes, setupDeleteRoutes).

If for some reason you are unhappy with the routes added by the Route::crud() macro, it's perfectly to delete that line, and manually write all routes your CrudPanel needs. Your CrudController uses a trait for each each operation that can be performed inside the admin panel. Each operation trait has a setupOperationNameRoutes() method, that clearly defines what routes it needs.

You can:

  • go through each operation your CrudController uses (ex: \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation);
  • inside that file, see the routes needed by that operation (ex: Route::delete('profile/{id}', ['as' => 'profile.destroy', 'uses' => 'ProfileCrudController@destroy', 'operation' => 'delete']);
  • hardcode that route into your routes/backpack/custom.php file, or anywhere else you'd like to hold that route;

Hope it helps.

Upvotes: 1

Related Questions