I was given the task to put the application on YII, i.e. I got its code and I have to put it on the server. Unfortunately, I have a problem, I threw the application on the server, I started the composer, I changed the data to the base, and after entering the website I get the following message:
Class 'app\assets\AppAsset' not found
I checked this and in the assets directory is the AppAsset class which has the code:
<?Php
namespace app\assets;
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'Css/app.css'
];
public $js = [
'Js/app.js'
];
public $depends = [
'Yii\web\YiiAsset'
'Yii\bootstrap\BootstrapAsset'
];
}
?>
I am looking for a solution to this problem all the time, but nothing works.
Related
I want to include the path of css/js files in service-worker for caching.
what I see Yii deployes css/js files to a dynamic directory using hash in AppAssets.
for example
/assets/a7fd2538/css/site.css
so is there a way I can disable this hashed directory a7fd2538.
and I get just
/assets/css/site.css
Thanks.
You can put your asset files directly to some accessible directory. For example in web/css/ and web/js/. Then you will set your AppAssets bundle to use them directly without copying by specifing $basePath and $baseUrl and leaving $sourcePath empty.
For example:
namespace app\assets;
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.css',
];
public $js = [
'js/site.js',
];
// ... other definitions
}
FileInput works fine with yii2 default theme.
But after change to AdminLTE Theme, almost Kartik-v widget does not work.
Anyone know problem and solution ?
namespace app\assets;
use yii\web\AssetBundle;
class AdminLTEAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'themes/AdminLTE/bootstrap/css/bootstrap.min.css',
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css",
"https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css",
'themes/AdminLTE/dist/css/AdminLTE.min.css',
'themes/AdminLTE/dist/css/skins/_all-skins.min.css',
'themes/AdminLTE/plugins/iCheck/flat/blue.css',
];
public $js = [
'themes/AdminLTE/plugins/jQuery/jquery-2.2.3.min.js',
'themes/AdminLTE/plugins/bootstrap-wysihtml5/bootstrap3-wysihtml5.all.min.js',
"https://code.jquery.com/ui/1.11.4/jquery-ui.min.js",
'themes/AdminLTE/bootstrap/js/bootstrap.min.js',
"https://cdnjs.cloudflare.com/ajax/libs/raphael/2.1.0/raphael-min.js",
'themes/AdminLTE/plugins/sparkline/jquery.sparkline.min.js',
'themes/AdminLTE/plugins/slimScroll/jquery.slimscroll.min.js',
'themes/AdminLTE/plugins/fastclick/fastclick.js',
'themes/AdminLTE/dist/js/app.min.js',
'themes/AdminLTE/dist/js/pages/dashboard.js',
'themes/AdminLTE/dist/js/demo.js',
];
public $depends = [
'yii\web\YiiAsset',
];
}
I solved this by removing this line:
'themes/AdminLTE/plugins/jQuery/jquery-2.2.3.min.js',
I have RTL layout for my yii2 application using airani\bootstrap\BootstrapRtlAsset. However, I want to customize the Bootstrap using this online tool.
The tool generates a css file which I want it to be linked directly before bootstrap-rtl.css comes from BootstrapRtlAsset.
The only way, that I know, to do that, is creating new AssetBundle in app/assets like the following:
<?php
namespace app\assets;
use yii\web\AssetBundle;
class ThemeAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/theme.min.css',
];
public $js = [
];
}
Then I should add it in the $depends of AppAsset:
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
'app\assets\ThemeAsset',
'airani\bootstrap\BootstrapRtlAsset',
];
So is there any other way that allows me to replace app\assets\ThemeAsset with just the path of the css file css/theme.min.css without need to create new AssetBundle?
Use registerCssFile() Concept
http://www.yiiframework.com/doc-2.0/yii-web-view.html#registerCssFile%28%29-detail
You just need to modify the file: frontend/assets/AppAsset.php, add one line.
public $css = [
'css/theme.min.css',
... Your other 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.
The css is located in:
frontend/views/web/css/custom.css
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',
];
}
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)
Sorry for the noob question and thanks in advanced. Just want to share a css between frontend and backend.
One direct way to do this is to have a common web accessible folder under app root. Something like /assets, which you can access from both BackEnd and FrontEnd. You would need to edit your .htaccess to allow this as well.
Yii2 will publish assets only if $sourcePath is set, and $basePath and $baseUrl are not set(!)
Therefore:
use yii\web\AssetBundle;
class AppAsset extends AssetBundle
{
public $sourcePath = '#app/assets/app';
public $css = [
'css/openbook.css',
'fontello/css/fontello.css',
'fontello/css/animation.css'
];
public $js = [
'js/plug.openbook.js',
'js/plug.interpret.js',
'js/plug.drop.message.js'
];
public $depends = [
// 'yii\web\YiiAsset',
// 'yii\bootstrap\BootstrapAsset',
];
}
in the main layout:
use frontend\assets\AppAsset;
...
AppAsset::register($this);
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";
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.
you can register the file on any view or layout itself
<?php $this->registerCssFile('http://domain.com/frontend/web/path/file.css');?>
or
you can also add the full path in asset bundle
public $css = [
'http://domain.com/frontend/web/path/file.css',
];
I have a module with two views, I need that each view have its own assets files, because of this I wrote a AssetBundle called DashboardAsset for my dashboard view, this is the code.
class DashboardAsset extends AssetBundle
{
public $sourcePath ='#asketchFront/assets/resources/';
public $basePath = '#webroot';
public $baseUrl = '#web';
public $js =
[
"js/src/dashboard.js"
];
public $css =
[
"css/dashboard.css",
'font-awesome/css/font-awesome.min.css',
];
}
Then in my dashboard view I put the following code line:
DashboardAsset::register($this);
afertward when I want to see my page, the following error messages are displayed in the console
GET http://localhost/js/src/dashboard.js localhost/:478
GET http://localhost/css/dashboard.css localhost/:19
GET http://localhost/font-awesome/css/font-awesome.min.css
Why this happens??
I solved the problem removing the basepath and the baseurl attributes