Newbies questions

First i would like to say, Thanks for this great framework. Really makes coding a lot easier.

Few things i would like to point out and maybe ask

  1. I was wondering how would i manage to make a global class that i can enter within any of my controllers? and where would i place it?

  2. How can i make an error handler that i can just do new someexception('error here'); and it will show me the error from a template i make?

  3. you have good documentation tough examples are missing to almost everything. If you can provide small bits of code examples to each method it will be very helpful.

Thanks.

  1. I don't quite understand…could you explain a bit more?

  2. See http://www.yiiframew…splaying-errors

  3. This is a long process. We are striving to make documentation better and better.

Thanks for your comment.

  1. For example i have a complete class that i wrote for various things i have for my application. Now i don't want to do
<?php





class IndexController extends CController


{





	public $defaultAction='index';





	public function actionIndex()


	{


                require_once('path/to/my/own/class/myclass.php'); <<


                $general_functions = new mygeneral_functions; <<


		$reader = Yii::app()->db->createCommand("SELECT * FROM gps_orders LIMIT 0,10")->query();





		// each $row is an array representing a row of data


		foreach($reader as $row)


		{


			$levels[] = $row['oid'];


		}





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


		//Yii::app()->user->loginRequired();








		// show the page


		$this->render('Index');


	}


}

in every controller i have, so instead maybe adding the entire class and it's methods to the application instance so i can do something like the database class does for example if i want to use Yii::app()->db->something i added

'components'=>array(


		 'db'=>array(


            'class'=>'CDbConnection',


            'connectionString'=>'mysql:host=localhost;dbname=***','username'=>'', 'password'=>'', 'charset'=>'utf8',


        ),

to my configuration file and then i can use Yii::app()->db->something where every i want in the application. Now i want to do the same thing but with my own class so i did:

'controllerMap'=>array(


        'myclass'=>array(


            'class'=>'myownclass'


        ),


        // other controllers


    ),

in the configuration file, but where do i place my file that will load the 'myownclass' ? and what i did is valid?

  1. not sure if i understood i would like for example to receive an input from a member then i check the incoming data, if it's empty then i would like to show an error but one of my own, it's not a 404, 400, 403 error just an internal one.

for example i used to do:

if(!$_POST['name'])


{


 $myfunctions->error('please enter name');


} 

How will i convert it to use the Yii structure?

  1. OK you had some things planned for release for the end of the year which is in two day, I hope there it will include some example codes and examples upon methods.

  2. another thing i wanted to know, i would like to make sure the member is logged in (for the ACP for example) if not then redirect him back to the login back. I can just insert the is loginRequired() in the begining of every controller, but i would like to avoid that and put it in one single place that will run all the time before any other action is taken and then i could verify that the member is actually logged in and then it passes just continue otherwise redirect to the login page.

Thanks again.

  1. Define a base controller class and extend all your controllers from this base class. In your base controller class, you override beforeAction() method and insert the code you need.

  2. Providing your own error view is the simplest way to go. You only need to write a single error.php if you don't want to write other errorXXX views. Inside the view, you can check $type to see what kind of exception you are raising and display the corresponding content. Of course, you can always write your own error handler and replace the default one by setting it as the 'errorHandler' application component.

  3. Refer to 1.

Thanks i will try the thing with the basecontroller

I did manage to display a page with my own error template. Tough i did no manage to print the error raised.

part of the template file that doesn't work:

<div class="auth_elements"><?php echo Yii::app()->error->getMessage(); ?></div>

error given:

Fatal error: Call to undefined method CErrorHandler::getMessage() in C:wampwwwrentcenteradminprotectedviewssystemerror330.php on line 25

I did add

'error' => array(

        'class' => 'CHttpException'

        ),

to the components array in the config file.

You should refer to existing error templates to see how to obtain different error information. For example, in order to display the error message, you just need to "echo $message". These are not explained clearly in current documentation.

Already did that and tried it and it didn't work just showed empty. Even using this template showed empty.

Here is the error.php from the base framework folder. when i try to use $data['message'] nothing happens.

<!DOCTYPE html PUBLIC


	"-//W3C//DTD XHTML 1.0 Transitional//EN"


	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">


<head>


<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>


<title>Error <?php echo $data['code']; ?></title>





<style type="text/css">


/*<![CDATA[*/


body {font-family:"Verdana";font-weight:normal;color:black;background-color:white;}


h1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }


h2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }


h3 {font-family:"Verdana";font-weight:bold;font-size:11pt}


p {font-family:"Verdana";font-weight:normal;color:black;font-size:9pt;margin-top: -5px}


.version {color: gray;font-size:8pt;border-top:1px solid #aaaaaa;}


/*]]>*/


</style>


</head>





<body>


<h1>Error <?php echo $data['code']; ?></h1>


<h2><?php echo nl2br(CHtml::encode($data['message'])); ?></h2>


<p>


The above error occurred when the Web server was processing your request.


</p>


<p>


If you think this is a server error, please contact <?php echo $data['admin']; ?>.


</p>


<p>


Thank you.


</p>


<div class="version">


<?php echo date('Y-m-d H:i:s',$data['time']) .' '. $data['version']; ?>


</div>


</body>


</html>

Sorry, it should be $data['message'], just as you tried.

Do you mean even the error view from the framework doesn't work?

Thanks worked like a chart. The problem i had was that i did:

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

i didn't passed the first paramter which is required otherwise the message showed as empty.  now i use:

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

And it works great.