[SOLVED] Call function every pageload

Hi,

i want to call a function every pageload whatever controller is given.

I have tested this LINK with behaviors but i am not really happy with this because for example i get no data for Yii::app()->controller->id.

I will use this for a web statistic (page impressions…).

Some ideas how i can achieve this?

thanks,

corvus

You can call this ‘dirty’, but what I used to do with something like this was to include the code in my /layouts/main.php, because in my case, I only use one layout for the whole application, thus only one main.php layout is being called. I did this when I was starting to learn Yii, and I was echo-ing and print_r-ing and var_dump-ing Yii::app()->controller->id,Yii::app()->user->name, etc., just for the sake of experimenting. I just remembered it now, since it can give what you want to know about almost everything in action in every page load.

Another option is to put afterAction() or beforeAction in your application’s main controller (/protected/components/Controller.php by default), and get the controller by $this->getId(), or action by $this->getAction()->getId(). I think this is much neater. You are still welcome to use my first suggestion though (nobody will notice ;))

I’d probably use/add method init() in protected/components/Controller.php.

That’s the way I would go too.

if you mean i just have to add this to the Controller


public function init() {

		$abc = array('controller' => 'init');

		var_dump($abc);

	}

this doenst work for me. something wrong or missing?

thanks

Make sure that:

  1. your controllers in /protected/controllers all extend from "Controller" (not "CController")

  2. your controllers call parent::init() in case they already use a init() method

changed to this but does not work :(


/**

 * Controller is the customized base controller class.

 * All controller classes for this application should extend from this base class.

 */

class Controller extends Controller

{

	/**

	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',

	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.

	 */

	public $layout='//layouts/column2';

	/**

	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.

	 */

	public $menu=array();

	/**

	 * @var array the breadcrumbs of the current page. The value of this property will

	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}

	 * for more details on how to specify this property.

	 */

	public $breadcrumbs=array();

	

	public function init() {

		parent::init();

		$abc = array('controller' => 'init');

		var_dump($abc);

	}

	

}

Uhm, not component/Controller.php should extend from Controller - but your controllers in the /proctected/controllers/ dir!

BTW:

Didn’t you notice that it’s hard to extend one class from the same class?? (Controller extends Controller). No offense, but usually it doesn’t hurt to think, before writing some code … ;)

my controllers all extend RightsBaseController because i use the Rights Module.

RightsBaseController extends from CController.

so i should change RightsBaseController to extend Controller and put my init function into this Module?

I see. Then i would try this:

components/Controller.php:




Controller extends RightsBaseController

{

  public function init() {

    // your stuff...

    parent::init();

  }

}



controllers/SomeController.php


SomeController extends Controller

worked. thanks :)