PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, March 6, 2022

[FIXED] Yii2, optional parameter in the rounte

 March 06, 2022     controller, rules, yii, yii-url-manager, yii2     No comments   

Issue

During the development, I have faced to the one issue with urlManager.

I have SiteController with "category" action.

public function actionCategory($id = null, $city = null, $location = null)
{
   echo $id;
   echo $city;
   echo $location;
}

All possible combination that can be used with this action:

id, city, location = null
id, city = null, location
id, city = null, location = null
id = null, city = null, location = null
id = null, city = null, location

I do not know how to write the rules in the UrlManager, after that I will get the following values in the variables after press the links:

<h4><?= Html::a('ID City', ['/site/category', 'id' => 'barbershop', 'city' => 'Praha'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = 'barbershop'
city = 'Praha'
location = ''

<h4><?= Html::a('ID Location', ['/site/category', 'id' => 'barbershop', 'location' => '23,65'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = 'barbershop'
city = ''
location = '23,65'

<h4><?= Html::a('ID', ['/site/category', 'id' => 'barbershop'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = 'barbershop'
city = ''
location = ''

<h4><?= Html::a('City', ['/site/category', 'city' => 'Praha'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = ''
city = 'Praha'
location = ''

<h4><?= Html::a('Location', ['/site/category', 'location' => '14,23'], ['class' => 'btn btn-primary']) ?></h4>
The return value from "category" action:

id = ''
city = ''
location = '14,23'

Would you like you help me with this issue?


Solution

As per your requirements you need to set rules only for Controller and action name. All other fields are passed through $_GET. and you can fetch them using Yii::$app->getRequest()->getQueryParam('param') method.

For you url you can use normal rules for pretty url like

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ],
    ],


Answered By - Ninja Turtle
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing