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'
],
],
Related
I want to set prefix for all keys of urlManager rules. At the moment I write rules like that:
'rules' => [
'api' => '_public/site/index',
'api/<controller>/<action>' => '_public/<controller>/<action>',
etc
]
I want to avoid copy/past-ing of api prefix to all rules. Because it could be 500 rules.
Is there a way to define somewhere a prefix in order to Yii placed it for every rule key itself?
$prefix and $prefixRoute is setting up prefix for values, but I want for keys.
Maybe this can be done by using .htaccess? But I don't know what should I write there.
The yii\web\GroupUrlRule and its $prefix property is exactly what you are looking for.
In your case:
rules => [
// ...
new \yii\web\GroupUrlRule([
'prefix' => 'api',
'rules' => [
'<controller>/<action>' => '_public/<controller>/<action>',
// ... other /api/_ routes
],
]),
// ... other rules
]
I'm not sure if adding rule like this '/' => '_public/site/index', into the group would work for /api route or if you would have to set that one explicitly outside of group rule. If you are going to set rule for /api outside of group rule it might be better to put it before the group rule.
The GroupUrlRule with properties: prefix, routePrefix and rules will be enough. You can use it as follows:
new GroupUrlRule([
'prefix' => 'api',
'routePrefix' => '',
'rules' => [
'' => '_public/site/index',
'<controller>/<action>' => '_public/<controller>/<action>',
]
])
Add this to the rules under urlManager in the main config file.
I added a line to config file:
$config = [
'components' => [
'request' => [
// some code
'baseUrl' => '/api', // <--- this line
],
]
]
and it worked.
$baseUrl - The relative URL for the application. https://www.yiiframework.com/doc/api/2.0/yii-web-request#$baseUrl-detail
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',
];
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',
],
];
I use yii-jui to add some UI elements in the views such as datePicker. In the frontend\config\main-local.php I set the following to change the theme used by the JqueryUI:
$config = [
'components' => [
'request' => [
// !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
'cookieValidationKey' => 'gjhgjhghjg87hjh8878878',
],
'assetManager' => [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/flick/jquery-ui.css'],
],
],
],
],
];
I tried the following to override this configuration item in the controller actions method:
public function actions() {
Yii::$app->components['assetManager'] = [
'bundles' => [
'yii\jui\JuiAsset' => [
'css' =>
['themes/dot-luv/jquery-ui.css'],
],
],
];
return parent::actions();
}
Also I tried to set the value of Yii::$app->components['assetManager'] shown above to the view itself (it is partial view of form _form.php) and to the action that calls this view (updateAction). However, all this trying doesn't be succeeded to change the theme. Is there in Yii2 a method like that found in CakePHP such as Configure::write($key, $value);?
You should modify Yii::$app->assetManager->bundles (Yii::$app->assetManager is an object, not an array), e.g.
Yii::$app->assetManager->bundles = [
'yii\jui\JuiAsset' => [
'css' => ['themes/dot-luv/jquery-ui.css'],
],
];
Or if you want to keep other bundles config :
Yii::$app->assetManager->bundles['yii\jui\JuiAsset'] = [
'css' => ['themes/dot-luv/jquery-ui.css'],
];
You are going about this all wrong, you want to change the JUI theme for 1 controller alone because of a few controls. You are applying 2 css files to different parts of the website that have the potential to change styles in the layouts too. The solution you found works but it is incredibly bad practice.
If you want to change just some controls do it the proper way by using JUI scopes.
Here are some links that will help you:
http://www.filamentgroup.com/lab/using-multiple-jquery-ui-themes-on-a-single-page.html
http://jqueryui.com/download/
In this way you are making the website easier to maintain and you do not create a bigger problem for the future than you what solve.
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