没有按预期回应

时间:2014-09-06 03:42:39

标签: php yii controller action

我在控制器的开头有以下代码(我正在使用Yii):

protected function beforeAction($action = null)
{
    switch ($this->action->id)
    {
        case 'Images':
            // Do something
            break;

            // ...
    }

    if ($this->action->id == 'index' || $this->action->id == 'videos')
    {
        // Do something else
    }

    return true;
}

public function actionIndex()
{
    // ...
}

public function actionVideos()
{
    // ...
}

public function actionImages()
{
    // ...
}

如您所见,if语句和switch语句都使用$ this-> action-> id,它返回操作名称。

在switch语句中,它只接受“Images”,大写字母为什么,而$ this-> action-> id返回一个小写字符串。

我甚至尝试编写另一个if语句而不是切换 - 但同样的问题。

另外,尝试检查字符串$ this-> action-> id返回actionImages()和其他 - 都是小写。

3 个答案:

答案 0 :(得分:2)

您可以用小写编写case语句 要么 您可以像这样使用ucfirst($ this-> action-> id)

protected function beforeAction($action = null)
{
    $var=ucfirst($this->action->id);
    switch ($var)
    {
        case 'Images':
            // Do something
            break;

            // ...
    }

    if ($this->action->id == 'index' || $this->action->id == 'videos')
    {
        // Do something else
    }

    return true;
}

public function actionIndex()
{
    // ...
}

public function actionVideos()
{
    // ...
}

public function actionImages()
{
    // ...
}

答案 1 :(得分:1)

更改字母的大小写 检查strtolower()ucfirst()方法

答案 2 :(得分:1)

尝试define字符串,看看它们是否在两个语句中匹配。还要检查action->id的来源及其返回的内容。

相关问题