How to get root directory in yii2

后端 未结 7 1872
闹比i
闹比i 2020-12-15 03:20

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

相关标签:
7条回答
  • 2020-12-15 03:35

    Open file D:\wamp\www\yiistore2\common\config\params-local.php

    Paste below code before return

    Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));
    

    After inserting above code in params-local.php file your file should look like this.

    Yii::setAlias('@anyname', realpath(dirname(__FILE__).'/../../'));
    
    return [
    ];
    

    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.

    echo Yii::getAlias('@anyname');
    
    0 讨论(0)
  • 2020-12-15 03:39

    Open below file C:\xampp\htdocs\project\common\config\params-local.php

    Before your code:

    <?php
    
    return [
    
    
    ];
    

    after your code:

    <?php
    yii::setAlias('@path1', 'localhost/foodbam/backend/web');
    
    return [
    
    
    ];
    
    0 讨论(0)
  • 2020-12-15 03:40

    To get the base URL you can use this (would return "http:// localhost/yiistore2/upload")

    Yii::app()->baseUrl
    

    The following Code would return just "localhost/yiistore2/upload" without http[s]://

    Yii::app()->getBaseUrl(true)
    

    Or you could get the webroot path (would return "d:\wamp\www\yii2store")

    Yii::getPathOfAlias('webroot')
    
    0 讨论(0)
  • 2020-12-15 03:48

    If you want to get the root directory of your yii2 project use, assuming that the name of your project is project_app you'll need to use:

    echo Yii::getAlias('@app');
    

    on windows you'd see "C:\dir\to\project_app"

    on linux you'll get "/var/www/dir/to/your/project_app"

    I was formally using:

    echo Yii::getAlias('@webroot').'/..';
    

    I hope this helps someone

    0 讨论(0)
  • 2020-12-15 03:49

    Use "getAlias" in Yii2

       \Yii::getAlias('@webroot')
    
    0 讨论(0)
  • 2020-12-15 03:53

    Try out this,

    My installation is at D:\xampp\htdocs\advanced

    \Yii::$app->basePath will give like D:\xampp\htdocs\advanced\backend.

    \Yii::$app->request->BaseUrl will give like localhost\advanced\backend\web\

    You may store the image using \Yii::$app->basePath and show it using \Yii::$app->request->BaseUrl

    0 讨论(0)
提交回复
热议问题