`
ljl_xyf
  • 浏览: 615023 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

joomla1.5应用小结

阅读更多

这些有工作时的小结,还有在万源之源上摘录下来的。

----------路径------------------

JPath is used to get the directory path.

DS -- is used as a directory separator.

JPATH_ADMINISTRATOR-- Sets the path to /Joomla/administrator

JPATH_BASE -- sets the entry to joomla /Joomla

JPATH_CACHE --Sets the cache path /Joomla/cache

JPATH_COMPONENT -- Sets the component path /Joomla/components/com_example

JPATH_COMPONENT_ADMINISTRATOR -- sets the backend component path /Joomla/administrator/components/com_example

JPATH_COMPONENT_SITE-- sets the frontend component path /Joomla/components/com_example

JPATH_CONFIGURATION --sets the configuration path

---------返回浏览器的根路径-----

 

       $temp =& JFactory::getURI();

        $tem = $temp->root();

        echo $tem;

----------JFactory------------------

getACL         ---返回一个指向JAuthorization对象的标识符,如果它不存在就创建一个。

getCache       ---返回一个指向JCache对象的标识符

getConfig      ---返回一个指向JRegistry对象的标识符

getDBO         ---返回一个指教JDatabase对象的标识符

getDoucment    ---返回一个指向JDocument对象的标识符

getEditor      ---如果安装了editor插件,得到一个editor的对象

getLanguage    ---返回一个指向JLanguage对象的标识符

getMailer      ---返回一个指向JMail对象的标识符

getSession     ---返回一个指向Jsession对象的标识符

getTemplate    ---返回一个指向JTemplate对象的标识符

getURI         ---返回一个指向JURI对象的标识符

getUser        ---返回一个指向JUser对象的标识符

getXMLParser   ---得到XML文件解析器

getApplication ---返回一个指向JApplication对象的标识符

----------JRequest------------------

getBool        ---取指令和回归特定被过滤的变量,bool过滤器只将退回真实或错误bool值。??

getCmd         ---取指令和回归特定被过滤的变量,cmd过滤器只允许字符[A-Za-z0-9。- _]。

getFloat       ---取指令和回归特定被过滤的变量 ,浮点过滤器只允许数字和期间?

getInt         ---取指令和回归特定被过滤的变量 ,整数过滤器将允许仅数字返回。

getMethod      ---得到请求方法.

getString      ---取指令和回归特定被过滤的变量 , 串过滤器删除‘非法’ HTML代码,?

getURI         ---得到请求路径?

getVar           ---取指令和回归特定变量.

getWord        ---取指令和回归特定被过滤的变量,cmd过滤器只允许字符[A-Za-z]

setVar         ---设置请求变量.

_cleanArray    ---增加一个数组到GLOBALS数组并且检查GLOBALS变量没有被攻击

_cleanVar      ---清除输入变量

----------简单用法------------------

1.在文章中引入module的方法:

        {loadposition xx}                     'xx'是渲染模块的地方(位置)

2.在module和compoment的代码中调用module的方法 :

        $modules = JModuleHelper::getModules("xx");

        JModuleHelper::renderModule($modules[0]);

3.判断是不是首页的方法:

        $menu = & JSite::getMenu();

        if($menu->getActive() != $menu->getDefault())            echo "这不是首页";

        else            echo "这是首页";

4.设置html基本参数的方法:

        $document =& JFactory::getDocument();

        $document->setTitle(*****); //设置html标题

        $document->setDescription(*****); //添加 meta

        $document->addStyleSheet(*****) //添加CSS文件

        $document->addScript(*****) //添加js脚本

        $document->setLanguage(***)  //设置语言文件

5.JDatabase 用法

        $db =& $JFactory::getDBO();

        这是数据库connector类,封装了与数据库操作的一系列操作。目前有两个子类,JDatabaseMysql,JDatabaseMysqli,这个类为以后扩展到其他数据库留出了接口。

        关于一些具体函数没有什么特殊的,基本对应mysql的函数的一些特征,对于这个类应用基本都是首先 setquery ,然后load

        或者直接执行 executequery ,主要还是不要混淆load开头的几个函数的具体功能:

        a、loadObject 以对象的形式返回结果集的第一行数据

        b、loadObjectList 对应上一个函数,返回一个对象的集合

        c、loadResult 返回第一行的第一个字段或者NULL

        d、loadResultArray 返回某一字段的数据到一个数组中

        e、loadRow 返回第一行数据,应该是数组形式

        f、loadRowList 对应上一个函数,返回行数据的集合

        g、query 无返回值的执行sql。

        还有一个函数很重要就是 setUTF ,这个函数决定了数据库能显示中文。

6.JUser类

        此类可以初始化用户信息,同时可以得到用户的信息。如:得到当前登陆用户的实例:

        $user =& JFactory::getUser();

        $userId = (int)$user->get('id');

        此外还要了解一些关于 JUserHelper 类的的用法,如获得随机码及密码加密的一些方法:直接引入该类的方法: jimport('joomla.user.helper');

7.JPath JFile Jfolder类

        1.创建和删除目录:

                if(JFolder::exists($targetpath)) JFolder::delete($targetpath);

                JFolder::create($targetpath);

                Jfolder::create($targetpath.DS.'tmpl');

        2.文件的拷贝:

                $sfile = $sourcepath.DS.'index.html';

                $tfile = $targetpath.DS."index.html";

                JFile::copy($sfile,$tfile);

                $files[]=$tfile;

        3.文件的读取和写入

                $sfile = $sourcepath.DS.'helper.php';

                $data = JFile::read($sfile);

                $tfile = $targetpath.'/helper.php';

                JFile::write($tfile,$data);

                $files[] = $tfile;

                unset($data);

8.这两个类是后台管理过程经常用到的。

        1.添加三个按钮:添加,删除,修改

            JToolBarHelper::title( JText::_('{{component}} Manager'),'generic.png');

            JToolBarHelper::deleteList();

            JToolBarHelper::editListX();

            JToolBarHelper::addNewX();

        2.其他

            preview    //预览

            publish    //发布

            cancel         //取消

9.组件是如何被渲染的

        在描述 /index.php的时候,我们看到根据option参数,$mainframework->dispatch(),就进入了组件的调用并渲染的过程,我们来看看JSite 的dispatch都做了什么工作。

    dispatch 最关键的是这几句话:

    $document->setTitle( $params->get('page_title') );   //设置标题

  $document->setDescription( $params->get('page_description') );  //设置meta

  $contents = JComponentHelper::renderComponent($component);

  $document->setBuffer( $contents, 'component');

    可以看到最为关键的是 JComponentHelper::renderComponent($component);

    再看看这一行程序完成了那些工作

  $task = JRequest::getString( 'task' );

  // Build the component path

  $name = preg_replace('/[^A-Z0-9_\.-]/i', '', $name);

  $file = substr( $name, 4 );

  // Define component path

  define( 'JPATH_COMPONENT',     JPATH_BASE.DS.'components'.DS.$name);

  define( 'JPATH_COMPONENT_SITE',    JPATH_SITE.DS.'components'.DS.$name);

  define( 'JPATH_COMPONENT_ADMINISTRATOR', JPATH_ADMINISTRATOR.DS.'components'.DS.$name);

  // get component path

  if ( $mainframe->isAdmin() && file_exists(JPATH_COMPONENT.DS.'admin.'.$file.'.php') ) {

   $path = JPATH_COMPONENT.DS.'admin.'.$file.'.php';

  } else {

   $path = JPATH_COMPONENT.DS.$file.'.php';

  }

        这部分实际上确定了那个compoent下的组件文件被引入,并取得了task,中间一部分兼容代码就不看了

        我们来看关键代码:

  ob_start();

  require_once $path;

  $contents = ob_get_contents();

  ob_end_clean();

        这部分代码就是包含了组件的开始文件,而这个文件,我们在组件开发的时候用到的。这个文件引入了controller 文件,并根据task决定进入那个分支。

        再深入下去就是组件的整个生成过程,以后再看了。

10.JTable是什么

        JTable是数据库中数据表在程序中的表达,不知道这句话怎么说,其实JTable更对应着表中的一行,以及相应的操作。Joomla中的 JTable**对应中数据库中 **表,我们在使用的时候要针对我们自己所使用的表扩展自己的JTable.我们需要关注的是JTable的函数checkin,checkout ,着两个函数对更新的数据进行合法性检查,我个人觉得对于数据完整性的检查应该放在Jtable的check中。

        Jtable 比较常用的函数,看名字就明白了,记住几个吧:

        delete,store,bind,load,setError等,具体还是需要用的时候看看源代码吧。

11.JModel是什么

        我们经常提到MVC模式,JModel在Joomla的MVC组件中是重要的一个环节,JModel是MVC中的数据视图层,我们需要明白的是 JModel不同于JTable,数据视图是由一个或者几个table构成,或者多条数据记录构成的数据集合,以及数据集合的相关操作,对于JModel 我们不必了解太多的具体函数,在组件开发过程中,通常都要继承JModel,在子类中完成数据集合的生成以及相关的操作,保存,删除。

        我个人倾向对于几个表之间的数据完整性,要在JModel中验证,而对于单一表的数据完整性要通过JTable check函数完成。

        同事对于那些有逻辑操作的验证则最好在MVC的 controller层完成。

12.Jview

        MVC模式中,重要的一环,JView 和 tmpl目录中的模板,共同决定了,页面html的代码,Jview是在Jmodel和template之间的桥梁。我们扩展做自己的组件,都需要扩展Jview的子类。这个类其实需要看看它的变量和函数也就理解:

        跟数据相关的部分:

        _defaultModel  默认的model ,可以通过 setModel 进行设置。同时function &get 可以从指定的model调用函数返回相应的数据

        _models 存贮model的数组,getModel,可以从中返回指定的Model

         assign assignref,数据赋值函数,这两个函数的任务是赋值变量给模板。

        跟模板相关部分:

        loadTemplate,setLayout,setLayoutExt 看名字就知道了

        还有一个函数:display  ,大部分的view子类都要继承这个。

13.JController

        同样 JController 是MVC中重要的起点,正式这个类决定的动作的下一步流向,我们来看看表格提交数据的典型的controller的代码:

         function edit()

         {

          JRequest::setVar( 'view', 'hello' );

          JRequest::setVar( 'layout', 'form'  );

          JRequest::setVar('hidemainmenu', 1);

          parent::display();

         }

        

         function save()

         {

          $model = $this->getModel('hello');

          if ($model->store($post)) {

           $msg = JText::_( 'Greeting Saved!' );

          } else {

           $msg = JText::_( 'Error Saving Greeting' );

          }

          // Check the table in so it can be edited.... we are done with it anyway

          $link = 'index.php?option=com_hello';

          $this->setRedirect($link, $msg);

         }

        

         function remove()

         {

          $model = $this->getModel('hello');

          if(!$model->delete()) {

           $msg = JText::_( 'Error: One or More Greetings Could not be Deleted' );

          } else {

           $msg = JText::_( 'Greeting(s) Deleted' );

          }

        

          $this->setRedirect( 'index.php?option=com_hello', $msg );

         }

        

         function cancel()

         {

          $msg = JText::_( 'Operation Cancelled' );

          $this->setRedirect( 'index.php?option=com_hello', $msg );

         }

        实际上 controller 跟提交的task参数,调用controller中的不同的函数,当然默认会调用display ,我觉得还需要记住的就是

        getModel ,和setRedirect ,其余函数用到再看就可以了。

14.MVC组件的执行

        以前的文章中,我们曾经说过 $mainframework->dispatch 是如何最终调用组件的,通过这个dispatch,最终 include 相应组件目录下的 组件名称.php 文件,现在我们来看看,这个文件是怎么按部就班的联系了MVC模式相关的各个文件。

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

        // Require specific controller if requested

        if($controller = JRequest::getVar('controller')) {

         require_once (JPATH_COMPONENT.DS.'controllers'.DS.$controller.'.php');

        }

        // Create the controller

        $classname = 'HelloController'.$controller;

        $controller = new $classname( );

        // Perform the Request task

        $controller->execute( JRequest::getVar('task'));

        // Redirect if set by the controller

        $controller->redirect();

         其实就是根据request提交的controller参数,创建相应的JController对象,然后由controoler对象执行相应的任务。

        这样我们就完全理解了,一个组件是如何被调用,MVC组件是如何执行,并最后返回html代码的。

15.模块是如何被执行并渲染?

        以前的文章中,关于/index.php我们已经分析完了 $mainframe->dispatch()是引入了组件,并被执行。我们知道对于Joomla,一个页面只能有一个或者0个组件,而上,下左右的碎片都是module,module是页面丰富的有效补充。比如我们知道菜单是  mod_mainmenu,而footer是mod_footer等等,那么这些module是怎么被引入的,并最后执行的?

        秘密都在$mainframe->render()这个函数上,我们看看这个函数都做了什么工作。

        以下是JSite 的render 函数的内容

          $document =& JFactory::getDocument();

          $user     =& JFactory::getUser();

          // get the format to render

          $format = $document->getType();

          switch($format)

          {

           case 'feed' :

           {

            $params = array();

           } break;

           case 'html' :

           default     :

           {

            $template = $this->getTemplate();

            $file   = JRequest::getCmd('tmpl', 'index');

        

            if ($this->getCfg('offline') && $user->get('gid') < '23' ) {

             $file = 'offline';

            }

            if (!is_dir( JPATH_THEMES.DS.$template ) && !$this->getCfg('offline')) {

             $file = 'component';

            }

            $params = array(

             'template'  => $template,

             'file'  => $file.'.php',

             'directory' => JPATH_THEMES

            );

           } break;

           }

          $data = $document->render( $this->getCfg('caching'), $params);

          JResponse::setBody($data);

        其实重要的部分是引入了相应的模板文件(templateindex.php),并调用了 JDocumentHtml的 render 函数。

        看到这里,我们终于明白了,模板的index.php原来是这个时候被引入的。

        我们再看看 JDocumentHtml 的render函数。

        这个函数中最重要的两句程序是

          $data = $this->_loadTemplate($directory.DS.$template, $file); 载入模板文件

          $data = $this->_parseTemplate($data); 解析模板

        再继续看看解析模板是什么过程:

          $replace = array();

          $matches = array();

          if(preg_match_all('##iU', $data, $matches))

          {

           $matches[0] = array_reverse($matches[0]);

           $matches[1] = array_reverse($matches[1]);

           $matches[2] = array_reverse($matches[2]);

           $count = count($matches[1]);

           for($i = 0; $i < $count; $i++)

           {

            $attribs = JUtility::parseAttributes( $matches[2][$i] );

            $type  = $matches[1][$i];

            $name  = isset($attribs['name']) ? $attribs['name'] : null;

            $replace[$i] = $this->getBuffer($type, $name, $attribs);

           }

           $data = str_replace($matches[0], $replace, $data);

          }

          return $data;

         }

        对了,就是这部分,对模板中 JDOC标签进行了解析,获得了相应的module名称和参数,并调用getBuffer函数执行。

        至此 调用 $renderer->render($name, $attribs, $result);

        上一文章中提到了getBuffer函数,在函数中实际上调用了render ,这个对象是JDocumentRendererModule 类的实例,我们看看JDocumentRendererModule 的render函数。

          if (!is_object($module))

          {

           $module =& JModuleHelper::getModule($module);

           if (!is_object($module))

           {

            if (is_null($content)) {

             return '';

            } else {

            

             $tmp = $module;

             $module = new stdClass();

             $module->params = null;

             $module->module = $tmp;

             $module->id = 0;

             $module->user = 0;

            }

           }

          }

          // get the user and configuration object

          $user =& JFactory::getUser();

          $conf =& JFactory::getConfig();

          // set the module content

          if (!is_null($content)) {

           $module->content = $content;

          }

          //get module parameters

          $mod_params = new JParameter( $module->params );

          $contents = '';

          if ($mod_params->get('cache', 0) && $conf->getValue( 'config.caching' ))

          {

           $cache =& JFactory::getCache( $module->module );

           $cache->setLifeTime( $mod_params->get( 'cache_time', $conf->getValue( 'config.cachetime' ) * 60 ) );

           $cache->setCacheValidation(true);

           $contents =  $cache->get( array('JModuleHelper', 'renderModule'), array( $module, $params ), $module->id. $user->get('aid', 0) );

          } else {

           $contents = JModuleHelper::renderModule($module, $params);

          }

        这段代码完成了找到对应的module ,和helper文件,兑取参数,并最后由'JModuleHelper'执行,并渲染。

        至此,我们也完全了解了模板是如何被调用,模块是如何并调用并渲染的
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics