I have Nav with items:
[
'label' => 'All',
'url' => ['project/index'],
],
[
'label' => 'Done',
'url' => ['project/index', 'assigned' => 'done'],
],
But both of them have an active class when I go to project/index&assigned=done. How can I force to attach this class only if item's url is strictly equal to $route value?
Nav widget will make an item active when its route and parameters match $route (if not set, it will use the route of the current request) and $params (if not set, it will use $_GET).
Your first item will always be active if the route is project/index (take a look here).
You should try this for example :
[
'label' => 'All',
'url' => ['project/index', 'assigned' => 'not-done'],
],
[
'label' => 'Done',
'url' => ['project/index', 'assigned' => 'done'],
],
Read more about how Nav widget set an item active or not.
Related
I'm trying to create a simple rule url, and I can't get it to work.
I want the following rule:
mysite.com/[username]
to go to
mysite.com/kit/page?id=[username]
Is this possible?. Right now I only have one rule, but I need to keep that one as well
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
],
],
If I understand correctly you are saying that you have an existing page mysite.com/kit/page?id=[username] which should be shown if you type in the URL mysite.com/[username] in the address bar, if yes then you can update the urlManager like below
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'/' => 'site/index',
'<id:\w+>' => 'kit/page'
]
]
Ideally, I would use mysite.com/kit/[username] to avoid conflicts with any other controller which somehow matches up with any username and use 'kit/<id:\w+>' => 'kit/page'.
Note : \w+ matches any word character (equal to [a-zA-Z0-9_]), so if your username can have any other character allowed you might have to update the pattern, for example to allow - you should change the rule to '<id:[\w\-]+>' => 'kit/page'
This app ru language start
'language'=>'uz',
'i18n' => [
'translations' => [
'*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#frontend/messages',
'sourceLanguage' => 'uz',
'fileMap' => [
'main' => 'main.php',
'yii' => 'yii.php',
],
],
],
],
'urlManager' => [
'class' => 'codemix\localeurls\UrlManager',
'showScriptName' => false,
'enableLanguageDetection' => true,
'enablePrettyUrl' => true,
// 'defaultLanguage'=>'uz',
'enableDefaultLanguageUrlCode' => true,
'languages' => ['uz', 'ru','oz','en'],
I am set system langugae uz, source langugae uz and codemix url manager one method have defaultLangugae but its not working
this error picture
Never used it but looking at the source code it says you need to use the languages option as an array of language codes. More specific patterns should come first, e.g. 'en_us' before 'en'. This can also contain mapping of <url_value> => <language>, e.g. 'english' => 'en'.
ou need to change the option to 'languages' => ['en_us'],
You must setup language for app config.
UPD:
Setup config(frontend/config/main.php) to needed language for app. Then first time app will launch in this language:
'language' => 'en'.
If this not work check in incognito mode, you must clean old session saved cookies
I have quite a few actions in my SiteController. I don't want site in the URL, so I have this in my config file:
'rules' => [
'/' => 'site/index',
'about' => 'site/about',
'contact' => 'site/contact',
'login' => 'site/login',
'logout' => 'site/logout',
'register' => 'site/register',
],
Is there a way to simplify all of this (excluding the / route) in to one rule?
You can use
'rules' => [
'<action:\w+>' => 'site/<action>',
],
This is the best way:
'<action:(about|contact|login|logout|register)>' => 'site/<action>',
The answer posted by Shringiraj Dewangan will match any request and route to site controller, whereas this will only match the specified requests.
I am trying to generate a hyper link by the method mentioned in
http://www.yiiframework.com/doc-2.0/guide-helper-html.html#hyperlinks like this
Html::a('<b>Register</b>',
['story/create', array('id' =>39,'usr'=>'11')],
['class' => 'profile-link'])
I want to get url like story/create/id/39/usr/11
But it is generating as
story/create?1%5Bid%5D=39&1%5Busr%5D=1
I have enabled the clean url functionality of yii2 like
'urlManager' => [
'class' => 'yii\web\UrlManager',
// Disable index.php
'showScriptName' => false,
// Disable r= routes
'enablePrettyUrl' => true,
'rules' => array(
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
),
], also.
How this can be achieved?
With generate url use like that (see more http://www.yiiframework.com/doc-2.0/guide-helper-url.html):
Html::a('<b>Register</b>',
['story/create', 'id' =>39,'usr'=>'11'],
['class' => 'profile-link'])
In urlManager input new rule:
rules' => array(
....
'story/create/<id:\d+>/<usr:\d+>' => 'story/create',
),
Output url will be like that:
story/create/39/11
And in controller:
public function actionCreate($id, $usr)
And Yii2 provide this parameter.
create Url Dynamically
Html::a('<b>Register</b>',
['story/create', 'id' =>39,'usr'=>'11'],
['class' => 'profile-link'])
In urlManager config rules :
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>/<usr:\d+>' => '<controller>/<action>',
],
],
Output url will be like that:
story/create/39/11
Another useful method :
Write in urlManager rules in your
'rules'=>array('/controller/action/<limit>/<offset>'=>'/controller/action/'),
Can be accessed in url controller/action/100/20
How to cache database translations on yii2
I tried the following but not worked
'i18n' => [
'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
'languages' => ['en', 'ar', 'fr'],
'sourceMessageTable' => 'source_message',
'messageTable' => 'message',
'cache' => 'cache'
],
The problem is in Zelenin i18n module. If you look at Module.php file, you can see:
$this->translations['*'] = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'on missingTranslation' => $this->missingTranslationHandler
];
in init() method. This Code set DbMessageSource options and there are no any options about caching. Module have no any caching options too.
If you change this code to:
$this->translations['*'] = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'enableCaching' => true,
'cachingDuration' => 3600,
'on missingTranslation' => $this->missingTranslationHandler
];
Cache will work. Some SELECT messages will gone from debug list.
The Yii documentation for i18n db messages says that the property cache only has meaning when the property cacheDuration is not zero. I suggest you set this value, so;
'i18n' => [
'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
'languages' => ['en', 'ar', 'fr'],
'sourceMessageTable' => 'source_message',
'messageTable' => 'message',
'cache' => 'cache',
'cacheDuration' => 3600
],