I'm developing an Yii2 extension that uses an AssetBundle for some js/css files:
namespace myExtension/assets;
class ResourcesAsset extends AssetBundle {
public $sourcePath = '#app/assets/resources';
public $css = ['resources.css'];
public $js = ['resources.js'];
public $depends = ['yii\bootstrap\BootstrapPluginAsset'];
}
Structure in extension:
my-extension
|-assets
| |-resources
| |-resources.css
| |-resources.js
| ResourceAsset.php
|-controllers
|-models
|-views
|-Module.php
This doesn't seem to work when I install the extension in another project because of the $sourcePath. There the alias #app points to the application base path, not to the extension root folder in vendor.
Currently, I use #myExtension/assets/resources, which works as I know it gets that alias in extionsions.php when it gets installed. But I think this is not optimal. So what is the optimal way to declare $sourcePath? Should I create an own alias (in Module.php?)? Should I use __DIR__?
The alias you are using (#myExtension) is the way to go.
From the Guide:
When the extension is installed in an application, Yii will create for each listed root namespace an alias that refers to the directory corresponding to the namespace.
Alternatively you can use alias for vendor folder so something like:
public $sourcePath = '#vendor/my-extension/assets/resources';
Related
I have successfully installed npm-asset/socket.io package and its dependencies by using Asset Packagist in yii2. Now, I cannot include it in my pages. I tried in AppAsset.php like this:
public $js = ['#npm/socket.io/client-dist/socket.io.js'];
it did not work:
GET http://project.localhost/#npm/socket.io/client-dist/socket.io.js
net::ERR_ABORTED 404 (Not Found)
Then, I tried to include that file inside view file like this:
<script src="<?php echo Yii::getAlias('#npm').'/socket.io/client-dist/socket.io.js' ?>"></script>
it gave me this error :
Not allowed to load local resource:
file:///C:/Projects_folder/Php/Yii2/project/vendor/npm-asset/socket.io/client-dist/socket.io.js
I need help with how to use this js file inside view files.
The sources installed by composer are placed in vendor folder which is not directly accessible. You need to publish the assets and then include the published resources.
To do that you can make an asset bundle for socket.io for example like this
namespace app\assets;
use yii\web\AssetBundle;
class SocketIOAsset extends AssetBundle
{
//this is path where source files that needs publishing are located
public $sourcePath = "#npm/socket.io/client-dist";
public $css = [
];
public $js = [
'socket.io.js'
];
}
Then in your AppAsset bundle add the SocketIoAsset into $depends property
class AppAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $depends = [
SocketIOAsset::class,
// ... other dependencies ...
];
// ... other definitions ...
}
Now because your AppAsset depends on SocketIOAsset, the folder defined in SocketIOAsset::$sourcePath will be published when AppAsset is registered, and files in SocketIOAsset::$js array will be linked.
I am working on a requirement of where I have to include all common methods like pagination, etc. which were used in my views into all my views. For this purpose I thought helper file is useful and created helper file in common\helpers\ directory with name Common as helper file name. I am facing difficulty in using this helper file in my view file.
I have included this helper file in my view as
use common\helpers\Common;
When I open the page I am getting error as "Class 'common\helpers\Common' not found"
My helper file: Common.php
namespace common\helpers;
class Common
{
protected $_file;
protected $_data = array();
public function __construct($file)
{
$this->_file = $file;
}
public static function getCommonHtml($id=NULL)
{
----
----
}
-----
--- Some other methods---
-----
}
I googled for this & got few solutions but they never worked.
You need to declare your new namespace in your composer.json:
"autoload": {
"psr-4": {
...
"common\\": "common/"
}
},
And the run:
composer dump-autoload
Alternatively you could declare alias for new namespace, so Yii autoloader will handle it (like in advanced template):
Yii::setAlias('#common', dirname(__DIR__))
But Yii autoloader will be dropped in Yii 2.1, so I would stick to composer-way (or do both - alias may be useful not only for autoloading).
I have added fullcalendar js/css in vendor/bower folder. I want to add this into just one page.
I read abt AssestBundle on this link - http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
But this add in all the pages.
In Yii 2 framework asset bundles is recommended way of working with js / css. It's not limited to just adding to all pages. You can use it only in specific view.
Example of asset bundle for JsTree plugin:
<?php
namespace backend\assets;
use yii\web\AssetBundle;
class JsTreeAsset extends AssetBundle
{
public $sourcePath = '#bower_components/jstree/dist';
public $js = [
'jstree.min.js',
];
public $css = [
'themes/default/style.min.css',
];
public $depends = [
'yii\web\JqueryAsset',
];
}
In this example, #bower_components alias is used, in order to get it working you also need to register it in application bootstrap file (in advanced application template this file is common/config/bootstrap.php):
Yii::setAlias('bower_components', dirname(dirname(__DIR__)) . '/bower_components');
Then, in view where you need to use it, call register() method of this asset bundle and pass current view:
use backend\assets\JsTreeAsset;
...
JsTreeAsset::register($this);
The files in default asset bundle (AppAsset) which included in application templates are loaded in every view because it's registered in application layout and layout is applied to all views.
I have a module named social with the directories like this :
frontend
-- modules
-- social
-- assets
-- SocialAsset.php
-- controllers
-- views
-- default
-- layouts
-- main.php
-- web
-- css
-- js
-- img
Module.php
I want my module have an own layout. Because of that, i add file SocialAsset, main.php for layout, css, js and img. But, unfortunatelly i can't access my css/js/img files.
SocialAsset file :
<?php
namespace frontend\modules\social\assets;
use yii\web\AssetBundle;
class SocialAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $sourcePath = '#app/modules/social/web';
public $css = [
'css/social.css',
'css/toolkit.css'
];
public $js = [
'js/jquery.min.js',
'js/social.js',
'js/toolkit.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
in main.php i use the SocialAssetfiles like this :
use frontend\modules\social\assets\SocialAsset;
SocialAsset::register($this);
Could someone to help me to solve this problem ?
You should only set sourcePath in your asset bundle :
sourcePath specifies the root directory that contains the asset
files in this bundle. This property should be set if the root
directory is not Web accessible. Otherwise, you should set the
basePath property and baseUrl, instead.
This means you should simply remove $basePath and $baseUrl in your assset bundle, Yii will then publish your files.
Read more : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
I want to include my js script specific.js for a specific view having action id specific-action-id. I don't want the specific.js to be included in every page.
For including a js script for the whole site I normally have to edit AppAsset.php. For this purpose, what should I do?
You should simply register this js file in your view, e.g. :
$this->registerJsFile('#web/js/specific.js');
Read more : http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html#registering-scripts
It is highly recommended to use asset bundles to register external JS files rather than registerJsFile() because these allow better flexibility and more granular dependency configuration. Also using asset bundles allows you to combine and compress multiple JS files, which is desirable for high traffic websites.
So you can create a new AppAsset file and put your css and javascript files in it.
Create the Asset Bundle
In the \assets directory, we create StatusAsset.php:
<?php
/**
* #link http://www.yiiframework.com/
* #copyright Copyright (c) 2008 Yii Software LLC
* #license http://www.yiiframework.com/license/
*/
namespace app\assets;
use yii\web\AssetBundle;
/**
* #author Qiang Xue <qiang.xue#gmail.com>
* #since 2.0
*/
class StatusAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [];
public $js = [
'/js/jquery.simplyCountable.js',
'/js/twitter-text.js',
'/js/twitter_count.js',
'/js/status-counter.js',
];
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap\BootstrapAsset',
];
}
In your view file
use app\assets\StatusAsset;
StatusAsset::register($this);
Resources
http://www.yiiframework.com/doc-2.0/guide-output-client-scripts.html
https://code.tutsplus.com/tutorials/how-to-program-with-yii2-working-with-asset-bundles--cms-23226
#soju is right. But you also can create specific asset bundles for use specific views. Your specific js files can be a file group on this situation you can use asset bundles and can use any views which you want.