Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii - getPathOfAlias inside setPathOfAlias

Tags:

php

yii

I'm trying to set alias path in Yii to my file upload directory in

testweb
   - ...
   - protected
   - ...
   - myupload

So I put like this in protected/config/main.php:

Yii::setPathOfAlias('upload_dir', Yii::getPathOfAlias('webroot') . '/myupload');

But then when I echo the alias, I only get '/myupload'

echo Yii::getPathOfAlias('upload_dir'); //only returns /myupload
like image 839
navilink Avatar asked Jul 03 '26 09:07

navilink


1 Answers

You can't call getPathOfAlias() inside your main.php configuration file, because path aliases are created in the constructor of CApplication. But the constructor wasn't called yet at the point when main.php is included.

The right way to configure path aliases, is to use the aliases property in your main.php. In your case you could do:

return array(
    'aliases' => array(
        'upload_dir' => 'webroot.myupload',
    ),
    ...

Also note, that you can (and should) make use of the dot notation for aliases.

like image 78
Michael Härtl Avatar answered Jul 04 '26 22:07

Michael Härtl