0

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.

rabrowne
  • 61
  • 6
  • 1
    Are you sure that you need a multi-module app? If you're new to Phalcon I'd suggest you to start with a single module application. But if you truly need app modules checkout [this repo](https://github.com/phalcon/mvc), there you can find some structure examples using app modules. About your namespaced controllers [in this answer](http://stackoverflow.com/questions/22591508/namespacing-your-application-classes) you might find some useful information. – cvsguimaraes Apr 14 '14 at 17:38
  • Also check your paths. The ../../ sometimes is not the best way forward. I usually set a ROOT_PATH constant which points to the top of my app and then set the paths with that. – Nikolaos Dimopoulos Apr 14 '14 at 18:58
  • @cvsguimaraes: yes, I do need a multi-module app. I'm trying to convert an existing system to Phalcon. I've checked out your second link and taking the option of registeringClasses, as I only need the two in this instance. I will continue to use this method and see where I get to. Thanks for your help. – rabrowne Apr 14 '14 at 19:52
  • @NikolaosDimopoulos: thanks for the tip. I think I will set up a ROOT_PATH constant and use that as it will make things easier to understand. I did check the paths and I was sure that it was working correctly. I think it was a case that the autoloader wasn't getting the files from the folder I was using in the registerNamespace. Thanks also for your help. – rabrowne Apr 14 '14 at 19:54
  • @rabrowne Have a look at this https://github.com/phalcon/website/blob/master/public/index.php#L8 and this https://github.com/phalcon/website/blob/master/app/var/config/config.php#L11 These are from the official website of PhalconPHP – Nikolaos Dimopoulos Apr 14 '14 at 20:25

1 Answers1

1

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.

rabrowne
  • 61
  • 6