Question

I hope that someone will be able to help me as I've been going a little crazy trying to get this to work. I have done numerous searches online and find tidbits of information, but unfortunately, I can't seem to get to a solution.

I'm trying to achieve the following in Phalcon with multiple modules.

MasterController extends Controller
BackendController extends MasterController
ModuleController extends BackendController
ClientsController extends ModuleController

The folder structure I have is this:

apps
|->crm
|-->controllers
|--->ModuleController
|--->ClientsController
common
|->controllers
|-->MasterController
|-->BackendController

Now in my Modules.php file under CRM I have the following:

    $loader->registerNamespaces(
        array(
            'Multiple\Crm\Controllers'      => __DIR__ . '/controllers/',
            'Multiple\Crm\Models'           => __DIR__ . '/models/',
            'Multiple\Crm\Plugins'          => __DIR__ . '/plugins/',
            'Multiple\Common\Controllers'   => __DIR__ . '/../../common/controllers/'
        )
    );

My ModuleController file looks like this:

class ModuleController extends Multiple\Common\Controllers\BackendBase
{
    public function onConstruct()
    {
        echo "hello";
    }
}

Whenever I run the code, I end up with the following fatal error:

Fatal error: Class 'Mulitple\Common\Controllers\BackendBase' not found in /var/www/phalcon/html/apps/crm/controllers/ModuleController.php on line 8

I have tried numerous things, but I cannot seem to get it to work. Can anyone shed some light on this and tell me what I'm doing wrong?

Many thanks in advance.

Was it helpful?

Solution

After reviewing the comments by @cvsguimaraes and @NikolaosDimoploulos I took the option to "start again". I used the phalcon devtools to generate the basis of the structure using the apps option.

Based on this I was then able to "build" the structure up from there.

In order to have the inheritance I was after this is what I ended up with:

#File: apps/crm/controllers/ClientsController.php
namespace myApp\CRM\Controllers

class ClientsController extends ControllerBase {...}

#File: apps/crm/controllers/ControllerBase.php
namespace myApp\CRM\Controllers

class ControllerBase extends \myApp\common\Controllers\FrontendBase {...}

#File: common/controllers/FrontendBase.php
namespace myApp\common\Controllers

class FrontendBase extends \myApp\common\Controllers\MasterBase {...}

#File: common/controllers/MasterBase.php
namespace myApp\common\Controllers

class MasterBase extends \Phalcon\Mvc\Controller {...}

Then in the file: apps/crm/Module.php I have the following:

namespace myApp\CRM;

class Module implements ModuleDefinitionInterface
{

    public function registerAutoloaders()
    {
        $loader = new Loader();
        $loader->registerNamespaces(array(
            'myApp\CRM\Controllers' => __DIR__ . '/controllers',
            'myApp\CRM\Models' => __DIR__ . '/models',
            'myApp\common\Controllers' => __DIR__ . '/../../common/Controllers',
            'myApp\common\Models' => __DIR__ . '/../../common/Models'
        ));
        $loader->register();
    }
}

This then works as you would expect. You can have functions in the MasterBase that you can call from the higher levels and you can also run functions such as adding css and JS to the output at the different levels, allowing you to separate out the control as you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top