Yii 1.1: Load the Yii-Bootstrap Extension on Specific Actions

15 followers

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.

Total 5 comments

#11708 report it
chuntley at 2013/01/28 04:51pm
YiiBooster

It seems that YiiBooster has implemented this into their code, however it is currently an undocumented feature. For YiiBooster you just have to disable preloading then call the filter in a controller, no extra steps required.

#11384 report it
bonnie at 2013/01/10 08:25am
Very nice

I had this problem and was looking for work around. This seem to solve my problem. Thanks

#11271 report it
PrplHaz4 at 2013/01/02 02:41pm
Best method for using with modules?

Thanks for posting...this helped me figure out why my autocomplete fields were loading VERY slowly.

It seems as if the provided solution works best when all controllers reside in the yii app itself, and Joblo's solution is better when using extensions/modules that define their own controllers/views (where you don't want to change the module code directly).

Is there any solution that would work well for both use cases? It seems like bootstrap shouldn't be loaded for any request that does not render a view (or within a layout)?

#11204 report it
Joblo at 2012/12/27 06:42am
Bootstrap scripts

I did like below.

  1. Configure the 'bootstrap' component in config/main.php with all properties registering clientscripts set to false. So only Yii::setPathOfAlias('bootstrap',...) is executed on Bootstrap::init();

  2. I only register the bootstrap clientscripts when needed in a layout (main, backend, frontend...). At the top of the layout I call a method like this (implemented in the BaseController or Module):

public function registerBootstrapScripts()
    {
        $bootstrap = Yii::app()->getComponent('bootstrap');
        $bootstrap->responsiveCss = true;
        $bootstrap->jqueryCss = true;
        $bootstrap->coreCss = true;
        $bootstrap->yiiCss = true;
        $bootstrap->enableJS = true;
 
        $bootstrap->init();
    }

This has the advantage, that I can use different css-frameworks in the frontend and backend.

#11166 report it
Antonio Ramirez at 2012/12/21 02:29am
Thank you

For this wiki and the PR on github... will also modify the asset registration as it is another challenge people facing when using the library the first time.

Leave a comment

Please to leave your comment.

Write new article
  • Written by: chuntley
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +10
  • Viewed: 19,386 times
  • Created on: Dec 13, 2012
  • Last updated: Dec 17, 2012
  • Tags: bootstrap, filter