Where should I put my custom widget files in Yii framework?
Asked Answered
D

2

8

From this page,

http://www.yiiframework.com/wiki/23/how-to-create-a-breadcrumb-widget/

It seems it suggests that we should put the files in the component folder. But if my widget contains javascript and css files, where should these files be placed?

By the way, is this a good idea that I create it as an extension? If I go this way, all widget files are more self-contained in a folder inside the extension folder. But since the widget I am going to work on is very customized, it's unlikely that it will be useful to other people or my other projects. Making it an extension seems a little bit strange.

I understand that it does not really matter where I put these files as long as the paths I am using in the codes are correct but I would like to know the common practice.

Detwiler answered 30/12, 2011 at 11:25 Comment(0)
B
11

I think the common practice is to put the widget in extensions folder with js & css files in an folder named asset. In the php class file, you do initialization first by publishing the asset with yii asset manager. The file structure may be like

extensions/
   widget_name/
     widget.class.php
     assets/
         plugin.js
         style.css
Bellow answered 30/12, 2011 at 11:44 Comment(2)
can you please explain how to publish these files and use these files in views file.Latoria
Extension folder is not seen when I enter use app\...\WidgetName.php. How can it be added there?Lyris
R
8

I would join the recommendation to put the widget under /protected/extensions. I put the assets in a slightly more detailed manner: /protected/extensions/WidgetClassName/assets/ and the widget view files in /protected/extensions/WidgetClassName/views/...

Don't forget to edit your /protected/config/main.php and add a row in the 'import' section (for autoloading of the widget): 'ext.WidgetClassName.WidgetClassName.*'

Rawalpindi answered 30/12, 2011 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.