简单的问题。 我用Yii2
先进的模板。 在apache
我DocumentRoot "{$path}/www/yii-application1/frontend/web"
。 如何访问/www/yii-application1/uploads
为了显示图像的用户? 下面的code
无法正常工作:
<?php echo Html::img('../../uploads/ring.jpg') ?>
它的工作原理与DocumentRoot "{$path}/www/yii-application1/"
但在这种情况下, index
网站的页面看起来像domain.com/frontend/web
。 但我只需要domain.com
。
步骤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步
现在创建一个components/Request.php
文件common
的目录和文件编写下面的代码。
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
分别文件。
//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
问题解决了按照上面的steps
。 你可以访问index
的网页domain.com/frontend/web
使用domain.com
。
现在你可以使用你访问图像
<?php echo Html::img('uploads/ring.jpg') ?>
您也可以访问domain.com/frontend/web/image
使用下面的代码
<?= Html::img(Yii::getAlias('@web').'/image/xyz.png', ['class' => 'retina']); ?>
还你使用这个根目录路径Yii::getAlias('@webroot')
您还可以在config文件夹中的文件web.php使用ALISE配置阵列内像
'aliases' => [
'@uploads' => '@app/web/upload',
],
并在您想要像使用任何
<?= Html::img("@uploads/image/xyz.png", ['class' => 'retina']); ?>
文章来源: Yii2. Access to higher level folder