Skip to content

Instantly share code, notes, and snippets.

@mbabker
Created October 2, 2012 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbabker/3815676 to your computer and use it in GitHub Desktop.
Save mbabker/3815676 to your computer and use it in GitHub Desktop.
fetchController method for CMS Admin application to find classes using the new MVC paradigm
/**
* Method to get a controller object.
*
* @param string $task The task being executed
*
* @return JController
*
* @since x.y
* @throws RuntimeException
*/
protected function fetchController($task)
{
// Initialise variables
$option = $this->input->get('option');
$task = explode('.', $task);
$cPath = JPATH_ADMINISTRATOR . '/components/' . $option . '/controller/' . $task[1] . '.php';
// First, check if a class exists at the component level
if (is_file($cPath))
{
require_once $cPath;
// Build the class name
$class = ucfirst(substr($option, 4)) . 'Controller' . ucfirst($base[1]);
}
// Check the core library now
elseif (class_exists('JController' . ucfirst($task[1])))
{
$class = 'JController' . ucfirst($task[1]);
}
// We don't have a class, panic.
else
{
// Nothing found. Panic.
throw new RuntimeException('Class for method ' . $task[1] . ' not found', 500);
}
// Define the component paths for legacy MVC use
define('JPATH_COMPONENT', JPATH_BASE . '/components/' . $option);
define('JPATH_COMPONENT_SITE', JPATH_SITE . '/components/' . $option);
define('JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR . '/components/' . $option);
// Load common and local language files.
$lang = JFactory::getLanguage();
$lang->load($option, JPATH_BASE, null, false, false)
|| $lang->load($option, JPATH_COMPONENT, null, false, false)
|| $lang->load($option, JPATH_BASE, $lang->getDefault(), false, false)
|| $lang->load($option, JPATH_COMPONENT, $lang->getDefault(), false, false);
return new $class;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment