laravel-advanced-route icon indicating copy to clipboard operation
laravel-advanced-route copied to clipboard

Wrong usage in documentation

Open raphjutras opened this issue 1 year ago • 4 comments

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.

raphjutras avatar Aug 29 '24 19:08 raphjutras

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.

raphjutras avatar Aug 30 '24 12:08 raphjutras

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);

lesichkovm avatar Aug 30 '24 18:08 lesichkovm

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();

lesichkovm avatar Aug 30 '24 18:08 lesichkovm

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!

raphjutras avatar Aug 30 '24 18:08 raphjutras

Documentation has been updated

lesichkovm avatar Sep 26 '24 06:09 lesichkovm