Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the proper way to work with assets in Yii?

I notice that Yii creates strange set of directories (names like 8523d23 or 10s89b92) in assets directory, and this even happens at runtime. For example, one of my tables got more than 10 records, pagination kicked-in and I got a new files in assets subdirectory named pager.css.

When I move my site from testing to production, should I copy all those, or just create an empty "assets" directory, and it will be filled at runtime?

If I want to add, for example, some new jQuery plugin, how should I proceed?

For example, I wish to add jquery.charcounter.js, do I copy it to assets or to yii/framework/web/js/source? If I do the latter, how do I get this .js file included in HTML page output?

like image 639
Milan Babuškov Avatar asked Nov 05 '09 08:11

Milan Babuškov


People also ask

How does Yii Framework work?

Yii framework uses “Gii” tool, which is a web-based code scaffolding tool that is used to create code quickly. Using this, we can create templates in models, controllers, forums, modules, extensions, CRUD controlled actions, and views.

What is difference between Yii and Yii2?

Conceptually Yii1 and Yii2 are quite similar, however Yii2 runs on newer PHP versions and utilizes namespaces, traits etc. Yii2 has also support for dependency injection through $container singleton available after initializing the framework.

Why Yii is the best framework?

It helps in reducing the response time and improves the speed and performance of the web application. Yii allows easy integration of a cache application component.

What is $App in Yii?

Applications are objects that govern the overall structure and lifecycle of Yii application systems. Each Yii application system contains a single application object which is created in the entry script and is globally accessible through the expression \Yii::$app .


2 Answers

assets should be a writable directory. Yii takes care of assets.

By calling Yii::app()->assetManager->publish() some stylesheets, images, scripts, or even entire directories can be put into a web-visible folder.

pager.css and other non-familiar files are produced by widgets (CLinkPager for example) and other components (such as CClientScript publishes jQuery whenever you need that).

During deployment, this folder should be empty, but it doesn't really matter.

Adding plugins should never be done through framework folders. You can place them either in components dir and publish it runtime if necessary, or into any other convenient visible directory (like as images or css).

Update

To embed jquery.charcounter.js, put it in components directory, then call

Yii::app()->clientScript->registerScriptFile(
    Yii::app()->assetManager->publish(
        Yii::getPathOfAlias('application.components').'/jquery.charcounter.js'
    ),
    CClientScript::POS_END
);

Regarding weird folder names, I firmly believe they are unique hashes (or part of), so they can be differentiated if application uses several extensions.

like image 164
pestaa Avatar answered Oct 01 '22 00:10

pestaa


This would resolve the query as this provides detailed explanation for the assets folder:

http://www.yiiframework.com/wiki/148/understanding-assets/

like image 43
Kshitij Avatar answered Oct 01 '22 00:10

Kshitij