问题描述
简单的问题.我使用 Yii2
高级模板.在 apache
我有 DocumentRoot "{$path}/www/yii-application1/frontend/web"
.如何访问 /www/yii-application1/uploads
以向用户显示图像?以下 code
不起作用:
Simple question. I use Yii2
advanced template. In apache
I have DocumentRoot "{$path}/www/yii-application1/frontend/web"
. How can I access /www/yii-application1/uploads
in order to show image to user?Following code
does not work:
<?php echo Html::img('../../uploads/ring.jpg') ?>
它适用于 DocumentRoot "{$path}/www/yii-application1/"
.但在这种情况下,网站的 index
页面看起来像 domain.com/frontend/web
.但我只需要 domain.com
.
It works with DocumentRoot "{$path}/www/yii-application1/"
. But in this case an index
page of website looks like domain.com/frontend/web
. But I need just domain.com
.
推荐答案
Step : 1
首先在这里创建.htaccess文件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
步骤:2
现在在 common
目录中创建一个 components/Request.php
文件,并在该文件中写入以下代码.
Now create a components/Request.php
file in common
directory and write below code in this file.
Request.php 文件
<?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();
}
}
}
?>
步骤:3
正在安装组件.将下面的代码分别写在frontend/config/main.php
和backend/config/main.php
文件中.
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'
],
]
您的 domain.com/frontend/web
问题已按照上述 步骤
解决.您可以使用 domain.com
访问 domain.com/frontend/web
的 index
页面.
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
.
现在您可以使用
<?php echo Html::img('uploads/ring.jpg') ?>
您也可以使用以下代码访问domain.com/frontend/web/image
Also you can access the domain.com/frontend/web/image
using below code
<?= Html::img(Yii::getAlias('@web').'/image/xyz.png', ['class' => 'retina']); ?>
你也可以使用这个 Yii::getAlias('@webroot')
这篇关于yii2.访问更高级别的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!