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

How to use Loader by registering prefixes?

I cannot access the Controller after set loader to use Prefixes strategy. I tried to rename the HomeController to something like "MyPrefix_HomeController", but it's not working.

edited Jul '14

Could you give me a complete MVC example? I have read and follow that part of document, but stuck while making the Conctrollers and Models to work with it. Do we need to modify the Dispatcher like namespace strategy?

Edit: I have tested in public/index.php, the loader could found my Prefix_HomeController class. But still don't know how to make it work with MVC setup. HomeController handler class cannot be loaded



98.9k
Accepted
answer

The dispatcher will not find the class because it does not expect that it has a prefix. Maybe a solution could be:

Create your own router:

<?php

class MyRouter extends \Phalcon\Mvc\Router
{
    public function getController()
    {
        if ($this->_controllerName) {
            return 'Prefix_' . $this->_controllerName;
        }
    }  
}

In your services replace the default router:

$di->set('router', function(){
    return new MyRouter();
}, true);

According to the source of \Phalcon\Mvc\Router, I changed to below.

class MyRouter extends \Phalcon\Mvc\Router
{
    public function getControllerName()
    {
        if ($this->_controller) {
            return 'Vc_' . $this->_controller;
        }
    }
}

return new MyRouter();

But it's still not working.