Yii2找不到资产

时间:2017-10-06 09:08:11

标签: apache .htaccess mod-rewrite yii2 yii2-advanced-app

Yii2找不到资产。

设置从地址/前端/网站转发到网站的根目录。

.htaccets

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /    
    RewriteCond %{REQUEST_URI} ^/(admin)
    RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
    RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]
    RewriteRule ^admin/js/(.*)$ backend/web/js/$1 [L]
    RewriteCond %{REQUEST_URI} !^/backend/web/(assets|js|css|js)/
    RewriteCond %{REQUEST_URI} ^/(admin)
    RewriteRule ^.*$ backend/web/index.php [L]
    RewriteCond %{REQUEST_URI} ^assets
    RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
    RewriteCond %{REQUEST_URI} ^css
    RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
    RewriteCond %{REQUEST_URI} ^js
    RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
    RewriteCond %{REQUEST_URI} ^images
    RewriteRule ^images/(.*)$ frontend/web/images/$1 [L]
    RewriteRule ^(.*)$ frontend/web/$1 [L]
    RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css|js)/
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_FILENAME} !-f [OR]
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ frontend/web/index.php
</IfModule>

main.php

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '' => 'site/index',         
        '<action>'=>'site/<action>',
    ],
],
    'assetManager' => [
         'basePath' => '@webroot/assets',
         'baseUrl' => '@web/assets'
    ],  
    'request' => [
        'baseUrl' => ''
    ]

一切都有效,但不是风格。 enter image description here

1 个答案:

答案 0 :(得分:0)

这对我有用,试一试

 Options +FollowSymlinks
RewriteEngine On

# deal with administrator first
RewriteCond %{REQUEST_URI} ^/projects/YiiRestful/(administrator)
RewriteRule ^administrator/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^administrator/css/(.*)$ backend/web/css/$1 [L]

RewriteCond %{REQUEST_URI} !^/projects/YiiRestful/backend/web/(assets|css)/
RewriteCond %{REQUEST_URI} ^/projects/YiiRestful/(administrator)
RewriteRule ^.*$ backend/web/index.php [L]


RewriteCond %{REQUEST_URI} ^/projects/YiiRestful/(assets|css|images)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L] 
RewriteRule ^images/(.*)$ frontend/web/images/$1 [L]

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

前端配置

'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'suffix' => '.html',
            'rules' => [
            ],
        ],

      //access Frontend from backend
        'urlManagerFrontend' => [
            'class' => 'yii\web\urlManager',
            'baseUrl' => '@frontend/web',
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                //['?r=site%2Fconcept' => '/site/concept']
            ],
        ],

公共/配置/ bootstrap.php中

Yii::setAlias('@common', dirname(__DIR__));
Yii::setAlias('@frontend', dirname(dirname(__DIR__)) . '/frontend');
Yii::setAlias('@backend', dirname(dirname(__DIR__)) . '/backend');

后端配置

'request' => [
            'csrfParam' => '_csrf-backend',
            'class' => 'common\components\Request',
            'web'=> '/backend/web',
            'adminUrl' => '/administrator'
        ],

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
        ],
    ],

    //access Frontend from backend
    'urlManagerFrontend' => [
        'class' => 'yii\web\urlManager',
        'baseUrl' => 'http://localhost/projects/YiiRestful',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            //['?r=site%2Fconcept' => '/site/concept']
        ],
    ],