A little help please, I have this route in web.php
Route::group(['prefix'=>'agent','namespace'=>'Agent','middleware'=>
['auth','agent'],'as'=>'agent.'], function()
{
Route::get('/dashboard',[AgentController::class, 'index'])->name('dashboard');
Route::resource('/properties', PropertyController::class);
});
When I run the command below,
php artisan route:list
I got this error:
Illuminate\Contracts\Container\BindingResolutionException
Target class [Agent\App\Http\Controllers\PropertyController] does not exist.
at C:\xampp\htdocs\sweethomeFinal\vendor\laravel\framework\src\Illuminate\Container\Container.php:879
875▕
876▕ try {
877▕ $reflector = new ReflectionClass($concrete);
878▕ } catch (ReflectionException $e) {
879▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
880▕ }
881▕
882▕ // If the type is not instantiable, the developer is attempting to resolve
883▕ // an abstract type such as an Interface or Abstract Class and there is
1 [internal]:0 Illuminate\Foundation\Console\RouteListCommand::Illuminate\Foundation\Console{closure}(Object(Illuminate\Routing\Route))
2
C:\xampp\htdocs\sweethomeFinal\vendor\laravel\framework\src\Illuminate\Container\Container.php:877 ReflectionException::("Class Agent\App\Http\Controllers\PropertyController does not exist")
But when I put the "Route::resource('/properties', PropertyController::class);" outside the auth
Route::group(['prefix'=>'agent','namespace'=>'Agent','middleware'=>
['auth','agent'],'as'=>'agent.'], function()
{
Route::get('/dashboard',[AgentController::class, 'index'])->name('dashboard');
});
Route::resource('/properties', PropertyController::class);
It just shows all the route lists. But I wanted to put it inside the auth, may I know what is wrong?
CodePudding user response:
Group namespaces made sense pre-Laravel 8, but now with the suggested way of defining routes as Controller::class
the prefixes are basically useless.
Routing before Laravel 8
Before v8, Laravel used a default prefix defined in RouteServiceProvider
of App\Http\Controllers\
. This meant that you only needed to provide the last part - MyController
and it was automatically built out to the fully qualified class name (App\Http\Controllers\MyController
).
Routing beginning in v8
In v8, the default controller path was removed ($namespace = null
), meaning you have to provide the fully qualified class name yourself, or add the prefix back to the service provider. The most efficient way to do this is using the ::class
syntax which returns the required name. This method of providing the class name is also more IDE friendly, which was one of the main reasons for the switch.
The problem with route group namespaces
In the older way of building out the controller class name, the group namespaces were useful for sub folders in your controllers folder.
The path would get built out like:
{default_prefix} {group_namespace} {controller name}
Yielding:
App\Http\Controllers\ Agent\ PropertyController
.
This is actually still how it works in version 8; however, you're providing the values in a different way:
(null) Agent App\Http\Controllers\PropertyController
, which doesn't make the right path.
Summary
When using the ::class
syntax for Laravel routes, group level namespace prefixes really don't make sense to use anymore.
If you browse the versions of the Laravel documentation, you'll also notice that the usage of group namespaces present in version 7 is not included in the version 8 docs, because even though it still "works", it's not very useful.
CodePudding user response:
when you set namespace for a route group, all the routes in this group take that namespace as prefex for it's name.
remove ,'namespace'=>'Agent'
form the group definition, it should solve it.
see laravel doc for more details.
CodePudding user response:
As OMR said, the namespace agent in the route group is the problem.
Remove this and see if it resolves or inside the route you can set namespace only in the routes you need with another group.