在Joomla组件开发中使用多个控制器 [英] Using Multiple Controllers in Joomla Component Development
问题描述
我的结构在我的组件的根目录中有component.php.我正在使用 http://www.notwebdesign.com/joomla-component -creator/j15/index.php 我感觉需要多个控制器来编写更清晰的代码,因为我要运行大约12个任务.我正在使用多个模型,并且需要多个控制器.
My structure has component.php in the root of my component. I am using the http://www.notwebdesign.com/joomla-component-creator/j15/index.php I feel the need to have multiple controllers to have cleaner code as I have around 12 tasks to be run. I am using multiple models and need to have multiple controllers.
谁能指出我正确的方向?示例代码受到高度赞赏.
Anyone who can point me in the right direction? Sample code highly appreciated.
推荐答案
您需要在组件的主目录中创建一个文件夹(例如,components/com_mycom/controllers/...)
You need to make a folder in your component's main directory (e.g. components/com_mycom/controllers/...)
然后,我组件的主文件(使用上面的示例将其称为"mycom.php")具有如下代码:
Then the main file of my component (it should be called "mycom.php" using the example from above) has code that looks like this:
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
// load a constants file for use throughout the component
require_once(JPATH_COMPONENT.DS.'lib'.DS.'constants.php');
// fetch the view
$view = JRequest::getVar( 'view' , 'null' );
// use the view to fetch the right controller
require_once( JPATH_COMPONENT.DS.'controllers'.DS.$view.'.php' );
// initiate the contoller class and execute the controller
$controllerClass = 'MycomController'.ucfirst($view);
$controller = new $controllerClass;
// call the display function in the controller by default - add a task param to the url to call another function in the controller
$controller->execute( JRequest::getVar( 'task', 'display' ) );
$controller->redirect();
然后,对于控制器目录中的控制器,您的代码将是正常的,例如
Then for your controllers in your controllers directory your code would be as normal e.g.
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
class MycomControllerAdd extends JController
{
function display() {
$viewLayout = JRequest::getVar( 'tmpl', 'add' );
$view = &$this->getView( 'add', 'html' );
$model = &$this->getModel( 'add' );
$view->setModel( $model, true );
$view->setLayout( $viewLayout );
$view->addStuff();
}
...
可以调用的网址如下所示:
A url that would call this would look like so:
http://somedomain.com/index.php?option=com_mycom&view=add
这篇关于在Joomla组件开发中使用多个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!