How to use Widget when not ->run()

I know I can create a file that do the following without actually do the run() to get Yii framework loaded and able to use the framework features but I can’t figure out how to use Widget, extension, Portlet and module if I don’t do the run() as below

<?php

$yii=dirname(FILE).’/../../../yiiroot118/framework/yii.php’;

$config=dirname(FILE).’/protected/config/main.php’;

defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);

defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’,5);

require_once($yii);

//Yii::createWebApplication($config)->run(); // skip the default run

$app=Yii::createWebApplication($config);

?>

<h1>Welcome to <i><?php echo CHtml::encode($app->name); ?></i></h1>

Any suggestion would be appreciated.

Thanks

Yii was designed as a full-stack framework. It means that you’re likely to run into a lot of trouble when trying to use its components outside of the scope of a CApplication, and this kind of usage isn’t recommended at all. Would you post some more details about your actual use case?

You can use Yii without calling the run() method if you want to use part’s of the features of Yii together with other PHP code.

See: Using Yii in 3rd-Party Systems

If you do so, the method CWebapplication.processRequest() is not called and no Controller will be created and no Action will be executed.

For example


Yii::app()->getController()

in this case will return NULL.

But you can create a wiget and call the run method like this:




  $breadCrumbs = new CBreadCrumbs();

  $breadCrumbs->links = array(...);

  $breadCrumbs->run(); //render the breadcrumbs 


  //use a widget instance to create another widget (instead of controller->widget) 

  $breadCrumbs -> widget(...)    



But you have to be aware that the controller property of the widgets … will always return null.

So you have to check if the extensions / widgets you need will work without controllers or you have to create controllers manually: Yii::app()->createController()

I tried to integrate Yii into my existing site gradually by converting piece by piece with Yii API. Thanks

This is very usefully and I am aware of the createController but can’t seem to figure out how to change the default path to something else outside of protected folder.

Can you provide an example of how to use createController with a controller outside of protected folder?

Thanks

"we can use the database features such as DAO and ActiveRecord; we can use the model and validation feature; and so on."

I am looking for more specific detail from the above statement especially the controller, extension, widget, portlet, modules. Some sample code would be really helpful. Thanks

If the entire site is going to be converted then I’d try to do that page-by-page rather than component-by-component, i.e. when I’m ready with a new controller and corresponding models/views I’d remove old code and use .htaccess to rewrite associated URLs to the entry script of Yii application.

Here is the test code that I was trying to do and the “$ctl” return something but the “widget” part didn’t cause any error but produce nothing!

Is this doable?

Thanks

<?php

$yii=dirname(FILE).’/../../../yiiroot118/framework/yii.php’;

$config=dirname(FILE).’/protected/config/main.php’;

defined(‘YII_DEBUG’) or define(‘YII_DEBUG’,true);

defined(‘YII_TRACE_LEVEL’) or define(‘YII_TRACE_LEVEL’,5);

require_once($yii);

//Yii::createWebApplication($config)->run();

$app=Yii::createWebApplication($config);

$ctl = $app->createController("/");

print_r($ctl);

?>

<h1>Welcome to <i><?php echo CHtml::encode($app->name); ?></i></h1>

<?php $form=$ctl->beginWidget(‘CActiveForm’); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;


&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;


&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'body'); ?&gt;


	&lt;?php echo &#036;form-&gt;textArea(&#036;model,'body',array('rows'=&gt;6, 'cols'=&gt;50)); ?&gt;


&lt;/div&gt;


&lt;?php if(extension_loaded('gd')): ?&gt;


&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'verifyCode'); ?&gt;


	&lt;div&gt;


	&lt;?php &#036;ctl-&gt;widget('CCaptcha'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'verifyCode'); ?&gt;


	&lt;/div&gt;


&lt;/div&gt;


&lt;?php endif; ?&gt;


&lt;div class=&quot;row submit&quot;&gt;


	&lt;?php echo CHtml::submitButton('Submit'); ?&gt;


&lt;/div&gt;

<?php $ctl->endWidget(); ?>

IMO, you are doing this the wrong way.

Try by replacing your application (main loop) with Yii, and then gradually convert your existing code to use Yii MVC.

The other way around is an exercise in … futility?

I do when I want to develop in Yii but in this case I have a different need that run outside of Yii but still take advantage of Yii as much as possible.