0

I am getting really confused on how namespace and use are being used here to reference my CategoryController. I am using laravel 8. Any guidance would be greatly appreciated!

enter image description here

My route uses resource and that seems to be ok. web.php:

namespace App\Http\Controllers\Admin\Category;
Route::get('/admin/categories', [CategoryController::class, 'category'])->name('categories');

My Category Controller is App\Http\Controllers\Admin\Category\CategoryController.php as shown here is my folder structure:

enter image description here

CategoryController.php:

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class CategoryController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:admin');
    }

    public function category(){
        echo "Hello Category";
    }

}
0

1 Answer 1

5

Your namespaces should mimic your directory structure. So if you have a CategoryController.php file that resides in the folder app/Http/Controllers/Admin/Category, then the namespace for that file would be namespace App\Http\Controllers\Admin\Category;

namespace App\Http\Controllers\Admin\Category;

use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    // stuff
}

When referencing classes, you include the relevant namespace with the use statement.

So in your web.php file you want to replace:

namespace App\Http\Controllers\Admin\Category;

with:

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

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.