简体   繁体   中英

Instantiate controller in joomla 3.1.1

I'm trying to instantiate a controller and execute some methods but no result :(

jimport('joomla.application.component.controller');
$controller = JController::getInstance('com_shop');
$controller->my_method($arg1, $arg2);

Any idea?

This won't work try: JControllerLegacy::getInstance('CONTROLLERNAME') assuming that the controller you are calling follows the naming convention

<COMPONENTNAME><Controller><CONTROLLERNAME> for example WeblinksControllerWeblink

following is controller instantiation code taken form lender . And you don't need to use jimport in Joomla 3 extensions. Joomla auto load all classes starting with J prefix.

<?php // No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

//sessions
jimport( 'joomla.session.session' );

//load tables
JTable::addIncludePath(JPATH_COMPONENT.'/tables');

//load classes
JLoader::registerPrefix('Lendr', JPATH_COMPONENT);

//Load plugins
JPluginHelper::importPlugin('lendr');

//application
$app = JFactory::getApplication();

// Require specific controller if requested
if($controller = $app->input->get('controller','default')) {
  require_once (JPATH_COMPONENT.'/controllers/'.$controller.'.php');
}

// Create the controller
$classname  = 'LendrController'.$controller;
$controller = new $classname();

// Perform the Request task
$controller->execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM