在 Joomla 组件开发中使用多个控制器 [英] Using Multiple Controllers in Joomla Component Development

查看:20
本文介绍了在 Joomla 组件开发中使用多个控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的结构在我的组件的根目录中有 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