We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Shared base controller between modules

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

But I was wondering 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?



2.6k
edited Nov '14

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?



2.6k

Should I even be using that file structure or is the modules one in devtools better?

I have created another project like so: phalcon project --name test_modules --type modules --enable-webtools

I have noticed that webtools does'nt seem to be working correctly, I get "Please specify a controller directory" when trying to create controllers

And "Database configuration cannot be loaded from your config file" when trying to create models

Scaffolding : "Adapter was not found in the config. Please specify a config variable [database][adapter]"

I have database set up with a table in it and as far as I can tell apps/frontend/config/config.php is config file so why is web tools not seeing it?

Does it even work with more than one module in an site?



7.9k

do you use namespace?

try this solution class AdminController extends \ControllerBase

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?



2.6k

Im am trying to use namespace but not sure how to register a namespace in my shared folder that will work accross both modules?



7.9k

I assume this is your base controller and you follow PSR-4 standard

<?php
// filepath : /var/www/your/project/app/shared/BaseController.php

namespace Vendor\App\Shared;

use Phalcon\Mvc\Controller;

class BaseController extends Controller
{

}

and this is your module controller

<?php
// filepath : /var/www/your/project/app/module/admin/LoginController.php

namespace Vendor\App\Module\Admin\Controller

use Vendor\App\Shared\BaseController;

class LoginController extends BaseController
{

}

and this is autoload code in bootstrap

// filepath : // filepath : /var/www/your/project/public/index.php

$loader->registerNamespaces(
    [
        "Vendor\\App" => __DIR__ . "/../app"
    ],
    true
)->register();


2.6k

I am not sure what PSR-4 standard is but Ill look at this code and get back to you later. Do you know about my second question regarding the webtools?



7.9k

I dont think webtools dont work with module, i have never use web tools before