Yii 1.1: Setting and getting systemwide static parameters

15 followers

Occasionally one wishes to set systemwide parameters for an application, such as a contact address for email, an application name, or setting an option that guides major behavior. Yii provides for setting of static parameters in the configuration file, and this article talks about how to do it conveniently.

The config file

In protected/config/main.php you'll find a 'params' attribute that can be populated with an array of name/value pairs:

return array(
    ...
    'params' => array(
        'email'      => 'steve@unixwiz.net',
        'someOption' => true
    )
)

These options are available at runtime via the Yii::app()->params[...] hash, which is indexed by the parameter name. And though it's possible to use it directly, it's prudent to check parameter existence first with isset(), so this makes it somewhat less convenient.

Our approach is to create a helper function to fetch these by name, with the caller providing a default value in the event the parameter is not set:

// in a helper file
function yiiparam($name, $default = null)
{
    if ( isset(Yii::app()->params[$name]) )
        return Yii::app()->params[$name];
    else
        return $default;
}
 
// in application code
$email      = yiiparam('email');             // null returned if not defined
$someOption = yiiparam('someOption', true);  // default to true if param not defined

This helper function can be placed in a common helper-function file as documented in this helpful wiki article; this is a great way to provide helper functions that your application needs often, but don't warrant a module, component, or special class.

Sharing params between configs

Users with console subsystems may well wish to share these parameters between main.php and config.php, and this can be done by creating a shared, common include file:

// in protected/config/_common.inc
$commonParams = array(
    'email'      => 'steve@unixwiz.net',
    'someOption' => true
);
 
 
// in protected/config/main.php
require_once('_common.inc');
 
return array(
    ...
    'params' => $commonParams
);
 
 
// in protected/config/console.php
require_once('_common.inc');
 
return array(
    ...
    'params' => $commonParams
);

Note that this method is for static configuration parameters - it's doesn't provide for dynamic parameters changed (or persisted) at runtime, or per-user settings.

Total 3 comments

#16540 report it
Trejder at 2014/03/04 07:57am
@JayRoe

This article is about Yii's configuration params. What your example (about configuring database connection, using connection string) has to do with it? I don't seem to be understanding your idea. I get your point with skipping the middle-man, but I don't get, what is this example about?

#12525 report it
codesutra at 2013/03/26 01:10am
Appricate Alternative Solution

@JayRoe i like your alternative solution. Thanks for it. :)

#2559 report it
JayRoe at 2011/01/17 01:19pm
Alternative

This is just a matter of preference, but personally I prefer skipping the middleman, $commonParams, and instead load the parameters directly.

//main.php
'components'=>array(
   ...
   'db'=>require('_db.php'),
   ...
)
 
//_db.php
<?php return array(
    'connectionString' => 'mysql:host=localhost;dbname=myDatabase',
    'username' => 'myUser',
    'password' => 'myPass',
);

Leave a comment

Please to leave your comment.

Write new article
  • Written by: Steve Friedl
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +10
  • Viewed: 49,935 times
  • Created on: Dec 28, 2010
  • Last updated: Dec 28, 2010
  • Tags: config