user794846
user794846

Reputation: 1931

Shared base controller between modules

I am setting up a multi-module application, so far I have it setup like this example http://docs.phalconphp.com/en/latest/reference/applications.html.

But I was wandering if its possible to have shared base controller that both the backend and frontend controllers extend from. This is so I can have a single ACL in the base controller. How would I set that up?

According to the docs I can create a controllerbase anywhere and then just require this file directly in the bootstrap file or cause to be loaded using any autoloader. So I created a folder called apps/shared/controllers/ControllerBase.php and required this file directly in the bootstrap file but this does not work.

If I try to load a controller like so:

 class AdminController extends ControllerBase
 {

       public function indexAction()
       {
            echo "<h1>Hello admin!</h1>";
        }
  }

I get an error ...Backend\Controllers\ControllerBase' not found in......

So how do I cause to be loaded using any autoloader as per the docs? Do I need to register it as its own namespace or something?

Upvotes: 1

Views: 454

Answers (1)

cvsguimaraes
cvsguimaraes

Reputation: 13240

You not using the full namespace path for your base controller so the autoloader attempts to find it under in the same namespace of the child class.

Try something like this:

namespace MyApp\Backend\Controllers;

use MyApp\Shared\Controllers\ControllerBase;

class AdminController extends ControllerBase
{
    public function indexAction()
    {
        echo "<h1>Hello admin!</h1>";
    }
}

This answer consider that you have applied the PSR-0 and PSR-4 properly.

Upvotes: 1

Related Questions