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.

User Defined Function in VOLT

according to phalcon docs,

 $compiler->addFunction('widget', function ($resolvedArgs, $exprArgs) {
    return 'MyLibrary\Widgets::get(' . $resolvedArgs . ')';
});

I have a class with some general functions located at app/lib/general.php

How should I register a random function withing that class in volt?

$compiler->addFunction(
                'itemStatus',
                function ($resolvedArgs, $exprArgs) {
                    return "\\lib\\general.php\\general::_GetStatus({$resolvedArgs})";
                }
            );

Please Step by Step.

Thanks

edited Oct '15
$compiler->addFunction(
    'itemStatus',
    function ($resolvedArgs, $exprArgs) {
        return 'Lib\General::functionName('. $resolvedArgs .')';
    }
);

Having the above setup:

Lib - is your namespace, you should register it if you did not. Like so for example:

    public function registerAutoloaders()
    {   
        $loader = new \Phalcon\Loader();
        $loader->registerNamespaces(array(
            'Lib' => __DIR__ . '/app/lib/', 
        ));
        $loader->register();
    }

General - is the classname ;

And in volt you can call it like:

{{ functionName(voltVariable) }}

Thanks for your answer but its not working... What am I doing wrong?

my services.php

<?php
$compiler->addFunction(
                'itemStatus',
                function ($resolvedArgs, $exprArgs) {
                    return 'Lib\General::_GetStatus('. $resolvedArgs .')';
                }
            );
?>

my loader.php

<?php
$loader->registerNamespaces(array(
            'Lib' => __DIR__ . '/app/lib/general.php', 
));
?>

my volt

<td>{{ itemStatus (item.status) }}</td>

Error: Fatal error: Class 'Lib\General' not found

Try changing your namespace declaration to:

<?php
$loader->registerNamespaces(array(
            'Lib' => __DIR__ . '/app/lib/', 
));
?>

Also could you show me your project folder structure, its something with the path.

Here goes my folder structure

project/app/ - cache - config - controllers - lib <---- here is located general.php which has the class General - migrations - models - views

project/public - css - img - ...

If your index.php is in project/public the namespace path should be:

 'Lib' => __DIR__ . '/../app/lib/',

Still not working =//

In general.php (class General) should I write something? like namespace ?



62.7k
Accepted
answer
edited Oct '15

Define namespace on top of your file

namespace Lib;

If its still not working you may consider uploading your code to Github so we can view and help :)

<3 Thank you!!!

I have more issues, can I ask?

1 - How do I use the same class in any controller? 2 - How do I divide the views into backend and frontend? For example.... http://mysite.com redirects to frontend and http://mysite.com/admin redirects to backend.

Thanls

edited Oct '15

Glad you made it! :)

You can mark this as resolved, so other people with your problem may find it useful.

About your new questions you can view phalcon skeleton examples here: https://github.com/phalcon/mvc

In the list multiple will answer your 1 and 2 questions. If you need any other help you can create new topic with the corresponding title, because it gets really messy to heave multiple questions in 1 topic :)