Yii URL rules to use ID as a parameter - yii2

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]);

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

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!

Routing in pretty url in yii2

I'm trying to use spanjeta/yii2-backup
For Project GM it works fine. here I'm not using prettyurl
For Projectamitopticals I'm getting 404 error.I'm using pretty url here.
I'm providing comparative codes below
GM - the routing -
[
'url' => Url::to(['/backup']),
'label' => 'Backup',
'icon' => 'glyphicon glyphicon-send',
'visible'=>Yii::$app->user->can('c_billing-person'),
],
Amitopticals - the routing
[
'url' => Url::to(['/backup']),
'label' => 'Backup',
'icon' => 'glyphicon glyphicon-download-alt',
],
Output screenshot in GM
Output screenshot of Amitopticals
Log of GM
Amitopticals Log
Please let me know how to resolve this.
I've sorted it out
Changed rules for pretty url -
Previous pretty url rule
'rules' => [
'<alias:>' => 'site/<alias>',
],
Corrected pretty url rule
'rules' => [
'<alias:\W+>' => 'site/<alias>',
],
Thanks everyone.

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

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