yii2 Question
My yii2 install in d:\wamp\www\yii2store
I want to get above path to save images which will be uploaded by me or users.
I have pass all available arguments in Yii::getAlias('@webroot')
(below are the lists of argument which I have used).
@yii
- framework directory.
@app
- base path of currently running application.
@runtime
- runtime directory.
@vendor
- Composer vendor directory.
@webroot
- web root directory of currently running web application.
@web
- base URL of currently running web application.
And also once I will get above path to save images then how can I get path something like this localhost/yiistore2/upload
to be use in img tag src.
One more thing how can I create my own alias with Yii::setAlias()
and where to create this so that I can load it on every controller.
Open below file
C:\xampp\htdocs\project\common\config\params-local.php
Before your code:
after your code:
Supposing you have a writable "uploads" folder in your application:
You can define a param like this:
Then you can simply use the parameter as:
Just depending on if you are using advanced or simple template the base path will be (following the link provided by phazei):
This way the application will be more portable than using
realpath(dirname(__FILE__).'/../../'));
To get the base URL you can use this (would return "http:// localhost/yiistore2/upload")
The following Code would return just "localhost/yiistore2/upload" without http[s]://
Or you could get the webroot path (would return "d:\wamp\www\yii2store")
Use "getAlias" in Yii2
Try out this,
My installation is at D:\xampp\htdocs\advanced
\Yii::$app->basePath
will give likeD:\xampp\htdocs\advanced\backend
.\Yii::$app->request->BaseUrl
will give likelocalhost\advanced\backend\web\
You may store the image using
\Yii::$app->basePath
and show it using\Yii::$app->request->BaseUrl
Open file
D:\wamp\www\yiistore2\common\config\params-local.php
Paste below code before return
After inserting above code in params-local.php file your file should look like this.
Now to get path of your root (in my case its
D:\wamp\www\yiistore2
) directory you can use below code in any php file.