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

Custom Tag

I'm creating my custom tag but I'm having trouble including my tag class.

So currently I have separated my controllers and that my affect my problem. So I start from the beginning.

I have 2 namespaces:

'MyApp\Controllers' => DIR . '/../controllers/', 'MyApp\Controllers\Ajax' => DIR . '/../controllers/ajax/'

I've placed my custom tags in app/library/tags and added file BSelect.php

namespace MyApp\Controllers;

class BSelect extends \Phalcon\Tag {

public static function bselect($params) {

    return "test";

}

}

In my services.php I use:

$compiler->addFunction('bselect',

            function ($resolvedArgs, $exprArgs) {

                return 'BSelect::bselect()';

            }

        );

So the problem is whenever I use bselect call in volt I'm getting Fatal error: Class 'BSelect' not found

Though I have

$loader->registerDirs(

array(

    $config->application->controllersDir,

    $config->application->controllersAjaxDir,

    $config->application->modelsDir,

    $config->application->tagsDir

)

)

where $config->application->tagsDir is defined in config.php which is

return new \Phalcon\Config(array(

'database' => array(

    'adapter'     => 'Mysql',

    'host'        => 'localhost',

    'username'    => 'xxxx',

    'password'    => 'xxxx',

    'dbname'      => 'xxx',

    'charset'     => 'utf8',

),

'application' => array(

    'controllersDir'        => APP_PATH . '/app/controllers/',

    'controllersAjaxDir'    => APP_PATH . '/app/controllers/ajax/',

    'modelsDir'             => APP_PATH . '/app/models/',

    'migrationsDir'         => APP_PATH . '/app/migrations/',

    'viewsDir'              => APP_PATH . '/app/views/',

    'pluginsDir'            => APP_PATH . '/app/plugins/',

    'libraryDir'            => APP_PATH . '/app/library/',

    'tagsDir'               => APP_PATH . '/app/library/tags/',

    'cacheDir'              => APP_PATH . '/app/cache/',

    'baseUri'               => '/myapp/',

)

));

I really confused with namespaces and don't know what I'm doing wrong.

Please help. And thanks in advance.



1.0k

Sorry can't format my code properly in the forum.

As I see, you register everything as directories but no namespaces. That's why you get that not found class

https://docs.phalcon.io/en/latest/reference/loader.html#registering-namespaces



1.0k

Thanks Stefan, It worked. And sorry for late reply.