We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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.

Registering Class automatically

Hi guys,

I need a confirmation if what i'm doing isn't redundant.


    $loader = new \Phalcon\Loader();

     * Register a user component
    $di->set('client', function(){
        return new ClientController();

app/view/client/index.php calling html function

    <?php echo $this->getContent(); ?>
    <?php echo $this->client->doHtml(); ?>

This is working, but I really need to register the class? The registerDirs method doesn't do this automatically? Perhaps is there any way to do this happen automatically?

Controllers doesn't have to be registered as a custom Class. Phalcon does this automaticly for you.

Custom classes need to be registered in the service.php if they are not a Controller or Model or sth. like that.

So at the end you can remove this line of code and your app should work too.

     * Register a user component
    $di->set('client', function(){
        return new ClientController();


The autoloader makes your class available. Registering the directory the classfile is in will suffice to make the class available. However, the second part, the "Register a user component" part, is putting a new instance of that class inside the Dependancy Injector. This makes it available in your view as $this->client. This second part isn't necessary, but it is pretty convenient. You could, for instance, not "register" the class, then just do this in your view.

<?php $Client = new ClientController();

I think we can all agree though, that using the Dependancy Injector is much easier and reduces code duplication.

edited Sep '14

Thank you for both responses. I'm familiarizing myself with phalcon yet and sometimes I'm unsure if I'm doing the right way