-2

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

<?php

namespace App\Http\Controllers\Backend\Admin\Users;

use App\Models\User;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Role;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use App\Repositories\Interfaces\UserRepositoryInterface;

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 Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Backend\Admin\DashboardController;
use App\Http\Controllers\Backend\Admin\Users\UserController;

Route::group(['namespace' => 'Backend\Admin', '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 [Backend\Admin\App\Http\Controllers\Backend\Admin\Users\UserController] does not exist. And in debuggar the following routing come: Backend\Admin\App\Http\Controllers\Backend\Admin\Users\UserController@index I want a bit assistance on how to use namespace for the resource controller and routing

2 Answers 2

0

You use UserController::class which returns App\Http\Controllers\Backend\Admin\Users\UserController. So when You add namespace option to the group, the result will be:

Backend\Admin\App\Http\Controllers\Backend\Admin\Users\UserController

Change it to:

Route::resource('users', 'Users\UserController');
Sign up to request clarification or add additional context in comments.

3 Comments

I would say to remove the namespace and always use ::class so it is trackable by any IDE that understands PHP. Using 'Users\UserController' is not trackable at all...
@matiaslauriti you are saying to use like this : Route::resource('users',Users\UserController::class);?
No. he is saying remove 'namespace' => 'Backend\Admin' from route group options and the rest of your code
0

Actually you are using namespace of class directly! Obviously namespace work incorrectly. You can use string namespace instead of this way!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.