You can move your route registration into controllers if you use static methods for this. The code below is checked in Laravel 7
In web.php
use App\Http\Controllers\MyController; ..... MyController::registerRoutes('myprefix');
In MyController.php
(I use here additional static methods from the ancestor controller also posted below)
use Illuminate\Support\Facades\Route; ..... class MyController extends Controller { ...... static public function registerRoutes($prefix) { Route::group(['prefix' => $prefix], function () { Route::any("/foo/{$id}", self::selfRouteName("fooAction")); Route::resource($prefix, self::selfQualifiedPath()); } public function fooAction($id) { ........ }
In Controller.php
class Controller extends BaseController { .... protected static function selfAction($actionName, $parameters = [], $absolute = false) { return action([static::class, $actionName], $parameters, $absolute); } protected static function selfQualifiedPath() { return "\\".static::class; } protected static function selfRouteName($actionName) { //classic string syntax return "\\".static::class."@".$actionName; // using tuple syntax for clarity return [static::class, $actionName]; } }
selfAction mentioned here is not related to your question, but mentioned just because it allows making correct urls for actions either by controller itself or any class using it. This approach helps making action-related activity closer to the controller and avoiding manual url-making. I even prefer making specific functions per action, so for example for fooAction
static public function fooActionUrl($id) { return self::selfAction('foo', ['id' => $id]); }
Passing prefix into registerRoutes makes controller even portable in a sense, so allows inserting it into another site with a different prefix in case of conflict
/toy-controller?action=updateweb.phpis getting large then you're probably also getting to the point where you will get a significant performance improvement by using route caching. Doing what you're suggesting would prevent you from caching your routes (And missing out on performance gains) as well has making it harder to find the routes later.