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?
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.
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.
You can anytime import it:
Yii::import('ext.bootstrap.components.Bootstrap');
Related
In Yii2 there is a $this->registerJs() method available in views for registering inline JS. I use it as described in documentation:
$js = "...prepare js code...";
$this->registerJs($js, View::POS_READY);
The problem is that with View::POS_READY (which is the default for second argument) registerJs() implicitly requires yii\web\JqueryAsset (source code).
But I already included Jquery in my own asset bundle - it is all-min.js, there are Jquery + plugins minified and concatenated in a single file. Yii2 includes JqueryAsset, therefore duplicating Jquery on a resulting page.
How do I tell Yii2 that Jquery is already included or avoid this duplication in some other way?
You can easily customize jquery asset bundle by configuring assetManager in the application components configuration (usually config/web.php).
You can disable one or multiple asset bundles by associating false with the names of the asset bundles that you want to disable :
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => false,
],
],
Read more : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#customizing-asset-bundles
Good day.
I've just started learning ZF2, replicated the Album example step-by-step, and then decided to make it a bit more interesting by adding user registration, and then make the two work together in some way, so i added a new 'Auth' module.
So, when i only had one module in the module list (aside from the Application module) in application.config.php, it was all fine, but when i added the Auth module to the list like so:
'modules' => array('Application', 'Album','Auth',)
i got the following error when trying to access any views from the album module which was working absolutely fine prior to this change:
Zend\View\Renderer\PhpRenderer::render: Unable to render template "album/album/index"; resolver could not resolve to a file
But when i changed the order in which the modules are presented in this array like so:
'modules' => array('Application', 'Auth','Album',)
not a single view (and it has only one) from the Auth module could be rendered, while the Album module was fine.
Zend\View\Renderer\PhpRenderer::render: Unable to render template "auth/user/index"; resolver could not resolve to a file
Both of these views exist at these locations, but the renderer doesn't see them for some reason.
You can see the project's contents here.
Thank you for any tips in advance.
Looks like you copy pasted the the view manager config for Auth module.config.php.
This should be auth rather than album for your templates to work correctly.
'view_manager' => array(
'template_path_stack' => array(
'auth' => __DIR__ . '/../view',
),
),
I'm trying to set an alias in Yii2 but I'm getting a Invalid Parameter / Invalid path alias for the below code that is placed in the app config file:
'aliases' => [
// Set the editor language dir
'#editor_lang_dir' => '#webroot/scripts/sceditor/languages/',
],
If I remove the # it works.
I noticed you can do this:
Yii::setAlias('#foobar', '#foo/bar');
...but I would prefer to set it within the app config file. Is this not possible? If so, how?
Yii2 basic application
To set inside config file, write this inside $config array
'aliases' => [
'#name1' => 'path/to/path1',
'#name2' => 'path/to/path2',
],
Ref: http://www.yiiframework.com/doc-2.0/guide-structure-applications.html
But as mentioned here,
The #yii alias is defined when you include the Yii.php file in your entry script. The rest of the aliases are defined in the application constructor when applying the application configuration.
If you need to use predefined alias, write one component and link it in config bootstrap array
namespace app\components;
use Yii;
use yii\base\Component;
class Aliases extends Component
{
public function init()
{
Yii::setAlias('#editor_lang_dir', Yii::getAlias('#webroot').'/scripts/sceditor/languages/');
}
}
and inside config file, add 'app\components\Aliases' to bootstrap array
'bootstrap' => [
'log',
'app\components\Aliases',
],
In config folder create file aliases.php. And put this:
Yii::setAlias('webroot', dirname(dirname(__DIR__)) . '/web');
Yii::setAlias('editor_lang_dir', '#webroot/scripts/sceditor/languages/');
In web folder in index.php file put:
require(__DIR__ . '/../config/aliases.php');
Before:
(new yii\web\Application($config))->run();
If run echo in view file:
echo Yii::getAlias('#editor_lang_dir');
Show like this:
C:\OpenServer\domains\yii2_basic/web/scripts/sceditor/languages/
#webroot alias is not available at this point, it is defined during application bootstrap :
https://github.com/yiisoft/yii2/blob/2.0.3/framework/web/Application.php#L60
No need to define this alias yourself, you should simply use another one :
'aliases' => [
// Set the editor language dir
'#editor_lang_dir' => '#app/web/scripts/sceditor/languages/',
],
To improve on #vitalik_74's answer
you can place it in config/web.php instead(if you are using the basic yii app, I'm not sure about the main config file in the advance version, but the same applies, just put the require on the main config file) so that it gets shorten to:
require(__DIR__ . '/aliases.php');
I've been having a hard time trying to figure out how to load jQuery or other CORE scripts in Yii 2.
In Yii 1 it seemed this was the way:
<?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>
In Yii 2, $app is a property of Yii, not a method, so the above naturally doesn't work, but changing it to:
<?php Yii::$app->clientScript->registerCoreScript("jquery"); ?>
produces this error:
Getting unknown property: yii\web\Application::clientScript
I couldn't find any documentation for Yii 2 about loading core scripts, so I tried the below:
<?php $this->registerJsFile(Yii::$app->request->baseUrl . '/js/jquery.min.js', array('position' => $this::POS_HEAD), 'jquery'); ?>
Whilst this loads jQuery in the head, a second version of jQuery is also loaded by Yii when needed and hence causes conflict errors.
Additionally, I don't want to use Yii's implementation of jQuery, I would prefer to maintain my own and hence that is why I am doing this.
How can I load jQuery and other core files without Yii loading duplicate copies of them when it needs them?
In order to disable Yii2's default assets you can refer to this question:
Yii2 disable Bootstrap Js, JQuery and CSS
Anyway, Yii2's asset management way is different from Yii 1.x.x. First you need to create an AssetBundle. As official guide example, create an asset bundle like below in ``:
namespace app\assets\YourAssetBundleName;
use yii\web\AssetBundle;
class YourAssetBundleName extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'path/file.css',//or files
];
public $js=[
'path/file.js' //or files
];
//if this asset depends on other assets you may populate below array
public $depends = [
];
}
Then, to publish them on your views:
use app\assets\YourAssetBundleName;
YourAssetBundleName::register($this);
Which $this refers to current view object.
On the other hand, if you need to only register JS files into a view, you may use:
$this->registerJsFile('path/to/file.js');
yii\web\View::registerJsFile()
And if you need to only register CSS files into a view, you may use:
$this->registerCssFile('path/to/file.css');
yii\web\View::registerCssFile()
You can remove the core jQuery from loading like so:
config/web.php
'assetManager' => [
'bundles' => [
// you can override AssetBundle configs here
'yii\web\JqueryAsset' => [
'sourcePath' => null,
'js' => []
],
],
],
I've been going around and around reading the docs and doing searches, but there isn't a clear explanation of how to reach a plugin view using a web browser.
My plugin is called MediaManager.
I have CakePlugin::loadAll(); in my bootstrap.php file.
I have CakePlugin::routes(); in my routes.php file.
I have MediaManagerAppController.php and MediaManagerAppModel.php in their respective folders, and I have a MediaController.php file with a MediaManager class that extends MediaManagerAppController, and a manager function defined within. In the view folder, I have a view file called manager.ctp.
I enter the URL /MediaManager/Media/manager into the address bar and I recieve an error that I haven't created a MediaManagerController in my app/Controllers/ directory, so obviously it isn't even trying to access my plugins directory.
I double checked that I was entering the url correctly by copying the output of $this->Html->url(array('plugin' => 'MediaManager', 'controller' => 'media', 'action' => 'manager')); I also tried every combination of upper and lower case letters for the parameters just in case.
I've created a routes.php file in my plugin/MediaManager/Config/ directory, and put in a var_dump, but it never gets called either.
Please help!
Thanks.
You should stick to the convention to use snake_case in your URLs, thus:
$this->Html->url(array('plugin' => 'media_manager', 'controller' => 'media', 'action' => 'manager'));
which will produce the URL
/media_manager/media/manager
Even though both versions might work.
Also make sure you clear the cache after adding plugins (or loading new ones).