laravel pass controller object instead of class

Viewed 149

In Laravel 8.0, web.php, I want to pass controller object instead of class name, is this possible somehow?

Example:

$controller = new CrudController();
$controller->title = 'View items';

Route::get('/{table}/view', [$controller, 'index']);

$controller->title = 'Edit items';
Route::get('/{table}/edit', [$controller, 'index']);
1 Answers

That wouldn't work as you would think. You don't need an instance of the Controller. You can define your route like normal to the action needed and pass a default route parameter for this 'title' instead:

Route::get('{table}/view', [CrudController::class, 'index'])
    ->defaults('title', 'View items');

public function index($table, $title)
{
    //
}
Related