THooking component

The Hooking allows for creating actions

and filters and hooking functions, and methods. The functions or methods will

then be run when the action or filter is called.

I borrow this idea from wordpress.



function for get or create component instance 


function getComponent($id,$class=false) {


	//debug($id,'getComponent #id');


	$comp = Yii::app()->getComponent($id);


	if (!$comp) {


		if ($class) {


			$comp = Yii::createComponent(array('class'=>$class) );


			Yii::app()->setComponent($id,$comp);


		}	


	}


	//debug($comp,'getComponent #comp');


	


	return $comp;


}


Looks cool. Could you explain how to use this component?

How to use it

I make a layout file like this test.php



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/2000/REC-xhtml1-200000126/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" />


	<meta name="language" content="en" />


	<title><?php echo $this->pageTitle; ?></title>


	


	<?php do_action('page_head'); ?>


</head>


<body>


	<?php echo $content; ?>


</body>


</html>


index.php



<?php


function load_styles() {


	$styles = array(


		array('url' => 'themes/default/dasgboard.css','media'=>'all'),


	);	


	


	$output = '';


	foreach ($styles as $style) {


		$output .= CHtml::cssFile($style['url'],$style['media'])."n";


	}


	echo $output;


	debug($output,'load_styles #output');


} 





function load_scripts() {


	$scriptFiles = array(


		'/js/dashboard.js',


	);	


	


	$output = '';


	foreach ($scriptFiles as $scriptFile) {


		$output .= CHtml::scriptFile($scriptFile)."n";


	}


	echo $output;


	debug($output,'load_scripts #output');


} 


add_action('page_head','load_styles',9);


add_action('page_head','load_scripts');


?>


<div>


	<h1>test index</h1>


</div>


functions-test.php



function _print_styles($handles=false){


	do_action( '_print_styles' );


	$styles = array(


		array('url' => 'themes/default/style.css','media'=>'all'),


		array('url' => 'themes/default/color-fresh.css','media'=>'all'),


	);	


	


	$output = '';


	foreach ($styles as $style) {


		$output .= CHtml::cssFile($style['url'],$style['media'])."n";


	}


	echo $output;


	debug($output,'_print_styles #output');


}





function _print_scripts($handles=false){


	do_action( '_print_scripts' );


	$scriptFiles = array(


		'/js/common.js',


	);	


	


	$output = '';


	foreach ($scriptFiles as $scriptFile) {


		$output .= CHtml::scriptFile($scriptFile)."n";


	}


	echo $output;


	debug($output,'_print_scripts #output');


}


default-action.php



add_action('page_head', '_print_styles', 9);	


add_action('page_head', '_print_scripts');	


and i change main program a index.php to like that



....


// create a Web application instance 


$application = Yii::createWebApplication($config_file);


require_once 'default-actions.php';





//- to run application.


$application->run();


Now i can add style sheet file or script into head tag section anywhere.

I not sure, you're understand my idea. I make a example to using the THooking component. may be you have a better idea.

Will appreciate and explanation for this.