Widget Shortcut

Hi all :)

I wrote my first small widget and call it with following code in my views:

<?php $this->widget("application.extensions.widgets.CountryDropdown.CountryDropdown"); ?>

More are to come and they will all be under protected/extensions/widgets/{FOLDERNAME}/{CLASSNAME}.

Where {FOLDERNAME} is always the same as {CLASSNAME}.

Is there a way to shortcut the class name/path like

<?php $this->widget("widgets.{FOLDERNAME}"); ?>

so that Yii will go and search for the widgets directory in protected/extensions and load the class with the same name (as the folder) from the folder?

Can/should I extend the createWidget() method of CBaseController? And where would I put the extendes CBaseController?

Any help/idea is much appreciated.





so the "widget" alias will point to the path of the alias "application.extensions.widgets"

also then you can use setPathOfAlias to reduce your widgets path the same way

you can set it in configs/main.php

Thank you Gustavo. setPathOfAlias work’s great for the application.extensions.widgets path.

I am still looking for a perfect solution to shorten CountryDropdown.CountryDropdown. The way you mention with setPathOfAlias is great, but this forces me to set the path for every widget manually in the config.

In case someone with the same problem will find this thread:

Yii::getPathOfAlias (and other methods like Yii::app()->basePath) don’t work in the config files, because it seems the Yii class is not yet instantiated(?).

But following code does work (in main.php):

Yii::setPathOfAlias('widgets', 'protected/extensions/widgets');

have the same issue over here. As my widgets have multiple view files, I want to keep these in a separate map. But with the current widget configuration, you have to put in the name of the widget twice. No autoloading of the central widget file. Any ideas?

to set and use an alias




To add some path to the autoload do something like

Yii::import('widgets');//this refers to the path of alias 'widget' defined above