Yii Framework: setPathOfAlias() returns null - yii

I am trying to put MaxMind's GeoIp2 into my Yii application. Basically, I copied the files under "src" (see previous link) under protected -> vendors -> maxmind. The folder structure under my application is the following:
protected
|---- vendors
|---- Zend
|---- maxmind
|---- Database
|---- Reader.php
|---- Model
|---- ...
|---- ...
After that, I created the path aliases into my index.php file:
Yii::setPathOfAlias('Zend', Yii::getPathOfAlias('application.vendors.Zend'));
Yii::setPathOfAlias('GeoIp2', Yii::getPathOfAlias('application.vendors.maxmind'));
The path works just fine for the 'Zend' alias, but it fails for 'GeoIp2' by returning null.
Yii::createApplication("FrontendApplication", $config)->run();
echo "Path 1: " . Yii::getPathOfAlias("Zend"). '<br />'; // Correct path!
echo "Path 2: " . Yii::getPathOfAlias("GeoIp2"). '<br />'; // <==== NULL
echo "Maxmind path: " . Yii::getPathOfAlias('application.vendors.maxmind'). '<br />'; // correct path
var_dump(is_dir(Yii::getPathOfAlias('application.vendors.maxmind'))); // true
Any ideas why this could happen?
Thanks!

getPathOfAlias() call to create path aliases are created in the constructor of CApplication. But the constructor wasn't called yet at the point when main.php is included.
To configure path aliases use the aliases property in your main.php. Like this:
return array(
'aliases' => array(
'GeoIp2' => 'application.vendors.maxmind',
),
...

Fixed! The solution:
Instead of calling setPathOfAlias() into the index.php file I added the aliases into my configuration file (i.e. protected -> config -> main.php), as paramaters, like this:
$config = array(
'import' => array(),
'components' => array(),
...
'aliases' => array(
'Zend' => 'application.vendors.Zend',
'Maxmind' => 'application.vendors.Maxmind',
),
...
'params' => array()
);
Apparently, index.php is not the right place to declare this. It may be because of the autoloader, I am not 100% sure, but since Zend has an autoloader and MaxMind doesn't, that's why it may work for Zend and not for MaxMind. Doing this made the things work. As you may notice, I also moved the Zend alias path to the same place, for consistency reasons :)

Related

How to update Puppet ini_setting or ini_subsetting resource without section header in conf file?

I wonder if someone can help me with my conf file problem. I need to get the output like below but I get problems in using the inifile. I have put below my code and testing output. My service won't start because of the '[]'. Your comments and ideas are highly appreciated. Thanks!
Expected output
cat /etc/service.conf
info something something...
without section header
setting1=value1
Testings
testscript1.pp
ini_setting {'setx':
ensure => present,
path => '/etc/service.conf',
key_val_separator => '=',
setting => 'setting1',
value => 'value1',
}
output of testscript1.pp
cat /etc/service.conf
info something something...
[setx]
setting1=value1
testscript2.pp
$defaults = {
ensure => present,
path => '/etc/service.conf',
key_val_separator => '=',
}
$settings = {
' ' => {
'setting1' => 'value1',
}
}
create_ini_settings($settings,$defaults)
output of testscript2.pp
cat /etc/service.conf
info something something...
[ ]
setting1=value1
Since I really wanted to delete the [] character because it's causing error during service restart, I used section_prefix => '#',. The first puppet agent run is smooth and working. Problem now is if puppet agent runs on its frequency time (like let's say every hour), it will auto-append details in conf file due to lack of section header. I decided to use ini_subsetting but I'm getting errors with it.
testscript3.pp
ini_subsetting {'subset':
ensure => present,
section => '',
key_val_separator => '=',
path => '/etc/service.conf',
setting => 'setting1',
subsetting => '',
value => 'value1',
}
output of testscript3.pp
Error: Failed to apply catalog: Parameter path failed on Ini_subsetting[subset]: File paths must be fully qualified, not '/etc/service.conf'.
Any suggestions or advises are highly appreciated.
Thank you.
If the file you are managing does not have section markers of some kind, then it is not an INI file, not even in the generalized sense that the puppetlabs/inifile module supports. To the best of my knowledge, you'll need to choose a different approach to managing the file.
You could consider templating the whole file, or writing a custom type and provider for it, but before going to so much trouble, you should consider whether a good old file_line resource from puppetlabs/stdlib would be adequate for your needs.
Have you tried your testscript1.pp with section => ''?
It would look like this:
ini_setting {'setx':
ensure => present,
path => '/etc/service.conf',
key_val_separator => '=',
section => '',
setting => 'setting1',
value => 'value1',
}
And the output would be:
cat /etc/service.conf
info something something...
setting1=value1
Or you could try to use force_new_section_creation => false, as it is true by default and forces the creation of a section, as stated in the module’s reference.
As for your 3rd example, it probably fails because of the blank subsetting parameter. The ini_subsetting resource type requires both setting and subsetting parameters to work.

