0

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.

3
  • 1
    For productivity and maintainability, I would stick to Laravel way. In my opinion, You are over complicating the routes. It may be a nightmare for other devs while maintaining the code if the routes are in different controller. Commented Jun 19, 2019 at 16:51
  • You can do this by passing a param along with route, which controller will use to determine what to call. e.g. /toy-controller?action=update Commented Jun 19, 2019 at 19:51
  • If you're getting to the point where your web.php is 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. Commented Jun 19, 2019 at 21:07

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

enter image description here

Alternate solution, easier way to sort all the routes.

1 Comment

FYI you can pass the string to the base_path() function and it will concatenate it for you. IMO it looks cleaner. base_path('path/inside/base')
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.