bilalahmedcodes
bilalahmedcodes

Reputation: 1

Laravel Namespace Resource Controller Routing

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

Upvotes: -2

Views: 414

Answers (2)

user20093290
user20093290

Reputation:

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

Upvotes: 0

Mahdi Rashidi
Mahdi Rashidi

Reputation: 437

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');

Upvotes: 0

Related Questions