How to declare a static variable with base url in Yii

I am using a variable
$path = Yii::app()->basePath.'/../images1/';
in many functions of a controller. How can i declare it as global?
In your main.php config you may use params.
...
// application-level parameters that can be accessed
// using Yii::app()->params['paramName']
'params' => require(__DIR__ . '/params.php'),
In params.php, for example.
return [
...
'imagesPath' => __DIR__ . '/../../images1/',
];
PHP 5.3+ required to use __DIR__ constant.
With PHP 5.2 you may use dirname(__FILE__) expression.

git ignore Yii database details

Some of the details in the main.php needed by all application instances (URL details) and some details will be specific to each application instance (database details).
Is there any idea to separate the database details from protected/config/main.php?
Just include the shared configuration from another PHP file:
main.php:
return array
(
....
'components' => array
(
'db' => include('sharedDatabaseConfiguration.php');
)
);
sharedDatabaseConfiguration.php:
return array('host' => ...);
You might have to add a path or something, depending where the file is stored.
Edit: Btw, Yii also has a fancy CMap::mergeArray() function that can do something similar (in case you want to "augment" the contents of a single config file with that from another one. Look at the default generated console.php for an example of that.
You can find an idea here: Manage application configuration in different modes .
Basically it works by importing a different PHP file (your db configuration) and merging the includedarrays:
<?php
return CMap::mergeArray(
require(dirname(__FILE__).'/db-config.php'),
array(
'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name' => 'Page Title',
...
)
);
?>
You can use separate configuration file (e.g. protected/config/production.php), that is based on your main configuration file and that overrides some settings using CMap::mergeArray as this answer suggests:
return CMap::mergeArray(
require(dirname(__FILE__) . '/main.php'),
array(
'components' => array(
'db' => array(
'connectionString' => '...',
'username' => '...',
'password' => '...',
),
),
)
);
Then you can add protected/config/production.php to .gitignore.

Yii - How to implement curly brackets in your module/models?

I must be missing something here, because I have the following in my config/main.php:
'db'=>array(
...some config...
'tablePrefix' => 'appname_'
...more config...
),
Then I created a new module (testModule) and have listed the table name there as 'test_user':
public $userTable = '{{test_user}}';
In my DB migration script I create the mysql table like so:
$this->createTable(Yii::app()->getModule('test')->userTable, array(
"id" => "pk",
"username" => "varchar(20) NOT NULL",
"password" => "varchar(128) NOT NULL",
"email" => "varchar(128) NOT NULL",
"active" => "varchar(128) NOT NULL",
"created" => "int(10) NOT NULL",
"updated" => "int(10) NOT NULL",
));
BUT, when I run the migration script, the table name in the DB is {{test_user}}, when what I expected was 'appname_test_user'.
What am I doing wrong here?
Check your console config file. You should have there the same 'tablePrefix' in 'db' section as in main.php (or where you store configuration of web app db). Running migrations, you execute yiic and console application config is used.
To avoid that confusion in future, you can move all settings of db component into another config file (ex config/db.php). Then you can include it in both config/console.php and config/main.php this way:
//..
'db'=>require('db.php'),
//..

PHPBB adding custom page in a new directory

My version of PHPBB is installed /board of my site
I am trying to make a custom page inside a new directory called store the file is called decals.php
decals.php
<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
page_header('Store - Decals');
$template->set_filenames(array(
'body' => 'store_decals_body.html',
));
make_jumpbox(append_sid("{$phpbb_root_path}viewforum.$phpEx"));
page_footer();
?>
the store_decals_body.html is uploaded to my styles directory and i cant seem to get mysite.com/board/store/decals.php to work. It always put me to mysite.com/board
Have I overlooked something here?
If you have the PHP file in a subdirectory, change the root path accordingly:
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : '../';