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

Using SubControllers?

Hi,

I am trying to access a simple subcontroller as follows: /modules/controller/action... I have seen the github example: https://github.com/phalcon/mvc/tree/master/simple-subcontrollers and I have attempted to follow it to a T, but I still get handler class cannot be loaded. It is even more weid because I am able to access it at work under my vagrant environment, but under my Gentoo environment at home it fails to load. I have everything setup correctly, that I know of, but I have no idea where the problem may lie. I am running PHP 5.3.10 at work and PHP 5.5.* on my Gentoo machine. I attempted to make sure the configs were as similar as possible, but still no luck! I ran into a similar issue with attempting to load the PHP DebugBar under the Phalcon loader. That didn't work so I used composer and it worked just fine.

Here is the relevant code:

.htaccess

RewriteBase /~tyler/mvc/

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ public/ [L]
    RewriteRule (.*) public/$1 [L]
</IfModule>
RewriteBase /~tyler/mvc/

AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>

Routers

$router->add('/modules/:controller/:action/:params', array(
    'namespace' => 'MyApp\Controllers\Modules',
    'controller' => 1,
    'action' => 2,
    'params' => 3,
));

$router->add('/modules/:controller', array(
    'namespace' => 'MyApp\Controllers\Modules',
    'controller' => 1
));

$router->add('/modules/:controller/:action', array(
    'namespace' => 'MyApp\Controllers\Modules',
    'controller' => 1,
    'action' => 2
));
/**
* Dispatcher use a default namespace
*/
$di->set('dispatcher', function() {
    $dispatcher = new Dispatcher();
    $dispatcher->setDefaultNamespace('MyApp\Controllers');
    return $dispatcher;
});

<?php

namespace MyApp\Controllers\Modules;

use Phalcon\Mvc\Controller,
    Phalcon\Mvc\Dispatcher;

class ControllerBase extends Controller
{
    //Append software title to the title of each webpage
    public function initialize()
    {
        $this->tag->setTitle(' - ' . $this->config->application->softwareTitle);
    }

    public function beforeExecuteRoute(Dispatcher $dispatcher)
    {
        if (!$this->user->id){
            if ($dispatcher->getControllerName() != 'session') {
                return $this->response->redirect('login');
            }
        }
    }

}

<?php

namespace MyApp\Controllers\Modules;

use Phalcon\Mvc\Controller,
    Phalcon\Mvc\View;

class TestController extends ControllerBase
{
    public function testAction()
    {
       echo 'Hello';

    }

}

Thanks for any help!!!!

Nevermind I solved it of course after many hours of fiddling with it.

Added to the config file:

'controllersmDir'   => __DIR__ . '/../controllers/modules/',

And to the loader:

'MyApp\Controllers\Modules' => $config->application->controllersmDir,