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 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序