2

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 Answer 1

1

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)
{
    //
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! How to pass that value to controller constructor?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.