laravel-advanced-route
laravel-advanced-route copied to clipboard
Wrong usage in documentation
I am in the process of migrating my Laravel 7 to Laravel 11.
When I did the upgrade to Laravel 8, I noticed my routing didn't work anymore.
I was receiving error: Target class [HomeController] does not exist.
In usage documentation, I can read:
Route::group(['prefix' => '/', 'middleware' => []], function () {
AdvancedRoute::controller('/auth', 'AuthController');
AdvancedRoute::controller('/cms', 'CmsController');
AdvancedRoute::controller('/shop', 'ShopController');
Route::any('/', 'WebsiteController@anyIndex');
});
However, it seem that we can't do that anymore since Laravel 8.
I had to convert all my existing usage of AdvancedRoute like so:
From this pattern:
AdvancedRoute::controller('/', 'HomeController');
To this pattern
AdvancedRoute::controller('/', \App\Http\Controllers\HomeController::class);
Am I missing something ?
PS: Thank you so much for this EXTREMELY helpfull laravel plugin. I have been using it for years.
I would like to add more information about this.
In fact, in v8 of Laravel, in app/Providers/RouteServiceProvider.php they commented the // protected $namespace = 'App\\Http\\Controllers'; and in v >9, they removed it completly.
So either we re-add it, along with Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php'));
That way, we are able to enter our Controllers in web.php just like in your usage your documentation.
Or else, if we use Laravel as it is by default (so, from v8+) we have to provide full class path in web.php like so.
AdvancedRoute::controller('/', \App\Http\Controllers\HomeController::class);
Or import the HomeController (via USE) and use the short way without full path: AdvancedRoute::controller('/', HomeController::class);
So basically, if someone has a new project (or anythign older than v8) and discover your package, based on documentation, it wont work.
Thank you.
I would highly recommend against using strings. and use the class to get the name, with the correct namespacing.
You may need to prefix with backslash to get the correct path.
Example below:
\AdvancedRoute::controller('/', '\\' . \App\Http\Controllers\Guest\HomeController::class);
BTW. one more hint, if you want to save yourself massive headaches from Laravel's constant changes.
Try to avoid all the Laravel's directories, i.e. "app" as much as possible, to shield yourself from the Laravel incompatibilities between versions:
\AdvancedRoute::controller('/auth', '\\' . \YourCompany\Controllers\AuthController::class);
In this example, I have moved all my controllers to my own directory.
Also I have moved my routes out of Laravel's "routes" directory. leaving only something like:
(new YourCompany\Routes\Web)->routes();
BTW. one more hint, if you want to save yourself massive headaches from Laravel's constant changes.
Try to avoid all the Laravel's directories, i.e. "app" as much as possible, to shield yourself from the Laravel incompatibilities between versions:
\AdvancedRoute::controller('/auth', '\\' . \YourCompany\Controllers\AuthController::class);In this example, I have moved all my controllers to my own directory.
Also I have moved my routes out of Laravel's "routes" directory. leaving only something like:
(new YourCompany\Routes\Web)->routes();
That is clever ;) Thanks for tip!
Documentation has been updated