jeudi 26 octobre 2017

Rewriting laravel 5.1 controller action route on the Request object

I have many modules my source code organized into subfolders inside the App\Http\Controllers e.g App\Http\Controllers\ModuleOne.

The base controllers are in App\Http\Controllers and the module controllers extend these base controllers. A module controller might not exist if I don't want to customize the base controller when using that particular module.

I want to write a logic where a route checks if the module controller exists. If the route does not exist, it should route the action to a BaseController.

I have tried to make a middleware and other solutions but can't seem to get this done.

I want to have the routing inside all the controllers done with the same name (thus ignoring the module name - which will be defined by an env variable). So, to simplify code I want to call:

Route::get('apple','AppleController@view')

and from this route it should check if:

App\Http\Controller\module1\module1_AppleController.php

exists.

If it does, use it. If not, it should route to the base controller action i.e. App\Http\Controller\AppleController.

Can't seem to figure out where to do this with efficient code. Can the rewrite be done in RouteServiceProvider in middleware or other?

Also, if the newer version of Laravel could present a solution not found in 5.1, I am willing to upgrade, so don't limit answers to 5.1.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire