1

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?

user794846
  • 1,881
  • 5
  • 29
  • 72
  • Where is `AdminController` and what is namespace of `AdminController`? – alu Nov 12 '14 at 23:58
  • 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 Nov 13 '14 at 14:37
  • 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 Nov 13 '14 at 14:47
  • 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 Apr 15 '15 at 19:04

1 Answers1

1

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.

cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
  • No, it is still not found. My controller base is Core\Controllers\ControllerBase My other module's controller is Modules\Contact\Controllers\IndexController it is already extends \Core\Controllers\ControllerBase but still error not found. – vee Mar 21 '15 at 16:55