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

Problem with Loading View Helpers (Tags)

I am trying to add a View Helpers file in my App Directory with a class called HoaTags. I'm using namespaces and the Phalconphp class loader, however it's having a problem in autoloading the class when it's called.

Here's my app/HoaTags/HoaTags.php file:

<?php

namespace HoaView\HoaTags;

use Phalcon\Tag;

class HoaTags extends Tag
{
 /* Helpers code here */
}
?>

Here is my loader.php file snippet

$namespaces = array(
    'HoaView\Models'      => $config->application->modelsDir,
    'HoaView\Controllers' => $config->application->controllersDir,
    'HoaView\Forms'       => $config->application->formsDir,
    'HoaView\HoaTags'     => $config->application->helpersDir,
    'HoaView'             => $config->application->libraryDir,
);

$loader->registerNamespaces($namespaces);

$loader->register();

And finally, my services.php file where I am adding it to the $di:

$di->set('hoatags', function () {
    return new HoaTags();
});

The problem occurs in the views when I try to call or instantiate a new HoaTags object. The error says that the class is not found: Fatal error: Class 'HoaView\HoaTags' not found ...

I've been over this many times trying to find what's not loading correctly. Any help is greatly appreciated by the Phalconphp community! Thanks!



85.5k
edited Dec '15
$di->set('hoatags', function () {
    return new \HoaView\HoaTags();
});

can you try it this way please ?

also, are you sure this 'HoaView\HoaTags' => $config->application->helpersDir, is correct, seems like you could placed different name by accident ?

@Izo, did try it that way and as well I have an include at the top of the services.php for use HoaView\HoaTags;, but it still didn't work. As for the loader array for namespaces, I just noticed that the last entry in the namespace array puts pretty much everything under the /library directory for the namespace. I wonder if this couldn't be overridden, just as we're doing now?



2.2k
Accepted
answer
edited Dec '15

Ok, I'm going to answer my own question here, since I just figured out what was happening. I think it's more related to PSR-0 issue other than anything. So, because my directory structure was ~/myApp/App/HoaTags/HoaTags.php I had to include the directory while using the namespace method in $loader rather than the directory method. So, I included the following:

use HoaView\HoaTags\HoaTags;

And this seemed to solve it all. Thanks!