Making a custom BaseController

How would i make a base controller that i could then extend all other controllers with the my custom base controller that will consist of lots of properties and methods that i would like to be available in all the classes that extends the base controller?

I tried the following:

<?php

class BaseController extends CController


{





	public function beforeAction()


	{


		die('123');


	}


}

And in one of the controllers that extends the base controller

<?php





class SiteController extends BaseController 


{


	


	public $defaultAction='login';


	


	public function actionlogin()


	{


		CController::setPageTitle(Yii::t('test', 'Login'));


		


		// show the page


		CBaseController::renderFile(CController::getViewFile('Login'));


	}


	


	public function actionDo()


	{


		throw new CHttpException('The specified post cannot be found.');


		//CApplication::displayError('00001', 'Some problems were made', '1', '2');


		//CBaseController::renderFile(CController::getViewFile('Login'));


	}


}

Didn't work as it tried to load it from the main application folder. where do i place the base controller so it will load correctly?

Thanks.

You should either import (include) the base class file, or place the base class file in a directory that is imported (e.g. protected/components)

Ohh may, I added it in the components directory and it worked. Nice job. Is there anything this thing doesn’t do?! :)

Now i would like to know another thing how do i manage to use the languages? I did inside a template or php file doesn't really matter the following:



Yii::t('test', 'test');

Which should load the protected/messages/en_us/test.php

i have that file in that directory with the follwing:

<?php





return array(





'test' => "test333",





); 

it still displays 'test' and not the test333 string. How would i go to be able to change the value of the language dynamically? I mean if i enter &lang=he_il i would like the language to be set up as 'he_il' and the messages to be displayed accordingly.

Another thing i would like to know how can i use the $_POST ? i know it's Yii::app()->request but how do i access a property?

Sorry for so many questions but i am getting the hang of it and once i do there will sure be posted lots of widgets and extensions and stuff.

For message translation, read http://www.yiiframew…ide/topics.i18n

Make sure you the 'language' property has a value different from 'sourceLanguage' of the application.

Yii doesn't try to encapsulate $_POST, $_GET and so on. You just access them like you usually do. Yii::app()->request mainly provides request information that may not be consistent across platforms.

How can i make sure the Global variables are sanitized?

These variables are sanitized on demand. For example, when you assign a value in $_POST to a data model, the data model will first be validated (based on your validation rules), and then when being saved to database, the values will be quoted appropriately (based on column types). And if you want to display something that was entered via $_POST, you can use CHtmlPurifier to avoid XSS attacks.

If you feel like to sanitize these variables no matter they are used or not, you can write a filter to accomplish this task. You can use the PHP function filter_input() internally.

Thanks, Will try that tomorrow.

Thanks the trick with the language worked, I assume it doesn't save it tough if i hit the refresh button it go back to the source language. Do i need to save it on my own using a cookie or updating the DB or does Yii has something that does it already?

Thanks again.

Nope, Yii doesn't save the language to cookie. We probably can consider adding this as a new feature.

Cool, For now this is how i did it if any one would come across this as well:

/* Language Variables */


		$this->_mylanguage = 'en_us';





		# Do we have a cookie for the language?


		if(Yii::app()->request->cookies['Rlang']->value)


		{


			$this->_mylanguage = Yii::app()->request->cookies['Rlang']->value;


		}


		


		# Changed the language right now?


		if(isset($_GET['language']) && $_GET['language'] != '')


		{


			$this->_mylanguage = trim($_GET['language']);


			# Save in cookie


			Yii::app()->request->cookies['Rlang'] = New CHttpCookie('Rlang', $_GET['language']);


			$this->_mylanguage = Yii::app()->request->cookies['Rlang']->value;


			Yii::app()->request->cookies['Rlang']->expire = time() + 100;


		}


		


		# Set the language


		Yii::app()->setLanguage($this->_mylanguage);


		/* Language Variables */

Two suggestions:

You need to check if Yii::app()->request->cookies['Rlang'] is null or not.

You also need to check Yii::app()->request->cookies['Rlang']->value gives a valid language code.

if(Yii::app()->request->cookies['Rlang']->value)


      {


         $this->_mylanguage = Yii::app()->request->cookies['Rlang']->value;


      }

Wouldn't that return null/false if the cookie is null or doesn't exists?

Quote

You also need to check Yii::app()->request->cookies['Rlang']->value gives a valid language code.

is there any way of getting the list of available languages? or should i just read the messages directory and insert the sub directories into an array and check against that array?

cookies['Rlang'] could be null.

You are responsible to provide the list of available languages (when you are developing a site, you know what languages are going to be supported.)

Quote

Nope, Yii doesn't save the language to cookie. We probably can consider adding this as a new feature.

+1

What's the preferred method now? Extending CController and using beforeAction / cookies (much like Vince here I guess)?

//Magnus