donglei1616 2013-09-06 11:55
浏览 123
已采纳

Joomla Protostar模板中的PHP代码可以解释这一部分

I am learning Joomla template development by studying and reverse engineering the default Joomla Protostar template. I understand about 80% of what is going on and through learning the basics of php I have been able to replicate it. However, I do not understand what the following chunk of code is doing (it is right at the top of the template after defined('_JEXEC') or die;

// Getting params from template
$params = JFactory::getApplication()->getTemplate(true)->params;

$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$this->language = $doc->language;
$this->direction = $doc->direction;

 // Detecting Active Variables
$option   = $app->input->getCmd('option', '');
$view     = $app->input->getCmd('view', '');
$layout   = $app->input->getCmd('layout', '');
$task     = $app->input->getCmd('task', '');
$itemid   = $app->input->getCmd('Itemid', '');
$sitename = $app->getCfg('sitename');

if($task == "edit" || $layout == "form" )
 {
$fullWidth = 1;
}
else
{
$fullWidth = 0;
}

A few things I dont get are...

  1. What are these variables such as "$task" since I dont see that they are set as a parameter

  2. Why arent these variables just included like the others and what is $app->input all about?

  • 写回答

1条回答 默认 最新

  • duanlu7223 2013-09-06 12:01
    关注
    $option   = $app->input->getCmd('option', '');
    $view     = $app->input->getCmd('view', '');
    $layout   = $app->input->getCmd('layout', '');
    $task     = $app->input->getCmd('task', '');
    $itemid   = $app->input->getCmd('Itemid', '');
    

    These calls look through the various global variables $_POST,$_GET,$_SESSION etc to see if those variables are defined ($_POST['task'], $_GET['task'], etc). If none exist return the default (the second argument).

    $app->input is an object of joomla's input class (like JRequest, JInput or similar). That class is the one that does the actual work of looking through the global variables. You could change out getCmd for like getString, but getCmd might do other filtering that getString does not. You would have to look at the core files (request.php,input.php etc) to see what it actually does.

    you can do the same thing like below:

    $input = new JInput();
    //get option or if none use the default 'com_users'
    $option = $input->getString("option","com_users"); 
    

    EDIT As to your question about MVC:

    No this is not part of the MVC. This is just joomla providing an easy way of filtering incoming data.

    MVC is the abstraction of data(Model), View, and Controller parts of an application. You can see this in the components. Look at the com_users component or any of other components that come with joomla.

    You will see that the models manipulate the data, the view only prints out the data (and the correct html,json what have you for that view), while the control just controls the flow.

    Each part should only do the work that deals with its area. For instance the code for the View should not be doing database calls, getting data from a web service, etc. This is the job of the Model. The View should only get the data from the Model, which should be in the format that the view needs, and then print it out.

    This is not to say YOU CAN'T do another parts work in a different part, it's just that you should let/build each part do the work its supposed to do.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错