Custom controller for the frontend

Solved
miquelangeldaniel

Nov 19th, 2020 05:24 PM

I want to create a controller for the frontend views. The tutorial about that uses the web.php insteand, but I don'r like to put a lot of code on this file and I want to create a controller instead. I created one and extended the BaseController but I get always an error because laravel can't find my controller class. How can I do that? thanks

thinkverse

Nov 19th, 2020 05:35 PM

Best Answer

Depends on the version of Laravel. Since Laravel 8+ the namespace lookup was removed. The old style of looking for a controller via string isn't working anymore out-of-the-box.

//web.php

// Deprecated
Route::get('path', 'ExampleController@index');

For 8+ you have to pass it in via the fully qualified name.

//web.php
use App\Http\Controllers\ExampleController;

Route::get('path', [ExampleController::class, 'index']);

For more information check our the Laravel documentation on controllers.

I also just published a new article on controllers called Laravel Quickie: Basic Controllers that might be helpful.

Report
1
miquelangeldaniel

Nov 23rd, 2020 12:10 PM

Ops I didn't realize that I installed the 8.12. And I need as you said to declare first the controller. Thanks!

Report
1