C
C
cmx2014-05-29 12:41:03
Yii
cmx, 2014-05-29 12:41:03

How to style routing rules for a single controller in Yii?

There is one controller which has 3 actions.
How to set up routing rules so that you do not need to pass the value of the controller (or if the value is not passed, then use default)?
UrlFormat used: get.
Tried many options but nothing works. Apparently, I do not understand something. Where did you stop:

'urlManager'=>array(
  'urlFormat'=>'get',
  'routeVar' => 'action',
  'rules' => array(
    '<action>' => 'site/<action>',
  ),
),

The final url I'm testing looks like this:
http://example.com/?action=index

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Marinin, 2014-06-06
@cmx

In Yii 1 (as in the second - I don’t know), the name of the controller and the name of the action are passed in one GET parameter “ r ” (short for route). In this case, the name of the action can be omitted. But the controller name must be required.
If you set urlFormat = path , then Yii will parse the URL and use "rules".
If you set urlFormat = get , then Yii just stops parsing the URL and setting "rules" is useless.
By default, it seems, DefaultController is called. You can change this in the config:

return array(
    'defaultController' => 'site',   // По умолчанию будет использоваться SiteController
    // ...
);

The controller must have a default action actionIndex() , or it can be overridden like this:
class DefaultController extends CController 
{
    public $defaultAction = 'my';

    public function actionMy() // Будет вызван по умолчанию
    {
          // делаем что-то
    }
}

You can do this (default urlFormat = path, no need to change routeVar):
'urlManager' => array(
    'showScriptName' => false,
    'rules' => array( 
        '' => '<controller>/<action>', // Будет использоваться контроллер и действие по умолчанию
    ),
),

And in the controller something like this:
class DefaultController extends CController 
{
    public function init()
    {
          parent::init();

          if ($action = Yii::app()->request->getParam('action', null)) 
          {
              $action = 'action' . ucfirst($action);

              if (in_array($action, get_class_methods(__CLASS__))) {
                  call_user_func(array(get_class(), $action)); // Вызываем действие
              }
          }
    }

    public function actionIndex() // Будет вызван по умолчанию
    {
        // делаем что-то
    }

    public function actionSimple()
    {
        // делаем что-то
    }
}

You can contact it like this: site.ru/?action=simple . Site.ru/simple will also work .
You can create links like this: Yii::app()->createUrl('/', array('action' => 'simple'));
In general, the init () method can be moved to a separate class (for example, named AppContoller), which will inherit CController, and the working controller (with actions) will already inherit from it.
PS I wrote in the blind, I could have made a mistake somewhere

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question