Load the Yii-Bootstrap Extension on Specific Actions

You are viewing revision #3 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.

« previous (#2)

A big problem I've hit with the Yii-Bootstrap extension 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

'preload'=>array(
   //'bootstrap',
   'log'
),

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

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

Then in a controller, add the new bootstrap filter:

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:

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.

7 0
15 followers
Viewed: 27 979 times
Version: Unknown (update)
Category: How-tos
Written by: chuntley
Last updated by: chuntley
Created on: Dec 13, 2012
Last updated: 12 years ago
Update Article

Revisions

View all history

Related Articles