Yii2. Access to higher level folder

前端 未结 2 643
陌清茗
陌清茗 2020-12-02 02:50

Simple question. I use Yii2 advanced template. In apache I have DocumentRoot \"{$path}/www/yii-application1/frontend/web\". How can I

相关标签:
2条回答
  • 2020-12-02 03:31

    Step : 1

    First create .htaccess file in here yii-application1/.htaccess

    Options +FollowSymlinks
    RewriteEngine On
    
    # deal with backend first
    RewriteCond %{REQUEST_URI} /(backend)
    RewriteRule ^backend/assets/(.*)$ backend/web/assets/$1 [L]
    RewriteRule ^backend/css/(.*)$ backend/web/css/$1 [L]
    RewriteRule ^backend/image/(.*)$ backend/web/image/$1 [L]
    
    RewriteCond %{REQUEST_URI} !/backend/web/(assets|css|image)/
    RewriteCond %{REQUEST_URI} /(backend)
    RewriteRule ^.*$ backend/web/index.php [L]
    
    
    RewriteCond %{REQUEST_URI} /(assets|css|js|img|font)
    RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
    RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
    RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
    RewriteRule ^image/(.*)$ frontend/web/image/$1 [L]
    
    RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|js|image|font)/
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ frontend/web/index.php
    

    Step : 2

    Now create a components/Request.php file in common directory and write below code in this file.

    Request.php file

    <?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 resolvePathInfo(){
            if($this->getUrl() === $this->adminUrl){
                return "";
            }else{
                return parent::resolvePathInfo();
            }
         }
      }
    ?>
    

    Step : 3

    Now Installing component. Write below code in frontend/config/main.php and backend/config/main.php files respectively.

    //Frontend
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => [
                'name' => '_frontendUser', // unique for frontend
            ]
        ],
        'session' => [
            'name' => 'PHPFRONTSESSID',
            'savePath' => sys_get_temp_dir(),
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                '<alias:>' => 'site/<alias>',
            ],
        ],
        'request'=>[
            'cookieValidationKey' => '[gfhjghsdjks44fdf4fgf4fgfg5645ggxcvvc]',
            'csrfParam' => '_frontendCSRF',
            'class' => 'common\components\Request',
            'web'=> '/frontend/web'
        ],
    ]
    
    //Backend 
    'components' => [
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => false,
            'identityCookie' => [
                'name' => '_backendUser', // unique for backend
            ]
        ],
        'session' => [
            'name' => 'PHPBACKSESSID',
            'savePath' => sys_get_temp_dir(),
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        'request'=>[
            'cookieValidationKey' => '[ruiqwybnddiogj786789hzcassdas9dasdjufi]',
            'csrfParam' => '_backendCSRF',
            'class' => 'common\components\Request',
            'web'=> '/backend/web',
            'adminUrl' => '/backend'
        ],
    ]
    

    Your domain.com/frontend/web problem solved to follow the above steps. and you can access the index page for domain.com/frontend/web using domain.com.

    Now You can access you image using

    <?php echo Html::img('uploads/ring.jpg') ?>
    

    Also you can access the domain.com/frontend/web/image using below code

    <?= Html::img(Yii::getAlias('@web').'/image/xyz.png', ['class' => 'retina']); ?>
    

    also you get webroot path using this Yii::getAlias('@webroot')

    0 讨论(0)
  • 2020-12-02 03:39

    You can also use alise inside config array on web.php file of config folder like

     'aliases' => [
        '@uploads' => '@app/web/upload',
    ],
    

    and use it in any where you want like

     <?= Html::img("@uploads/image/xyz.png", ['class' => 'retina']); ?>
    
    0 讨论(0)
提交回复
热议问题