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

How to define a base images directory in configuration for phalcon website?

I have defined a shared service baseImagesURL in my configuration Class but when I try to use it in volt it throws this error Phalcon\Mvc\View\Exception: Macro 'baseImagesURL' does not exist

     /**
         * Create url helper service
         * Instance of Phalcon\Mvc\Url.
         * This service helps in the setting base images folder URL 
         */
        $di->setShared('baseImagesURL', function() use ($di) {
            /** @var Config $config */
            $config = $di->get('config');
            $url = new Url();
            $url->setStaticBaseUri( $config->path("environment.baseImagesUri"));
            return $url;
        });

Volt

       <img src="{{baseImagesURL('Labor-Sale-big.jpg')}}" >

In volt, when you call {{ url('some path') }}, you're not directly calling the service, you're calling a function defined in Volt that uses the url service.

So what you need to do is define a new function in the compiler to call the service. Actually, I don't think you need a service for this. Something like (untested):

$Compiler->addFunction('baseImagesURL',function($resolvedArgs,$exprArgs) use($Compiler,$di){
        $filename = $Compiler->expression($exprArgs[0]['expr']);
        $config = $di->get('config');
        return $config->path('environment.baseImagesUri').'/'.$filename;
    });

The tricky part is that the function is supposed to return valid PHP code that gets put in the compiled template to be executed. So you may need to play around with what gets returned.

Alternatively, you could just forgoe the function and just do:

<img src = "{{ config.environment.baseImagesUri }}/Labor-Sale-big.jpg" />