Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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



83.4k
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.