Dhara
Dhara

Reputation: 1481

Get Root Directory in yii2 advanced app

I want to get root directory of my project in yii2 advanced app.

My Url is like following

http://localhost:83/Working-copy/mySite/backend/web/index.php

I have used

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

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

but it gives me

/var/www/Working-copy/mySite

i have used

Yii::$app->request->BaseUrl

But it also gives

/Working-copy/mySite/backend/web

i only want

/Working-copy/mySite

Any Help??

Upvotes: 1

Views: 6486

Answers (2)

Dhara
Dhara

Reputation: 1481

Here is my solution.

  1. Create components into common folder.
  2. Add Request.php

    namespace common\components;
    
    
    class Request extends \yii\web\Request {
        public $web;
        public $adminUrl;
    
        public function getBaseUrl(){
            return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;
        }
    
        public function getActualBaseUrl(){
            return str_replace($this->web, "", parent::getBaseUrl());
        }
    
        public function resolvePathInfo(){
            if($this->getUrl() === $this->adminUrl){
                return "";
            }else{
                return parent::resolvePathInfo();
            }
        }
    }
    
  3. Use Yii::$app->request->getActualBaseUrl(). // this gives me /Working-copy/mySite

Upvotes: 1

user5201742
user5201742

Reputation:

You have two apps, backend and frontend, and each has a different BaseURL. The path /Working-copy/mySite it's not a BaseURL for any app. But you can have this value if you set in params.

Upvotes: 0

Related Questions