1
votes

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?

1
Where is AdminController and what is namespace of AdminController? - alu
When you say you included the file in the bootstrap, are you directly including the file, or are your providing the directory location in the configuration section for autoload? - JackB
Directly including the file in the bootstrap using require. Ill post up the file structure later when i get home if it will help. - user794846
I'm having the same problem right now. It's due to your method registerAutoloaders on the Module.php in each modules. On it we define a new autoloader wich overloads the previous and invalidate all your other registered namespaces. No clue on how to avoid it. I will try to answer if I find something. - Surt
thanks let me know how you get on. - user794846

1 Answers

1
votes

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.