I want to use group function for root. But when I use the namespace
in a function like
Route::group(['namespace' => 'Admin'], function() {
Route::get('/', [indexController::class, 'index']);
});
I get errors of:
Target class [indexController] does not exist.
And I used
php artisan make:controller Admin/indexController.php
command to create the controller.
And I’ve tried this code, but I got the same error:
Route::group(['namespace' => 'AppHttpControllersAdmin'], function() {
Route::get('/', [indexController::class, 'index']);
});
It works when I have "use AppHttpControllersAdminindexController;" in my code,
but I want to use the namespace.
2
Answers
Based on the information you provided, it seems that you are facing a namespace resolution issue in your Laravel application. The error message "Target class [indexController] does not exist" is indicating that Laravel cannot find the indexController class in the specified namespace.
To resolve this, follow these steps:
1.Check Controller Namespace:
Ensure that the indexController class is defined in the correct namespace. Open the indexController.php file located at AppHttpControllersAdmin and verify that it has the correct namespace defined at the top of the file. It should be something like this:
2.Use Full Namespace in Routes:
If the controller namespace is correct, you should use the full namespace of the controller in your route definition. The full namespace includes the root namespace (AppHttpControllers in this case) and the subnamespace (Admin in this case). Here’s how your route should look:
3.Route Caching:
If you have already made changes to your route or controller files, make sure to clear the route cache using the following command:
This command will clear the route cache and reload your routes with the latest changes.
4.Namespace Autoloading:
Lastly, if the issue persists, make sure that your controller files are being autoloaded properly. Laravel uses Composer to autoload classes, and the composer.json file should have the correct mapping for the controllers. It should look like this:
If you made any changes to the composer.json file, run the following command to update the autoloader:
After following these steps, your route should be able to resolve the indexController class without explicitly importing it with the use statement.
Remember, using namespaces in your routes is a good practice as it helps maintain code organization and readability. If you still encounter issues, double-check the namespace declarations and ensure that the file names and class names are correctly capitalized and match the actual file names.
in the php artisan block below
the .php at the end of block is wrong.
remove your last indexController and run command again without .php I hope it works.