joomla组建分析 rsform为例

一、组建入口

开门见山,先来分析以下rsform的入口文件:

<?php
/**
* @version 1.4.0
* @package RSform!Pro 1.4.0
* @copyright (C) 2007-2011 www.rsjoomla.com
* @license GPL, http://www.gnu.org/copyleft/gpl.html
*/

defined('_JEXEC') or die('Restricted access');

$cache =& JFactory::getCache('com_rsform');
$cache->clean();

// Require the base controller   主控制器
require_once(JPATH_COMPONENT.DS.'controller.php');  //包含了组建目录下的controller.php文件
require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_rsform'.DS.'helpers'.DS.'rsform.php');   帮助类

// See if this is a request for a specific controller  看看是否有特殊的控制器
$controller = strtolower(JRequest::getWord('controller'));   
// These are not controllers but legacy functions   没有控制器组,但是有传统功能(这一点目前不是很清楚)
if ($controller == 'functions' || $controller == 'adapter')
	$controller = '';
//这里加了一个判断,如果$controller非空,且com_form/controllers/controller.php文件存在,则执行上面代码,否则执行下面代码,我看了一下目录目录里面似乎没有那个文件
if (!empty($controller) && file_exists(JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php'))
{
	require_once(JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php');
	$controller = 'RSFormController'.$controller;
	$RSFormController = new $controller();
}
else
	$RSFormController = new RSFormController();//也就是说最后执行的是这里,RSFormController()这个类是在com_rsform下面的controller.php文件中定义的。
$RSFormController->execute(JRequest::getWord('task'));//控制器执行任务task

// Redirect if set
$RSFormController->redirect();
?>

下面我们来依次分析一下:

上面的大写的DS是目录分隔符:’/’ ’\’它由框架自动设置,因此开发者不需要关心为不同的操作系统开发不同版本的问题。当使用本地文件时,也应该一直使用’DS’常数。

二、创建控制器

这个的控制器有点长,慢慢来看看

class RSFormController extends JController
{
    var $_db;
    //下面是rsform控制器的构造函数
    function __construct()
    {
        parent::__construct();
        
        $this->_db = JFactory::getDBO();//获取数据库
        
        $view = JRequest::getVar('view', 'rsform');//  获取请求
       
 
 //在这里现在我终于明白了,为什么我设置了在前台查看提交的表单列表显示不出来,原来在这里的view选项就只有rsform,可能是因为这个版本有问题吧,收费版没买呢,现在只是研究用用。
  
                      if ($view == 'rsform')  
            $this->registerDefaultTask('showForm');//注册默认任务
        
        $doc =& JFactory::getDocument();
        $doc->addScript(JURI::root(true).'/components/com_rsform/assets/js/script.js'); //加入了脚本,下一篇打开看看
    }
    
    function captcha()
    {
        。。。。。
    }
    
    function plugin()
    {
        $mainframe =& JFactory::getApplication();
        $mainframe->triggerEvent('rsfp_f_onSwitchTasks');
    }
    
    function showForm()
    {
        ........
    }
    
    function submissionsViewFile()
    {
        ......
    }
    
    function ajaxValidate()
    {
        ......
    }
    
    function confirm()
    {
        ......
    }
}

控制器建立之后,调用控制器执行任务,在URL中定义为:index.php?option=com_hello&task=sometask。若没有设置具体的任务,则执行默认任务'display'。当执行display任务时,’view变量将判断显示什么内容。其它一些常见任务是保存(save)、编辑(edit)、新建(new)…

完成任务(如:'save')后,通常控制器会决定重定向的页面。最后一行语句来确定实际的重定向页面。

本质上讲,主入口点(hello.php)将控制权交给了控制器,控制器来执行在请求中指定的任务。

三、创建视图


获取用来显示的数据并且将其注入到模板。使用JView::assignRef方法将数据注入到模板。(注意:传递给assignRef方法的键值(第一个参数)不能以下划线开头,即$this->assignRef('_greeting',$greeting)。这样做assignRef方法会返回false,并无法将变量值注入到模板中)



视图com_rsform/views/rsform/view.html.php的源代码:


defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view');

class RSFormViewRSForm extends JView
{
    function display( $tpl = null )
    {       
        parent::display($tpl);
    }
}

这似乎就是个框框,没有内容,目前搞不明白。



函数解析:



getVar()返回给定变量       getword()是他的另一个版本,直接收a-z的字符。明白了吧?






You can force the source by setting the $hash parameter(你可以通过设定hash值来强制设定变量来源):

mixed getVar (string $name, [string $default = null], [string $hash = 'default'], [string $type = 'none'], int $mask)

§ string $name变量名

§ string $default变量不存在时的默认值

§ string $hash: Where the var should come from (POST, GET, FILES, COOKIE, METHOD)

§ string $type: Return type for the variable, for valid values see JFilterInput::clean()

§ int $mask过滤器


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值