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

Storing abitrary strings/paths etc

Hi there,

I want to read and reference certain files in my application. Most of them are off the value returned by $this->url->getBasePath() which is set to my document root. Is there a recommended approach in Phalcon to store paths and strings so that they are globally accessible, rather than keep defining them in each class that uses them?

Thanks.

theres not really a recommended approach ... i try to keep it simple by setting path to application folder in configuration file and use that as base path whereever its needed. as for relative urls i think generating urls with url service is the way to go.



38.8k

Thanks for your advice. I've done this in index.php

 /**
   * The URL component is used to generate all kind of urls in the application
   */
  $di->set('url', function() use ($config){
    $url = new \Phalcon\Mvc\Url();
    $url->setBaseUri('/');
    $url->setBasePath($_SERVER['DOCUMENT_ROOT']);
    return $url;
  });

but I have a number of third party libraries that need additional pathing and it would be nice to have them specified once with some kind of centralised access. I think I've been recently spoiled by being able to registered my custom services in the DI container and just access them so easily.

It says a lot about Phalcon - it's really awesome :)

You can store those 3rd party libraries in the DI as well. You could also store the path to the libraries in your config, and just reference that whenever necessary.



38.8k

I had some issues with file paths when initially using a config.ini file - I actually switched back to inlining the paths in the index.php file. Do you use a config.ini or some other approach as your config file approach?

Thanks.

I use a config.php file and Phalcon\Config.

When referencing the config from controllers, I simply do:

include $this->config->dirs->path_to_library

In models I have a custom di() function that returns a static instance of the DI container.



38.8k

I wonder whether you'd be prepared to share your config.php file (well, the content structure)? I'm not sure what you mean by this - is this a Phalcon thing or something that you "created"! :)

Cheers.

Here's an abbreviated version of my config file:

<?php

/**
 * Set some path properties used in config
 */
$FS       = '/var/www/htdocs/';
$WEB      = '';

/* Categories of configuration are listed first.  One-offs are listed after */
$Config = new Phalcon\Config([      
        ,'dirs' =>  ['config'                => $FS.'/app/config'
                        ,'app'              => $FS.'/app'
                        ,'app_web'          => $WEB
                        ,'assets_web'           => $WEB.'/public/assets'
                        ,'file_budget'      => realpath($_SERVER['DOCUMENT_ROOT'].'/../uploads/budget')
                        ,'file_acceptance'  => realpath($_SERVER['DOCUMENT_ROOT'].'/../uploads/acceptance')
                        ,'file_user'            => realpath($_SERVER['DOCUMENT_ROOT'].'/../uploads/user')
                        ,'public'               => $FS.'/public'
                        ,'public_web'           => $WEB.'/public'
                        ,'views'                => $FS.'/app/views'
                        ,'views_compile'        => $FS.'/app/views/compiled/'
        ]
        ,'username_max_length'  => 20
    ]
);

return $Config;

And how it is put into my DI in my bootstrap.php

$Config = require 'config.php';
$DI->setShared('config',$Config);

Little known fact: Files you reference with include() and require() can return a value.



38.8k

Thank you very much! :)