skip to Main Content

I was working on a laravel project. The following is the code of my UserController:

<?php

namespace AppHttpControllersBackendAdminUsers;

use AppModelsUser;
use IlluminateHttpRequest;
use SpatiePermissionModelsRole;
use AppHttpControllersController;
use IlluminateSupportFacadesSession;
use AppRepositoriesInterfacesUserRepositoryInterface;

class UserController extends Controller
{
protected $userRepository;

    public function __construct(UserRepositoryInterface $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    public function index(Request $request)
    {
        $users = $this->userRepository->all($request);
        return view('backend.admin.users.index')->with(compact('users'));
    }
}

My routes in route.php file are:

<?php
use IlluminateSupportFacadesRoute;
use AppHttpControllersAuthLoginController;
use AppHttpControllersBackendAdminDashboardController;
use AppHttpControllersBackendAdminUsersUserController;

Route::group(['namespace' => 'BackendAdmin', 'prefix' => 'admin'], function () {
    // login for super admin
    Route::middleware('guest')->group(function () {
        Route::get('/login', [LoginController::class, 'index'])->name('login');
        Route::post('/login', [LoginController::class, 'login'])->name('login.submit');
    });
    Route::middleware(['auth', 'role:Super Admin'])->group(function () {
        Route::get('dashboard', [DashboardController::class, 'index'])->name('dashboard');
        Route::get('logout', [LoginController::class, 'logout'])->name('logout');
        Route::resource('users', UserController::class);
    });
});

But whenever I goto the users route by using http://localhost/ecommerce-app/admin/users. The following error occurs:
Target class [BackendAdminAppHttpControllersBackendAdminUsersUserController] does not exist.
And in debuggar the following routing come:
BackendAdminAppHttpControllersBackendAdminUsersUserController@index
I want a bit assistance on how to use namespace for the resource controller and routing

2

Answers


  1. You use UserController::class which returns
    AppHttpControllersBackendAdminUsersUserController. So when You add namespace option to the group, the result will be:

    BackendAdminAppHttpControllersBackendAdminUsersUserController
    

    Change it to:

    Route::resource('users', 'UsersUserController');
    
    Login or Signup to reply.
  2. Actually you are using namespace of class directly!
    Obviously namespace work incorrectly.
    You can use string namespace instead of this way!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search