2017-10-06 106 views
0

Yii2找不到资产。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

+0

请给'/frontend/resources/AppAsset.php'文件** $ basePath **和** $ baseUrl **路径? – vishuB

+0

参考[Yii2。访问更高级别的文件夹](https://stackoverflow.com/questions/38426427/yii2-access-to-higher-level-folder)。也许帮忙。 – vishuB

+0

/frontend/assets/AppAsset.php - 'namespace frontend \ assets; 使用yii \ web \ AssetBundle; class AppAsset extends AssetBundle { public $ basePath ='@webroot'; public $ baseUrl ='@web'; public $ css = [ 'frontend/web/css/site.css', ]; public $ js = [ ]; public $ depends = [ 'yii \ web \ YiiAsset', 'yii \ bootstrap \ BootstrapAsset', ]; }' – RedSpace

回答

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'] 
     ], 
    ],