0

I have separated my project routes into home.php that contains the client-side routes and admin.php that contains server-side routes.

So here is my RouteServiceProvider.php:

public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace($this->namespace) ->group(base_path('routes/web/home.php')); Route::middleware(['web', 'auth.admin']) ->namespace($this->namespace . '\Admin') ->prefix('admin') ->group(base_path('routes/web/admin.php')); }); } 

So as you see I have specified ->namespace($this->namespace . '\Admin') because of Admin Controllers that are placed in this directory:

App\Http\Controllers\Admin\...

Then in the admin.php, I added this route:

Route::resource('users', UserController::class); 

But I get this error:

Target class [Admin\UserController] does not exist. 

So what's going wrong here? How can I solve this issue and properly call the Controller from Admin?

3
  • Can you add that full UserController file here ....checkout the namespace of that userController also.. Commented Jun 7, 2022 at 8:52
  • @SaravanaSai It has this namespace: namespace App\Http\Controllers\Admin; Commented Jun 7, 2022 at 8:54
  • Have you properly imported on your routes file check i out ...i think its should b e something like this 'Route::resource('users',App\Http\Controllers\Admin\ UserController::class); ' try it out this Commented Jun 7, 2022 at 8:56

3 Answers 3

2

You might need to add a use statement to point out where your controller lies within the route file.

For example:

use App\Http\Controllers\Admin\UserController; 
Sign up to request clarification or add additional context in comments.

Comments

0

The namespace changes are correct.

The problem is a missing using.

So to correct it, change in your route file:

use App\Http\Controllers\Admin\UserController; // <-- add this. Route::resource('users', UserController::class); 

Comments

0

You need to adjust this for the RouteServiceProvider.php:

public function boot() { $this->configureRateLimiting(); $this->routes(function () { Route::prefix('api') ->middleware('api') ->namespace($this->namespace) ->group(base_path('routes/api.php')); Route::middleware('web') ->namespace('\App\Http\Controllers') ->group(base_path('routes/web/home.php')); Route::middleware(['web', 'auth.admin']) ->namespace('\App\Http\Controllers\Admin') ->prefix('admin') ->group(base_path('routes/web/admin.php')); }); } 

And then you can run this:

Route::resource('users', 'UserController'); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.