Yii Bootstrap does not work if not preloaded
Asked Answered
S

3

7

I have been using Yii Bootstrap in an application for a while. Now, I have a section where the loaded CSS files are causing issues, and I don't want to load the bootstrap extension in that controller.

In my configuration file, I had bootstrap set to preload:

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

I have now removed bootstrap from the preload array, and it stops working saying the alias is incorrect:

Alias "bootstrap.widgets.BootNavbar" is invalid. Make sure it points to an existing directory or file.

The alias though has been defined in the components section of the configuration file and works fine when the bootstrap component is preloaded:

'bootstrap' => array(
    'class' => 'ext.bootstrap.components.Bootstrap'
 ),

What can I do to make the bootstrap extension work without preloading it?

Spruik answered 23/7, 2012 at 12:7 Comment(8)
What is the value of the alias named bootstrap.widgets.BootNavbar? Please add it to your question.Boatload
C:\Program Files\XAMPP\xampp\htdocs\myproject\protected\extensions\bootstrap\widgets\BootNavbarSpruik
When bootstrap is removed from the preload array, the getPathOfAlias returns falseSpruik
I'd say there is a dependency between the main module your remove and the submodule that is not removed. Unless you do not resolve the dependency issue, Yii will give that error. But sorry I'm not that fluent with Yii that I can spot the dependency so that I can give a concrete suggestion how to resolve the dependency problem.Boatload
Yii would auto import components when they're initialized. But for some reason, the alias is not being defined until the component is preloaded. I am not too familiar with Yii myself, so it's a little confusing how this works.Spruik
Okay, you might be able to get some grip on your issue by using a step-debugger like Xdebug. Install it on your development environment and then step through before that error is thrown. You then can inspect the issue much better.Boatload
I use the component myself and I simply added it as an application component (which you did as well or you wouldn't be able to preload it). I just call Yii::app()->bootstrap->init() at the point where I need it :)Acquiescent
@Blizz: That worked. I created a component extending the CController class and applied this to the constructor.Spruik
P
13

Just call this:

Yii::app()->getComponent("bootstrap");

'preload' => 'bootstrap' actually trigers this command. If you just call Yii::app()->bootstrap->init() as suggested by Blizz you will end up calling init() twice, which could be harmful.

Proctology answered 22/9, 2012 at 20:37 Comment(0)
S
4

I create the bootstrap extension in module init() method on this way. Preload is not needed.

 if (!Yii::app()->hasComponent('bootstrap')) {
            Yii::setPathOfAlias('bootstrap', realpath(dirname(__FILE__) . '/extensions/bootstrap'));
            Yii::createComponent('bootstrap.components.Bootstrap')->init();
        }

If you would like to manage Bootstrap component under Yii::app() (e.g. hasComponent method) you can change the last row:

Yii::app()->setComponent('bootstrap',Yii::createComponent('bootstrap.components.Bootstrap'));

This solution not need preload, or init(), but you have to set alias to Boostrap folder manually.

Smoker answered 9/11, 2012 at 7:25 Comment(0)
M
2

You can anytime import it:

Yii::import('ext.bootstrap.components.Bootstrap');
Melmon answered 23/7, 2012 at 15:49 Comment(1)
Where exactly do I put this? I tried putting it over the controller classes and it's still not working?Spruik

© 2022 - 2024 — McMap. All rights reserved.