FabricioG
FabricioG

Reputation: 3320

How to NOT use full controller path in Laravel routes

In Laravel web routes I use the full path to a controller like so:

'App\Http\Controllers\frontend\EmailController@index'

This gets a little long and redundant. How can I use a path like so:

'EmailController@index'

Upvotes: 2

Views: 841

Answers (1)

miken32
miken32

Reputation: 42709

This is not how routes are declared in Laravel any more. You should be doing something like this in modern versions:

<?php

use App\Http\Controllers\frontend\EmailController;

Route::get("/email", [EmailController::class, "index"]);

IIRC, for older versions the App\Http\Controllers was always assumed (or set as a property in the route service provider) and you could just use "frontend\EmailController@index", or wrap it in a route group to remove the "frontend" prefix:

<?php

Route::namespace("frontend")->group(function () {
    Route::get("/email", "EmailController@index");
});

Upvotes: 3

Related Questions