user3803707
user3803707

Reputation: 97

Does not respond as expected

I have the following code at the beginning of a controller (I'm using 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()
{
    // ...
}

As you can see, both the if statement and the switch statement are using $this->action->id, which returns the action name.

The problem that on the switch statement, it only accepts "Images", with a capital letter some why, while $this->action->id returns a lowercase string.

I even tried writing another if statement instead of the switch - but same problem.

Plus, tried checking the string $this->action->id returns both on actionImages() and the others - all are lowercases.

Upvotes: 0

Views: 60

Answers (3)

sprytechies
sprytechies

Reputation: 317

you can write your case statement in lowercase or you can use ucfirst($this->action->id) like this

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()
{
    // ...
}

Upvotes: 2

Atieh
Atieh

Reputation: 230

try to define strings and see if they match in both statements. Also check the source of action->id and what it returns.

Upvotes: 1

MMK
MMK

Reputation: 365

change the case of letters check strtolower() and ucfirst() methods

Upvotes: 1

Related Questions