Yii2 combine assets into one css and js - php

I have a site based on the advanced app template, everything works fine except I want to combine all the css and js files into one.
So I read the official documentation in order to combine assets.
The problem:
the bootstrap's js file in unminified and is included separately but the css file is compressed and combined perfectly (with the others)?
how can I combine the assets included with a widget?
Here is my assets.php (which is used with the php yii asset command)
Yii::setAlias('#webroot', __DIR__ . '/frontend/web');
Yii::setAlias('#web', '/');
return [
// Adjust command/callback for JavaScript files compressing:
'jsCompressor' => 'java -jar compiler.jar --js {from} --js_output_file {to}',
// Adjust command/callback for CSS files compressing:
'cssCompressor' => 'yui-compressor --type css {from} -o {to}',
// The list of asset bundles to compress:
'bundles' => [
'yii\web\JqueryAsset',
'yii\bootstrap\BootstrapAsset',
'frontend\assets\AppAsset',
'yii\web\YiiAsset',
'\rmrevin\yii\fontawesome\AssetBundle',
],
// Asset bundle for compression output:
'targets' => [
'all' => [
'class' => 'yii\web\AssetBundle',
'basePath' => '#webroot',
'baseUrl' => '#web',
'js' => 'js/all-{hash}.js',
'css' => 'css/all-{hash}.css',
],
],
// Asset manager configuration:
'assetManager' => [
'basePath' => '#webroot/assets',
'baseUrl' => '#web/assets',
],
];

Related

How to Fix Cache JavaScript in Yii 2

In Yii 2, when I add a few code rows to my JavaScript file, that has a cache, it will not accept my additional code to the JavaScript file.
How can I fix this problem?
You need to add forceCopy option in asset manager under components :
'components' => [
'assetManager' => [
'class' => 'yii\web\AssetManager',
'forceCopy' => true,
],
],
In my case I did it by chanding version in asset file where i include this js:
public $js = [
'js/script.js?0.0.1',
];

yii2 assetBundle - all absolute links instead of relative

I am trying to figure out, how can I configure AssetBundle via /config/main.php in Yii2 . The reason is, that we need to use globally absolute links for all assets (CSS + JS bundles) instead of relative.
We have set absolute #web alias:
Yii::setAlias('#webabs', empty($_SERVER['SERVER_NAME']) ? '/' : '//'.$_SERVER['SERVER_NAME']);
So the only thing we need to change is the property baseUrl in class \yii\web\AssetBundle:
baseUrl = '#webabs'
Following did not work for me:
'assetBundle' => [
'baseUrl' => '#webabs',
],
because "assetBundle" is not core component.
'yii\web\AssetBundle' => [
'class' => 'yii\web\AssetBundle',
'baseUrl' => '#webabs',
],
because Object configurator will not configure property.
So is there any way to configure "baseUrl" property globally in "\yii\web\AssetBundle"?
Thank you.
Try in configuration:
// ...
'components' => [
// ...
'assetManager' => [
'baseUrl' => '#webabs/assets'
],
],

translation view for Yii app not found

I set up an i18n page, where I translate messages using yii\i18n\PhpMessageSource with the following config part:
(config/web.php)
$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'bootstrap' => ['debug'],
'language' => 'de-DE',
'components' => [
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'fileMap' => [
'app' => 'app.php',
],
'forceTranslation' => true,
],
],
]]
...byt the way: this works fine.
For a kind of static content -like an imprint-, I like to use an complete translated view.
So I added some sub-directories in the views - folder, with the view insight:
#app/views/myController/de-DE/myview.php
#app/views/myController/en-US/myview.php
So my action does the following:
public function actionImpressum() {
\Yii::$app->language = 'en-US';
return $this->render('myview');
}
...which results in an invalid parameter
yii\base\InvalidParamException: The view file does not
exist: /path/to/my/app/views/myCtrl/myview.php
This error is valid, because there is no view at this path. But shouldn't the render() method use the path for the translation views, like:
/path/to/my/app/views/myCtrl/en-US/myview.php ??
Is there something I forgot?
Thank you.
Since there is no sourceLanguage set in your configuration I assume you have not changed it and the source language of your app is en-US (default one).
When the source language is the same as target language view is not translated.
See documentation about this:
Note: If the target language is the same as source language original view will be rendered regardless of presence of translated view.
So for en-US it looks for /path/to/my/app/views/myCtrl/myview.php file.

