How do I get a path to the main index.php file?
How do I get a path to the main index.php file?
$this->redirect(Yii::app()->homeUrl);
or just simply
Yii::app()->homeUrl <----- that’s the same as writing ‘yoursite/index.php’
Thanks, bu I wanted something slightly diferent.
I am using an elfinder module and I have to specify a path (not URL!) to the folder with photos, which in my case is in www.example.com/images/content. And my protected folder is somwhere completly outside of public_html, so I cannnot just specify it as
Yii::app()->basePath.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR
For now I have written an additional line in index.php:
define('ROOT_PATH',dirname(__FILE__));
and I use ROOT_PATH constant later in elfinder module. But sth tells me there is a better way…
This one:
www.example.com/images/content
is an url, that can be generated as:
Yii::app()->request->baseUrl."/images/content"
This one:
C:/www/myWebApp/images/content
Is a path, that can be generated as:
Yii::app()->basePath.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'content'
Zaccaria, you are right, but only if I have my application folder in
C:/www/myWebApp/protected
For example, when I have my application folder in
C:/some/very/strange/path/to/protected/folder/not/even/under/www
then
Yii::app()->basePath
points to that folder above and nothing potins to
C:/www/myWebApp
I don’t understand, if your application is installed under
C:/some/very/strange/path/to/protected/folder/not/even/under/www
and your images are in
webroot/images/content
, you need the path:
C:/some/very/strange/path/to/protected/folder/not/even/under/www/images/content
and nothing points to
C:/www/myWebApp
because maybe this directory doesn’t exists, or, if exists, it has nothing concerning with the application.
If you need the url of something that lies close to protected, you can create a url:
Yii::app()->request->baseUrl."/images/content"
that will generate
http://localhost/webapp/images/content
.
What exactly do you need?
assets, themes, images, css folders as well as index.php file are in folder C:/www/mywebapp, so they are accessible via http protocol under http://localhost/mywebapp. But my protected folder, as well as yii framework folder are somewhere outside the C:/www folders so they cannot be requestet via http. Lets say this are C:/dev/yii/framework and C:/dev/mywebapp-protected.
So I can build my path to http://localhost/mywebapp/images/content as
Yii::app()->basePath.'/../../www/mywebapp/images/content';
but maybe there is an variable or some other way to point folder which includes index.php file, so I could point to my folder as
Yii::app()->thisMisteriousVarIamLookingFor.'/images/content';
Do you know what I mean?
Well, usually protected lies at the same level of assets, css images and so on.
If you moved the protected in another path, the variable Yii::app()->basePath cannot help you. You can use Yii::app()->assetsPath.’/../’ and so on.
As far as I know, CWebApplication (the class of Yii::app()) has no this variable you need. You can extend Yii and add this variable if you need.
I have always been fine with Yii::app()->basePath->’/../’ because I never changed the folder structure generated by Yiic webapp
You can use dirname(Yii::app()->getRequest()->getScriptFile()) to recover the path just you want, but you have to remove the filename of the script. I didn’t find any better method, and Quiang is using it in some source code (like in CThemeManager) but I’m pretty sure we need a more strightfoward method…
Otherwise, I think the best method is to set an alias for recurrent paths in main.php like:
$basePath = dirname(dirname(dirname(FILE)));
Yii::setPathOfAlias(‘images’, $basePath . ‘/images’);
and the use them like: Yii::getPathOfAlias(…)
Yii::getPathOfAlias('webroot')