Answer the question
In order to leave comments, you need to log in
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>',
),
),
http://example.com/?action=index
Answer the question
In order to leave comments, you need to log in
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
// ...
);
class DefaultController extends CController
{
public $defaultAction = 'my';
public function actionMy() // Будет вызван по умолчанию
{
// делаем что-то
}
}
'urlManager' => array(
'showScriptName' => false,
'rules' => array(
'' => '<controller>/<action>', // Будет использоваться контроллер и действие по умолчанию
),
),
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()
{
// делаем что-то
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question