Yii2 Kartik Grid View Export Config

Summary: Trying to add export in the for csv & pdf download.
Followed documantation to install. Grid view is otherwise working.
Also added as module in config/web.php's $config array -
'modules' => [
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
//'class' => 'yii\i18n\PhpMessageSource',
//'basePath' => '#kvgrid/messages',
//'forceTranslation' => false
]
]
],
N.B: I am using basic template and new in yii2. I have tried other fixes like composer update etc as suggested in various posts but really stuck with the problem.
the thing causing problem is - Yii::t('kvgrid', 'Reset Grid')
Can somene give me a direction here. I guess it is very simple issue :(
You was got above error because of you have not configure i18n component in web.php file.
You need to configure i18n component like as,
'i18n' => [
'translations' => [
'kvgrid*' => [
'class' => 'yii\i18n\PhpMessageSource',
],
]
],
Or uncomment the gridview modules's i18n configuration
'gridview' => [
'class' => '\kartik\grid\Module',
// enter optional module parameters below - only if you need to
// use your own export download action or custom translation
// message source
'downloadAction' => 'gridview/export/download',
'i18n' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#kvgrid/messages',
'forceTranslation' => true
]
]
You have to replace
Yii::t('kvgrid', 'Reset Grid')
to
Yii::t('app', 'Reset Grid')
in your view file
I suggest better to generate CRUD using the Ajax Crud Generator, it will do all the required task for CRUD and export as well...try this

Yii2 Asset Convertor

I'm trying to use Less with my Yii2 application.
I use the advanced application and would like to convert my .less files in the frontend/web/css using the asset convertor build in with yii2.
'assetManager' => [
'bundles' => [
'yii\bootstrap\BootstrapAsset' => [
'css' => []
],
],
'converter' => [
'class' => 'yii\web\AssetConverter',
'commands' => [
'less' => ['css', 'lessc {from} {to} --no-color'],
],
],
],
The above is in my main.php config file.
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'css/site.less',
'css/superhero.less',
];
public $js = [
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
The above is the appAsset file.
but how can I install the less tool in my yii2 installation? I put less-1.7.5.js in the root folder where my yii console bootstrap file is but where do I have to adjust the configuration to convert the less files?
Thx in advance!
What I've just did in my project:
main.php config file:
'assetManager' => [
'converter' => [
'class' => 'yii\web\AssetConverter',
'commands' => [
'less' => ['css', 'nodejs "' . PROTECTED_BASE_PATH .
'/node_modules/less/bin/lessc" {from} {to} --no-color'],
],
],
],
Command line (in the subdirectory, represented by PROTECTED_BASE_PATH above):
$ npm install less
That's all. It works fine after git pull on another machine.
Of course, nodejs itself must be installed globally.
Important note. This is unusual way to keep the nodejs packges in VCS (though I have a reason to do so). Consider use of package.json instead.
To install less compiler (lessc) to your project use composer command:
composer require bower-asset/less
Then add to config:
'assetManager' => [
'converter' => [
'class' => 'yii\web\AssetConverter',
'commands' => [
'less' => ['css', '#bower/less/packages/less/bin/lessc {from} {to} --no-color'],
],
],
],
This config is helpful for shared hosting, when you have no root privileges and can't install lessc globally.
install less converter: Server-Side and Command Line Usage
http://lesscss.org/usage/
and make sure command available as lessc from anywhere i.e. added to PATH

Categories