问:如何获取module参数?如果module参数不存在,如何处理?
答:首先检查$_REQUEST['module'],然后再检查$sugar_config['default_module']是否有设置,如果都没有则用SugarApplication类的实例变量$default_module(默认为Home)。这些在SugarApplication::execute()函数的开头进行处理:

if (!empty($sugar_config['default_module']))
$this->default_module = $sugar_config['default_module'];
$module = $this->default_module;
if (!empty($_REQUEST['module']))
$module = $_REQUEST['module'];

问:获得module后,会去创建控制器类,在哪里找到该类?
答:ControllerFactory::getController($module)函数负责创建控制器类:

function getController($module) {
$class = ucfirst($module).'Controller';
$customClass = 'Custom' . $class;
if (file_exists('custom/modules/'.$module.'/controller.php')){
$customClass = 'Custom' . $class;
require_once('custom/modules/'.$module.'/controller.php');
if (class_exists($customClass)) {
$controller = new $customClass();
} else if (class_exists($class)) {
$controller = new $class();
}
} elseif (file_exists('modules/'.$module.'/controller.php')) {
require_once('modules/'.$module.'/controller.php');
if (class_exists($customClass)) {
$controller = new $customClass();
} else if (class_exists($class)) {
$controller = new $class();
}
} else {
if (file_exists('custom/include/MVC/Controller/SugarController.php')) {
require_once('custom/include/MVC/Controller/SugarController.php');
}
if (class_exists('CustomSugarController')){
$controller = new CustomSugarController();
} else {
$controller = new SugarController();
}
}
//setup the controller
$controller->setup($module);
}

从代码分析,ControllerFactory按照下列顺序寻找控制器类:

  • custom/modules/{module}/controller.php,检查Custom{Module}Controller类是否存在,若不存在,再检查{Module}Controller类是否存在。
  • modules/{module}/controller.php,检查Custom{Module}Controller类是否存在,若不存在,再检查{Module}Controller类是否存在。
  • custom/include/MVC/Controller/SugarController.php,检查CustomSugarController类是否存在,若不存在,再检查SugarController类是否存在。
  • include/MVC/Controller/SugarController.php,检查CustomSugarController类是否存在,若不存在,再检查SugarController类是否存在。

问:如何获取action参数?如果action参数不存在,如何处理?
答:action参数对应到SugarController的$action实例变量,该实例变量默认值为index:

class SugarController{
/**
* The name of the current action.
*/
public $action = 'index';
}

当控制器setup()时调用loadPropertiesFromRequest()获取$_REQUEST中的action参数:

public function setup($module = ''){
......
//set properties on the controller from the $_REQUEST
$this->loadPropertiesFromRequest();
//load the mapping files
$this->loadMappings();
} private function loadPropertiesFromRequest(){
if(!empty($_REQUEST['action']))
$this->action = $_REQUEST['action'];
if(!empty($_REQUEST['record']))
$this->record = $_REQUEST['record'];
if(!empty($_REQUEST['view']))
$this->view = $_REQUEST['view'];
if(!empty($_REQUEST['return_module']))
$this->return_module = $_REQUEST['return_module'];
if(!empty($_REQUEST['return_action']))
$this->return_action = $_REQUEST['return_action'];
if(!empty($_REQUEST['return_id']))
$this->return_id = $_REQUEST['return_id'];
}

也就是说,action参数首选会从$_REQUEST['action']获取,如果没有设置$_REQUEST['action']则使用SugarController的默认值index。

问:SugarController中如何装入Mapping文件?
答:控制器中有一个loadMapping()函数用来装入Mapping文件。该函数在控制器的setup()函数中被调用:

public function setup($module = ''){
......
//load the mapping files
$this->loadMappings();
} private function loadMappings(){
$this->loadMapping('action_view_map');
$this->loadMapping('action_file_map');
$this->loadMapping('action_remap', true);
}

控制器允许会从多个地方寻找Mapping文件:

