mezlad
mezlad

Reputation: 41

Yii 2 Optional Parameter

Ok, I am transitioning an app from yii 1.1 to yii 2, unfortunately I cannot figure out how to use optional parameters within my url routes. Even when I set defaults in the urlmanager in config I can't state the second parameter without the first one or I end up with a 404 error.

Is there a way to replicate optional url parameter rules such as

array( '<controller:\w+>/<action:\w+>?(/<status>)?',
'pattern' => '<controller>/<action>'                   
),

in yii 2 ?

Upvotes: 4

Views: 6339

Answers (3)

d.raev
d.raev

Reputation: 9556

If you don't want to use Default and stick to the short syntax, you can define 2 rules instead, make sure the "longer" rule is higher in the list:

rules : [
    <controller:\w+>/<action:\w+>/<status> => '<controller>/<action>',
    <controller:\w+>/<action:\w+> => '<controller>/<action>',
]

First rule will trigger if it matches an URL with status element and send you to the controller/action.
Second rule will trigger if first one is skipped. Make sure that your method has a default value for $status.

Upvotes: 2

PouriaDiesel
PouriaDiesel

Reputation: 735

After alot of search I found this solution.in your rules you must set two parameter:

array(   
   'pattern' => '<controller: \w+>/<action:\w+>/<status>',    
   'route' => '<controller>/<action>',   
),
array(
   'pattern' => '<controller:\w+><action:\w+>',
   'route => '<controller>/<action>',  
)

then go to your controller action and add this:

public function action...(/*Your inputs except status*/)
$get = Yii::$app->request->get();
$status = $get['status'] ?? null;

and then you can check $status value without default value.
note: if your parameter is a post parameter change get() to post().

Upvotes: 0

CedX
CedX

Reputation: 3987

This is not very clear in the documentation (see http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html), but here is the answer:

By default, all parameters declared in a rule are required. If a requested URL does not contain a particular parameter, or if a URL is being created without a particular parameter, the rule will not apply. To make some of the parameters optional, you can configure the defaults property of a rule. Parameters listed in this property are optional and will take the specified values when they are not provided.

So your route must be expressed like:

array(
  'pattern' => '<controller:\w+>/<action:\w+>/<status>',
  'route' => '<controller>/<action>',
  'defaults' => array('status' => '<a default value for status>')
)

Upvotes: 7

Related Questions