简体   繁体   中英

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'

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");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM