简体   繁体   中英

Routes inside controllers with laravel?

I have been declaring all the routes for my application inside web.php , but it is now getting quite large. I find that I am losing a lot of time shifting between web.php and each controller and this is hurting productivity.

I feel like it would be better to define routes inside of the controller, perhaps ideally delegating some URL to a controller and then allowing the controller to handle the "sub routes" since this would allow me to use inheritance when I have two similar controllers with similar routes.

It is not possible given how laravel works. Every request is passed onto router to find its designated spot viz. the controller with the method. If it fails to find the route within the router, it just throws the exception. So the request never reaches any controller if the route is not found. It was possible in earlier versions on Symphony where you would configure the route in the comment of a particular controller method.

Sadly with laravel it works how it works.

But for me, I just like to have the routes in a separate file.

在此处输入图像描述

Alternate solution, easier way to sort all the routes.

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

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