在Yii2 Advanced模板中的前端和后端之间共享资产 [英] Share Assets between frontend and backend in Yii2 Advanced template
问题描述
我一直在尝试从我的项目的前端和后端加载一个简单的CSS(custom.css),但我并不是很痛苦.
i've been trying to load a simple CSS (custom.css) from the frontend and backend of my project and I't has been a real pain.
css位于:
frontend/views/web/css/custom.css
它可以毫无问题地加载到前端...这是位于前端的AppAsset文件:
It loads without problems in the frontend... this is the AppAsset file located in the frontend:
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
/**
* Main frontend application asset bundle.
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/site.css',
'css/custom.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
所以我只是认为位于后端的AppAsset文件应如下所示:
So I just thought that the AppAsset file located in backend should look like this:
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
/**
* Main backend application asset bundle.
*/
class AppAsset extends AssetBundle
{
public $basePath = '@webroot';
public $baseUrl = '@web';
public $css = [
'css/custom.css',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
但是在加载后端索引时,我得到的只是:
But all I get when loading the backend index is this:
http://localhost:8888/backend/web/css/custom.cssFailed to load resource: the server responded with a status of 404 (Not Found)
很抱歉出现noob问题,并在此先感谢您.只想在前端和后端之间共享一个CSS.
Sorry for the noob question and thanks in advanced. Just want to share a css between frontend and backend.
推荐答案
如果要加载js或css共享资源,则YourAsset类中的sourcePath必须为"@ yourSource/web/", 例如,如果您想在common/web/css/目录中的前端加载一个CSS文件,则您的AppAsset或任何其他自定义资产类都位于common/中,必须具有以下变量initialize
If you want to load a share resource i.e js or css , in YourAsset class sourcePath must be "@yourSource/web/", for example if you want to load a css file in frontend which is located in common/web/css/ directory , your AppAsset or any other custom asset class resides in common/ must have following variables initialize
public $ sourcePath ="@ common/web"; public $ basePath ="@ common";
public $sourcePath = "@common/web"; public $basePath ="@common";
注意:"@ ewbroot"路径& $ baseUrl ="@ web"将定位到您当前的访问路径,在这种情况下,它是前端 &将加载它 本地主机/yii-应用程序/前端/css/filename.css" 所以您必须忽略它.
Note : "@ewbroot" path & $baseUrl ="@web" will locate to your current accessing path, in this case it is frontend & will load it "localhost/yii-application/frontend/css/filename.css" so you must omit it.
这篇关于在Yii2 Advanced模板中的前端和后端之间共享资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!