Passing multiple parameters in a hyperlink in yii2 with clean urls, Html::a() doesnt generate clean url - yii2

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

Related

Yii2 UrlManager Rule For String

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'

Yii2 how to set default language in codemix url manager

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

Yii URL rules to use ID as a parameter

In Yii I am using below code to generate a URL
Yii::$app->urlManager->createUrl('site/delete',array('id' => 100))
It generates the URL like below and parameter ID is missing in the URL
http://localhost/yii-basic/web/index.php?r=site%2Fdelete
In the config file, rules are as below
'urlManager' => [
'enablePrettyUrl' => false,
'showScriptName' => false,
'rules' => [
'post/<id:\d+>/<title:.*?>'=>'post/view',
'posts/<tag:.*?>'=>'post/index',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
],
],
According to the documentation, the correct syntax would be:
Yii::$app->urlManager->createUrl(array('site/delete', 'id' => 100));
I find it more convinient to use \yii\helpers\Url class and short array syntax:
Url::to(['site/delete', 'id' => 100]);

Yii2 url config not working

I'm using yii2 and i want url to be this way:
example: backend.dev/ads/browse/city/london
My code is this(backend/config/main.php):
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'<controller:\w+>/<city:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<city:\d+>' =>
'<controller>/<action>'
],
],
And my link is:
echo Html::a('<h4>'.$cities[$i].'</h4>', ['/ads/browse',
'city'=>$cities[$i]], ['class'=>'btn buttonCity']);
But the output is this: backend.dev/ads/browse?city=london
I want ? to disappear, help me please.
\d+ means it expects number but you gave london.
Change
'<controller:\w+>/<action:\w+>/<city:\d+>' => '<controller>/<action>'
to
'<controller:\w+>/<action:\w+>/<city:\w+>' => '<controller>/<action>'
Thanks to #Bizely
I have to make these changes:
'<controller:\w+>/<action:\w+>/<city:\d+>' => '<controller>/<action>'
change to:
'<controller:\w+>/<action:\w+>/<city:\w+>' => '<controller>/<action>'
My carelessness!

How to cache database translations on yii2

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
],