Difference between #3 and #2 of Load the Yii-Bootstrap Extension on Specific Actions

unchanged
Title
Load the Yii-Bootstrap Extension on Specific Actions
unchanged
Category
How-tos
unchanged
Tags
bootstrap, filter
changed
Content
A big problem I've hit with the [Yii-Bootstrap
extension](http://www.yiiframework.com/extension/bootstrap
"Yii-Bootstrap") is that all AJAX requests are initializing Bootstrap
because of preload. This is a huge waste of resources, especially when using
AJAX-based file uploaders that split the file into chunks. Large file uploads
using that method could be initializing bootstrap hundreds of times.

I ended up disabling preload and switched the loading of bootstrap to a filter.

Disable bootstrap preload: /protect/config/main.php
~~~
[php]
'preload'=>array(
   //'bootstrap',
   'log'
),
~~~

Create file: /protected/extensions/bootstrap/filters/BootstrapFilter.php

~~~
[php]
<?php
class BootstrapFilter extends CFilter
{
    protected function preFilter()
    {
        Yii::app()->getComponent("bootstrap");
        return true;
    }
}
~~~

Then in a controller, add the new bootstrap filter:

~~~
[php]
public function filters()
{
    return array(
        'accessControl',
        'postOnly + delete',
        array('ext.bootstrap.filters.BootstrapFilter - delete')
    );
}

~~~

This will load bootstrap for all actions in the controller, other than the
"delete" action. By default the "delete" action does
not render a view, which allows us to safely skip bootstrap loading on
that action. If you wanted to disable loading bootstrap for another
specific action, do the following:

~~~
[php]
array('ext.bootstrap.filters.BootstrapFilter - delete, uploadajax')
~~~

**Make sure you add this filter to all site controllers that require bootstrap
(this includes the Site controller to display errors)**

This functionality could easily be added to the bootstrap Gii CRUD generation,
and is a far better way to initialize this extension in my opinion.