-1

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?

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 1
    Does this answer your question? [Target class controller does not exist - Laravel 8](https://stackoverflow.com/questions/63807930/target-class-controller-does-not-exist-laravel-8) – miken32 Oct 07 '21 at 16:43
  • I read that all the answers to that questions and tried it but still the same I also perform some commands like route:cache, dump-autoload, and optimize. – Inquisitive Oct 08 '21 at 00:25

3 Answers3

3

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.

Brian Thompson
  • 13,263
  • 4
  • 23
  • 43
  • Thank you for the great explanation, Sir. I've been searching for what I can do since I got the wrong path and I don't know how to figure out where I want the properties create only accessible for the agent. So when I enter the add button it routes to "agent/properties/create". If you don't mind Sir, can you give me some example? – Inquisitive Oct 07 '21 at 15:33
-1

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.

OMR
  • 11,736
  • 5
  • 20
  • 35
  • Thank you for your response. I tried it but I got an error that says "agent.dashboard not define." Is there something that I add or change for it to works ? – Inquisitive Oct 07 '21 at 14:55
  • remve 'prefix'=>'agent' ..... replace 'as' with 'name' – OMR Oct 07 '21 at 17:09
  • Thank you so much, Sir, it works now. As I replace as with name so it means that it is the name of the route group? – Inquisitive Oct 08 '21 at 00:42
  • it means that its the name prefix for all routes in this group – OMR Oct 08 '21 at 10:32
-1

your are using laravel 8. add the below line on your web.php .

use App\Http\Controllers\yourcontrollername;