Action->Id Camelcase And Lowercase Inconsistency

Hi guys

I have the following two controllers and actions:

SiteController/actionCreateEnterprise

usr1_UserController/actionResetPassword

In a defaultScope function in my model, I use Yii::app()->controller->action->id.

For SiteController/actionCreateEnterprise it returns: ‘createEnterprise’ (CAMELCASE)

BUT, for usr1_UserController/actionResetPassword it returns: ‘resetpassword’ (LOWERCASE)

My config file contains:




'urlManager'=>array(

	'urlFormat'=>'path',

	

	'rules'=>array(

		'<controller:\w+>/<id:\d+>'=>'<controller>/view',

		'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

		'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

	), 

),



Any ideas why action -> id would return the actions in different case?

Are you sure you haven’t named the action method actionResetpassword() instead of actionResetPassword() in your controller?

Yip sure. Here it is (copied and pasted): actionResetPassword()

I’m not sure without seeing more code. See if you can find minimal code that duplicates the problem.

Hi, here is the entire controller:


<?php


class usr1_UserController extends myCController

{

	public function actionForgotPassword()

	{

		$branchesToUpdate = Adm3Branch::model()->findAll(); 

	}

}

Here is the model:




<?php


class Adm3Branch extends myCActiveRecord

{

	public $var_adm3_business_name;


	public function defaultScope() 

	{	

		 

		$guest = Yii::app()->user->isGuest;

		$contrl = Yii::app()->controller->id; this returns "usr1_User" 

		$actn = Yii::app()->controller->action->id; this returns "forgotpassword"

...



But, If I use this controller:




<?php

class SiteController extends myCController

{

	public function actionIndex()

	{

		$this->render('index');

	}


	public function actionLogin()

	{

        $loginModel = new UsrF1LoginForm;

		$formToDisplay = 'Login';

        $registerModel = new Usr1User;

		$registerModel->scenario = 'insertUser';


        $this->render('login_register', array(

			'loginModel'=>$loginModel,

			'registerModel'=>$registerModel,

			'formToDisplay'=>$formToDisplay,

		)); 

	}


	public function actionCreateEnterprise()

	{

		$branchesToUpdate = Adm3Branch::model()->findAll();

	}

}

then it works fine and this function $actn = Yii::app()->controller->action->id, returns "createEnterprise" in the model.

All I can think to do is to change the statement in the model to:


$actn = strtolower(Yii::app()->controller->action->id);

At least you then know that all actions will be converted to lowercase in the model’s defaultScope. But this is not ideal.

I have no clue…

and where is the code of actionResetPassword ?

You’re not setting the URL manager’s caseSensitive property to false at any point are you? (Clutching at straws now)

Hi Maurizio

I used actionForgotPassword just to see if it produces the same problem as actionResetPassword. It does.

Keith, I don’t think so. The only thing I am doing with UrlManager is that little bit in the config file.

I’m not familiar with Yii’s source code but would it change anything if you removed the underscore from the controller’s class name?

I’ll check it out.

Hi bennouna

Taking out the underscore has no influence.

OK, it looks like Yii::app()->controller->action->id pics up the action from the link:

If my link looks like this:


<a href="<?php echo Yii::app()->request->baseUrl; ?>/index.php/site/forgotpassword">Forgot Password</a>

then Yii::app()->controller->action->id returns "forgotpassword" (LOWERCASE).

But, if my link looks like this:


<a href="<?php echo Yii::app()->request->baseUrl; ?>/index.php/site/forgotPassword">Forgot Password</a>

then Yii::app()->controller->action->id returns "forgotPassword" (CAMELCASE).

Probably a stupid mistake on my side.

Of course… but this is not the way you should write links… what would happen if you would change the URL rules… than all those "manual" links would be wrong.

Instead you should use createUrl() to generate links.

Yep, or CHtml::link().

I am using


	

echo CHtml::link($caption, array($actionUrl),

	array(

		'class'=>'mySubmitButton', 

	)

);



But obviously that is not enough, because I still have to get the action’s camel-case correct in $actionUrl.

So I will read through createUrl() again.

Many thanx guys.

Can I go back to ‘newbie’ now? :)

Relax. Everyone’s a newbie in something. Except turtles of course that can be ninjas :)

Is it better to keep all controller and action names lowercase in the links?

I always keep anything that will appear in a URL as lowercase, but that’s personal preference. It means that for controllers and actions that need to be shown as two words, you end up with class and action names like Member_areaController and actionChange_password(), which I find a little jarring, but I prefer that to having mixed case in the URL’s.

Cool, thanx Keith.