private function loadMapping($var, $merge = false){
if ($merge && !empty($this->$var)){
$$var = $this->$var;
} else {
$$var = array();
}
if(file_exists('include/MVC/Controller/'. $var . '.php')){
require('include/MVC/Controller/'. $var . '.php');
}
if(file_exists('modules/'.$this->module.'/'. $var . '.php')){
require('modules/'.$this->module.'/'. $var . '.php');
}
if(file_exists('custom/modules/'.$this->module.'/'. $var . '.php')){
require('custom/modules/'.$this->module.'/'. $var . '.php');
}
if(file_exists('custom/include/MVC/Controller/'. $var . '.php')){
require('custom/include/MVC/Controller/'. $var . '.php');
} // entry_point_registry -> EntryPointRegistry
$varname = str_replace(" ","",ucwords(str_replace("_"," ", $var)));
if(file_exists("custom/application/Ext/$varname/$var.ext.php")){
require("custom/application/Ext/$varname/$var.ext.php");
}
if(file_exists("custom/modules/{$this->module}/Ext/$varname/$var.ext.php")){
require("custom/modules/{$this->module}/Ext/$varname/$var.ext.php");
} $this->$var = $$var;
}

loadMapping()函数在最后会将装入的Mapping映射为控制器的实例变量。

问:控制器中如何实现URL重定向?
答:SugarController中定义了一个实例变量redirect_url:

class SugarController {
/**
* url to redirect to
*/
public $redirect_url = '';
}

在Action方法中设置$redirect_url就可以实现URL重定向。例如:

function action_redirect() {
$this->redirect_url = 'http://www.baidu.com/';
|

这是因为SugarController使用execute()函数处理请求,在该函数末尾有检查$redirect_url是否有设值。若有设值,则调用SugarApplication::redirect()跳转:

final public function execute() {
try
{
$this->process();
if(!empty($this->view))
{
$this->processView();
}
elseif(!empty($this->redirect_url))
{
$this->redirect();
}
}
catch (Exception $e)
{
$this->handleException($e);
}
} protected function redirect(){
if (!empty($this->redirect_url))
SugarApplication::redirect($this->redirect_url);
}

如果不在控制器中要实现URL重定向,就应该用SugarApplication::redirect()函数。

问:控制器如何自动读取Bean?

答:如果请求参数中有record存在,则SugarController会自动读取后台数据到实例变量$bean中。首先控制器要从$_REQUEST获取record参数到$this->record:

public function setup($module = ''){
......
//set properties on the controller from the $_REQUEST
$this->loadPropertiesFromRequest();
} private function loadPropertiesFromRequest(){
......
if(!empty($_REQUEST['record']))
$this->record = $_REQUEST['record'];
}

然后,控制器在处理请求时通过在process()函数中调用loadBean()实现读取Bean:

public function process(){
......
$this->loadBean();
} public function loadBean()
{
if(!empty($GLOBALS['beanList'][$this->module])){
$class = $GLOBALS['beanList'][$this->module];
if(!empty($GLOBALS['beanFiles'][$class])){
require_once($GLOBALS['beanFiles'][$class]);
$this->bean = new $class();
if(!empty($this->record)){
$this->bean->retrieve($this->record);
if($this->bean)
$GLOBALS['FOCUS'] = $this->bean;
}
}
}
}

从loadBean()代码可以发现,要自动装入Bean需要定义$GLOBALS['beanList']和$GLOBALS['beanFiles']。默认的定义在include/modules.php:

// index.php
// require_once('include/entryPoint.php')
// require_once('include/modules.php'); $beanList['Leads'] = 'Lead';
$beanFiles['Lead'] = 'modules/Leads/Lead.php';

问:控制器如何处理action_remap?
问:控制器如何处理action_file_map和action_view_map?
问:控制器如何检查用户请求的权限?例如谁人可以执行数据库的备份和恢复处理?

SugarCE问题点记录的更多相关文章

  1. 记一次debug记录:Uncaught SyntaxError: Unexpected token ILLEGAL

    在使用FIS3搭建项目的时候,遇到了一些问题,这里记录下. 这里是发布搭建代码: // 代码发布时 fis.media('qa') .match('*.{js,css,png}', { useHash ...

  2. nginx配置反向代理或跳转出现400问题处理记录

    午休完上班后,同事说测试站点访问接口出现400 Bad Request  Request Header Or Cookie Too Large提示,心想还好是测试服务器出现问题,影响不大,不过也赶紧上 ...

  3. Kali对wifi的破解记录

    好记性不如烂笔头,记录一下. 我是在淘宝买的拓实N87,Kali可以识别,还行. 操作系统:Kali 开始吧. 查看一下网卡的接口.命令如下 airmon-ng 可以看出接口名称是wlan0mon. ...

  4. 2015 西雅图微软总部MVP峰会记录

    2015 西雅图微软总部MVP峰会记录 今年决定参加微软MVP全球峰会,在出发之前本人就已经写这篇博客,希望将本次会议原汁原味奉献给大家 因为这次是本人第一次写会议记录,写得不好的地方希望各位园友见谅 ...

  5. 分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间)

    分享一个SQLSERVER脚本(计算数据库中各个表的数据量和每行记录所占用空间) 很多时候我们都需要计算数据库中各个表的数据量和每行记录所占用空间 这里共享一个脚本 CREATE TABLE #tab ...

  6. 我是如何在SQLServer中处理每天四亿三千万记录的

    首先声明,我只是个程序员,不是专业的DBA,以下这篇文章是从一个问题的解决过程去写的,而不是一开始就给大家一个正确的结果,如果文中有不对的地方,请各位数据库大牛给予指正,以便我能够更好的处理此次业务. ...

  7. 前端学HTTP之日志记录

    前面的话 几乎所有的服务器和代理都会记录下它们所处理的HTTP事务摘要.这么做出于一系列的原因:跟踪使用情况.安全性.计费.错误检测等等.本文将谥介绍日志记录 记录内容 大多数情况下,日志的记录出于两 ...

  8. ASP.NET Core应用中如何记录和查看日志

    日志记录不仅对于我们开发的应用,还是对于ASP.NET Core框架功能都是一项非常重要的功能特性.我们知道ASP.NET Core使用的是一个极具扩展性的日志系统,该系统由Logger.Logger ...

  9. python+uwsgi导致redis无法长链接引起性能下降问题记录

    今天在部署python代码到预生产环境时,web站老是出现redis链接未初始化,无法连接到服务的提示,比对了一下开发环境与测试环境代码,完全一致,然后就是查看各种日志,排查了半天也没有查明是什么原因 ...

