Hijacking controllers and views in Joomla


Through our plgCommunityExt class what was described in the first article in this series it’s very easy to use controllers and views anywhere you like.

Below is an example of taking advantage of a view and controller of JomSocial:

function getComCtrl($file, $cls){
	require_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'controllers' . DS . "$file.php");
	return new $cls();
}

function getComView($folder, $cls){
	require_once( JPATH_BASE . DS . 'components' . DS . 'com_community' . DS . 'views' . DS . $folder . DS . "view.html.php");
	return new $cls();
}

function fetchProfile(){
	$profile = $this->getComCtrl('profile', 'CommunityProfileController')->profile(true);
	return $this->getComView('profile', 'CommunityViewProfile')->_getProfileHTML($profile->profile);
}

The first two methods are simply helpers to load the chosen file and then instantiate and return the class.

The final fetchProfile will then let us fetch a user’s profile page wherever we want.

Taking advantage of already existing model classes is another good tip, just like this:

function getFriends(){
  	$fm = CFactory::getModel('friends');
  	return $fm->getFriends( $this->getUser()->id );
  }

Here we simply load the model since we have access to all the JomSocial libraries and helpers (remember, we are loaded last in the stack).


Related Posts

Tags: , , , , ,