0
votes

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.

1

1 Answers

0
votes

If you use namespaces correctly, You could organize your Controllers like that. With extending the Controllers from a common BaseController for a certain logic you could do something like this:

<?php

namespace App\Http\Controllers\Default;

use Illuminate\Routing\Controller;

class MyFruitController extends Controller {
  // Implement you callback methods, e.g.:
  public function getApples() {}
  public function getPeaches() {}
}

?>

And in you modules:

<?php

namespace App\Http\Controllers\MyModuleA;

class MyFruitController extends \App\Http\Controllers\Default\MyFruitController {
  // Implement those methods that should differ from default MyFruitController:
  public function getApples() {}
  // getPeaches() will be used from default MyFruitController 
}

?>

And:

<?php

namespace App\Http\Controllers\MyModuleB;

class MyFruitController extends \App\Http\Controllers\Default\MyFruitController  {
  // Implement those methods that should differ from default MyFruitController:
  public function getPeaches() {}
  // getApples() will be used from default MyFruitController 
}

?>

You could repeat that for as many modules you have. So you have the same Controller name that only differs in the namespace. With use of route groups and the env() function you could do cool stuff in your routes.php file:

<?php

...

// Fruits
   Route::group([
        'namespace' => env('module', 'Default'),
        // And your other group options, like middleware, ..
    ], function () {

        Route::get('apples', 'MyFruitController@getApples');
        Route::get('peaches', 'MyFruitController@getPeaches');

    });

...

?>