YII2 call jquery-ui code before bootstrap.js - yii2

Here is my asset code..
public $js = [
'js/jquery-ui.min.js',
'js/app.min.js',
];
I have some widgets used in the view file... and here are the order of the js files. What I want is to call the jquery-ui.js before bootstrap.js.. How to do that??

Placing jQuery UI after Bootstrap doesn't make any sense since they are not dependent on each other at all. But for including bundle before another, you should add dependency to the related bundle.
For custom asset bundle you can just write this:
$depends = [
// Write classes of dependent asset bundles here, for example:
'yii\jui\JuiAsset',
];
But because Bootstrap is built-in asset, you can not modify it that way. Instead you can set it globally through config of Asset Manager:
return [
// ...
'components' => [
'assetManager' => [
'bundles' => [
'yii\bootstrap\BootstrapAsset' => [
'depends' => [
'yii\jui\JuiAsset',
];
],
],
],
],
];
Or just set dependency in one specific place before rendering view:
Yii::$app->assetManager->bundles['yii\bootstrap\BootstrapAsset'] = [
'depends' => [
'yii\jui\JuiAsset',
];
],
Official docs:
Customizing built-in asset bundles
Asset Manager
yii\web\AssetBundle $depends

Related

How to load class at every page load in advanced app

I want to have class in which I would put all my methods that should run everytime someone loads a page, lets call it InitRoutines.
In yii2 basic app I would do something like this. I would add class to compontents config file, and add it to bootstrap, simple as that.
But I can not figure out how to do it in advanced app, preferable in common/config
config.php
$config = [
// ..
'components' => [
'InitRoutines' => [
'class' => 'app\commands\InitRoutines',
],
],
];
$config['bootstrap'][] = 'InitRoutines';
And in InitRoutines I have init class which run everything I need at page load.
InitRoutines.php
namespace app\commands;
use Yii;
use yii\base\Component;
use app\commands\AppHelper;
use app\commands\Access;
class InitRoutines extends Component
{
public function init()
{
parent::init();
Access::checkForMaintenance();
Yii::$app->language = AppHelper::getUserLanguageCode();
}
}
How can I accomplish same in advanced app ?
I think I found the solution, I dont know if it is correct way but it seem to be working.
config.php
return [
// ...
'components' => [
'InitRoutines' => [
'class' => 'common\commands\InitRoutines',
],
],
'bootstrap' => [
'log',
'common\commands\InitRoutines'
],
];

how to run action of frontend controller from backend in yii2

Is it possible to run action of frontend controller from backend?
this code works if called action is in backend too. can i specify in runAction that controller/action is in frontend?
Yii::$app->runAction('controller/action')
Also i' m tried something like
$c=new controller();
$s->action();
too but it seems it not working too. //new controller() need some parameters and i have no idea what it is.
The yii\base\Application object has a public property controllerNamespace, which defaults to app\\controllers. You need to change it accordingly to changing default controller namespace.
Change namespace in action:
Yii::$app->controllerNamespace = 'frontend\controllers' and use runAction
A way could be this .
In your backend application config you could create an additional 'UrlManager' component
name eg: urlManagerFrontEnd
return [
'components' => [
'urlManager' => [
// here is your backend URL rules
],
'urlManagerFrontEnd' => [
'class' => 'yii\web\urlManager',
'baseUrl' => 'http://your_path/frontend/web/index.php',
'enablePrettyUrl' => true,
'showScriptName' => false,
],
],
];
Then you should invoke following to compose front-end URL:
Yii::$app->urlManagerFrontEnd->createUrl();
and add the controller/action you prefer
remeber that
runAction()
Runs an action within this controller with the specified action ID and
parameters.
http://www.yiiframework.com/doc-2.0/yii-base-controller.html#runAction()-detail
this mean that cannot run an action of another controller or of another application ..
If you need a service then you must configure a RESTFull or you simply need a redirection you can use redirect

Yii2 params access within local config file in common directory

I'm using Yii2 advanced template,
I want to access params.php in main-local.php file,
I called this ways:
main-local.php:
'mailer' => [
'class' => 'myClass',
'apikey' => \Yii::$app->params['mandrill_api_key'],
'viewPath' => '#common/mail',
],
and I have stored this mandrill_api_key in params.php
params.php:
<?php
return [
'adminEmail' => 'admin#example.com',
'supportEmail' => 'support#example.com',
'user.passwordResetTokenExpire' => 3600,
'mandrill_api_key' => 'mykey'
];
I'm getting this error:
Notice: Trying to get property of non-object in C:\xampp\htdocs\myproject\common\config\main-local.php on line 25
What should I do to access these parameters?
The config files are read before the application is instantiated as explained in the request lifecycle:
A user makes a request to the entry script web/index.php.
The entry script loads the application configuration and creates an application instance to handle the request.
The application resolves the requested route with the help of the request application component.
...
As such \Yii::$app does not yet exist hence the error. I would suggest moving your api_key definition to the main-local.php config such that there is no confusion over where it is being set:
'mailer' => [
'class' => 'myClass',
'apikey' => 'actual api key',
'viewPath' => '#common/mail',
],
Alternatively, you can use Yii2's dependancy injection container to set the apikey in your application's entry script:
...
$app = new yii\web\Application($config);
\Yii::$container->set('\fully\qualified\myClass', [
'apikey' => \Yii::$app->params['mandrill_api_key'],
]);
$app->run();
You can just do
$params['mandrill_api_key']
you dont need to use
\Yii::$app->params['mandrill_api_key']
The params is a part of config and you can not call this in your config .
the best way for handel this you can use this in your class :
myClass:
class myClass extends ... {
public $apikey;
public function __construct(){
$this->apikey = \Yii::$app->params['mandrill_api_key'];
}
}

Where to put pretty urls configuration

Where should I put this code:
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
// your rules go here
],
// ...
],
And what rules should I put there?
You need to put it inside application config.
Its location varies depending on template you are using (basic / advanced).
There is components section, where each framework component configured:
return [
'components' => [
'urlManager' => [
'enablePrettyUrl' => true,
'rules' => [
// your rules go here
],
// ...
],
],
];
This will prevent passing route passed as $_GET parameter r.
Note that for pretty urls you also need to add this:
`showScriptName` => false,
This will prevent showing index.php in urls.
As for rules - it's more extensive question. Its content depends on your needs.
You can configure route / group of routes / all routes.
Read more in official docs:
UrlManager $enablePrettyUrl
UrlManager $showScriptName
UrlManager $rules
Using Pretty Urls
Url Rules

Yii2 - Change jQuery Version

I want to change the jQuery version of yii2. I've installed yii2 through composer. I've read a similar question here:
Change JqueryAsset's jQuery version on certain page
But the problem I'm facing is where should I place this code? Should I place it on the view page? But I want to change the jquery version for all the pages. Is this code compatible for yii2?
You can easily customize jquery asset bundle by configuring assetManager in the application components configuration (usually config/web.php), for example if you want to use a jquery file in web/js folder :
'assetManager' => [
'bundles' => [
'yii\web\JqueryAsset' => [
'sourcePath' => null,
'basePath' => '#webroot',
'baseUrl' => '#web',
'js' => [
'js/jquery.js',
]
],
],
],
Read more : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#customizing-asset-bundles