随机推荐

  1. Xamarin Android项目真机测试闪退

    Xamarin Android项目真机测试闪退 项目在模拟器中运行正常,但在真机上闪退.这是由于项目设置使用共享的Mono运行时.Visual Studio中,在项目属性-Android Option ...

  2. Extjs MVC模式

    最近在学习Extjs,发现他可以使用MVC模式,不但可以组织代码,而且可以 减少实现的内容,模型(Models)和控制器(Controllers)也被引入其中. Model模型是字段和它们的数据的集合 ...

  3. 初识C#设计模式

    利用设计模式可以使我们的代码更灵活,更容易扩展,更容易维护.各种面向对象的程序设计语言都提供了基本相同的机制:比如类.继承.派生.多态等等.但是又有各自的特色,C# 中的反射机制便是一个很重要的工具, ...

  4. [BZOJ 1058] 报表统计

    Link: BZOJ 1058 传送门 Solution: 为了这道题今天下午一直都在和常数大战…… 1.对于询问1,我们记录每个数末位置的数$T[i]$和初始位置$S[i]$ 用平衡树维护所有差值, ...

  5. 【Floyd】POJ2139 -Six Degrees of Cowvin Bacon

    普通的Floyd了分分秒可以水过,结果在submit前删调试段落的时候把程序本体给删了吃了两个WA…… #include<iostream> #include<cstring> ...

  6. 【bugku】【RE】file WriteUp

    FILE 查壳后显示无壳,拖进IDA: 运行程序时传递的第一个参数为一个文件名,并打开该文件.下面点进去encode函数分析一下: for循环中每三个为一组,进行Base64的操作,接下来两个if分别 ...

  7. 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) C. XOR Equation 数学

    C. XOR Equation 题目连接: http://www.codeforces.com/contest/635/problem/C Description Two positive integ ...

  8. hdu 5236 Article 概率dp

    Article Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5236 ...

  9. Delphi XE10下用FireDAC与SQLite连接要注意的问题 转

      Delphi在XE的版本上,已经实现了安卓与苹果的移动跨平台,因此只需要一份代码,就可以统领两种手机平台,确实是一种高效的做法和节约的策略. 用Delphi XE7连接SQLite,主流使用Fir ...

  10. python笔记4-遍历文件夹目录os.walk()

    前言 如何遍历查找出某个文件夹内所有的子文件呢?并且找出某个后缀的所有文件 walk功能简介 1.os.walk() 方法用于通过在目录树种游走输出在目录中的文件名,向上或者向下. 2.walk()方 ...