A
A
arab7892015-03-31 12:52:26
Yii
arab789, 2015-03-31 12:52:26

Yii. How to make rule an optional parameter?

There is a rule:

'urlManager'=>array(
       'urlFormat' => 'path',
                            'showScriptName'=>false,
                            'rules'=>array(
                                'page/<id:\d+>/<alias:.*>' => 'page/index/',
                                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
      ),

I need to add a parameter to the 'page' rule <platform:.*>, but it may not be there.
If you write like this:
'page/<id:\d+>/<alias:.*>/<platform:.*>' => 'page/index/',

it doesn't work without this parameter.
How to set an optional parameter?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
arab789, 2015-03-31
@arab789

Maybe a crutch of course, but it turned out to solve the problem without setting an optional parameter. In 'rules' listed what links can be:

'rules'=>array(
                                'page/<id:\d+>/<alias:.*>/<platform:.*>' => 'page/index/',
                                'page/<id:\d+>/<alias:.*>' => 'page/index/',
                                'page/<id:\d+>' => 'page/index/',
                                'page' => 'page/index/',
                                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
      ),

M
matperez, 2015-03-31
@matperez

Try to use

/**
   * @var array the default GET parameters (name=>value) that this rule provides.
   * When this rule is used to parse the incoming request, the values declared in this property
   * will be injected into $_GET.
   */
  public $defaultParams=array();

Something like this instead of a line with a route
[
  'route' => 'page/index', 
  'pattern' =>'page/<id:\d+>/<alias:.*>/<platform:.*>', 
  'defaultParams => [
    'platform' => 'some platform'
  ]
]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question