Joomla!组件如何工作基础知识

https://docs.joomla.org/Absolute_Basics_of_How_a_Component_Functions


1、进入平台

平台的入口是index.php。joomla!的设计就是主要来推送组件文件的结果的。当访问链接index.php?option=com_<name>时,

jooma!平台载入文件:components/com_<name>/<name>.php。所以,如果你的组件叫 'com_read' ,那么,

你应该有个文件夹 'com_read',和一个其中的文件'read.php',称之为基础文件。在这个文件里,你可以决定

用那种形式,老式的还是MVC模式。


MVC模型用2条腿走路:文件和类。Joomla!平台先去找文件,找到好再去找类。都找不到就出错了。


2. controller.php文件

所有的一些从在base file中引用controller文件开始。

In your base file (<name>.php), the following code is typical:

require_once(JPATH_COMPONENT . '/controller.php');

controller可以在任何地方。如果用上面的写法的话,就必须和base file在同一目录下。因为JPATH_COMPONENT

是base file所在的的路径。

如何使用controller.php


// Get an instance of the controller prefixed by <name>
$controller = JControllerLegacy::getInstance('<Name>');
 
// Perform the Request task
$controller->execute(JFactory::getApplication()->input->getCmd('task'));
 
// Redirect if set by the controller
$controller->redirect();


之后,平台就开始自动的载入文件、调用类,所以,必须非常小心的文件的位置、名字、类名,一个字母

不对都会出错。


3、获取数据

在哪获取数据?Where does the 'task' in the execute call come from? Do you really have a meaningful 'task' variable?

默认执行display task


class <Name>Controller extends JControllerLegacy
 
{
  function display()
  {
    echo 'displaying';
  }
}


如果有一个task=jump,就执行jump

class <Name>Controller extends JControllerLegacy
{
  function jump()
  {
    echo 'jumping';
  }
}


4、视图

不一定非得写task的参数,但最好写,因为系统很特殊的对待他

Joomla中用views文件夹保存不同

The Joomla! Platform includes a file named view.html.php that should exist in your view folder.


When you built your request, you included a variable named 'view' that tells the MVC model what view you want. Or, if you did not include it, you need to include it now because there is no such a thing as a default view. So your URL is something like:


http://example.com/index.php?option=com_<name>&view=<myview>[&task=<mytask>]


With this URL Joomla! is importing a file located at <site root dir>/components/<name>/views/<myview>/view.html.php.

// Get an instance of the controller prefixed by <name>
$controller = JControllerLegacy::getInstance('<Name>');
 
// Perform the Request task
$controller->execute(JFactory::getApplication()->input->getCmd('task'));
 
// Redirect if set by the controller
$controller->redirect();

class <Name>Controller extends JControllerLegacy
 
{
  function display()
  {
    echo 'displaying';
  }
}

class <Name>Controller extends JControllerLegacy
{
  function jump()
  {
    echo 'jumping';
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值