Yii2如何访问“root"中的文件夹文件夹? [英] Yii2 How to access a folder in "root" folder?

查看:32
本文介绍了Yii2如何访问“root"中的文件夹文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Yii2 高级模板制作了一个网站,我想访问位于根"文件夹中的上传文件夹中的文件.我有:

I have made a website with Yii2 advanced template and I want to access files in the upload folder located in the "root" folder. I have :

backend/web/
frontend/web/
uploads/

我遵循了 Yii2 的第一个答案.访问更高级别的文件夹

所以我的 .htaccess 是

So my .htaccess is

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 ^img/(.*)$ frontend/web/img/$1 [L]

RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|js|img|font)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

我的 common/components/Request.php 文件是

My common/components/Request.php file is

<?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();
        }
     }
  }
?>

我的前端/config/main.php

My fronted/config/main.php

..
'components' => [
    'request' => [
        'cookieValidationKey' => 'xxxxxxxxxxxxxxxxx',
        'csrfParam' => '_csrf-frontend',
        'class' => 'common\components\Request',
        'web'=> '/frontend/web'
    ],

现在我可以使用 localhost/myapp(没有/frontend/web/)访问我的网站

Now I can access to my site with localhost/myapp (without /frontend/web/)

在我用

Html::img('../..'/uploads/my-image.png')

但它不再起作用,无论是 ../../uploads 还是uploads/

But it does not work anymore, neither with ../../uploads nore with uploads/

如何访问上传文件夹?

推荐答案

.htaccess 文件中的更改.

RewriteCond %{REQUEST_URI} !/(uploads)/ 

显示在.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 ^img/(.*)$ frontend/web/img/$1 [L]

RewriteCond %{REQUEST_URI} !/(frontend|backend)/web/(assets|css|js|img|font)/
RewriteCond %{REQUEST_URI} !/(uploads)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

这篇关于Yii2如何访问“root"中的文件夹文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