2009-07
26

注:本文使用的CakePHP版本为 1.2.3.8166。

使用CakePHP开发时,免不了要将调试级别设置为2, 这样页面上会显示出执行的SQL语句及结果,很方便。 但在做API页面时就比较麻烦。一般API页面的输出结果是XML或者JSON格式, 如果后面多了些调试信息,客户端就无法正确解析了。

解决方法很简单,只要在API的action函数中改写调试等级即可:

function api_index() {
  ...
  Configure::write('debug', 0);
}

不过,每个action都要写这么一行,太麻烦了。能不能想个办法,自动地关闭所有API页面的调试信息?

可以仿照管理界面的方式。管理界面的URL为 /admin/posts/index, 这样就可以自动调用 PostsController的 admin_index() 函数。 类似地,我们可以把所有 API 的URL都写成 /api/posts/index, 让CakePHP自动调用 PostsController 的 api_index() 函数。 只需在 config/routes.php 中添加下面一行:

Router::connect('/api/:controller/:action/*', array('prefix' => 'api', 'api' => true));

简单说明一下:上述路径中的 :controller 和 :action 为系统默认的路径元素, 只需写在适当的位置,CakePHP即可识别出Controller和Action。 prefix选项指定action函数的前缀为API,这样系统就会调用 api_index()函数。 最后的 'api' => true 是个标志,加上以后,传给 action 的 $this->params 数组中 就会出现 'api' => true 这个值,稍后我们会用到它。

接下来新建 /api_controller.php 文件,如果已存在,就修改一下:

<?php
class AppController extends Controller {

  function beforeFilter() {
    // 如果是API请求,则关闭调试信息
    if (isset($this->params['api'])) {
      $this->layout = 'api';
      $debug = Configure::read('debug');
      if ($debug > 0) {
        Configure::write('debug', 0);
      }
    }
  }
}
?>

使用beforeFilter()函数,在请求到达真正的action函数之前,判断是否为API请求, 如果是,就将页面布局设置为 'api',再关闭调试信息。

当然,别忘了建立 /pages/layouts/api.ctp 文件,只需一行:

<?php echo $content_for_layout ?>

这样,以后访问API时只要写成 /api/controller/action 即可,系统会自动关闭调试信息, 并自动应用API的页面模板。



这篇文章有 4 条评论了,快来一起讨论讨论吧!
#2
bs hao
2009-08-12 12:47

http://www.phpgz.com/html/framework/kcakephp/index.html

参照一下这个cakephp的文章 .

#3
jinhr
2009-09-12 01:17

思想上没错。我在自己的项目中,处理ajax请求时,是这样做的,利用了RequestHandler这个Component.

class AppController extends Controller
{
/*** Components ***/

var $components = array(’RequestHandler’);

function beforeFilter()
{
$this->_checkAjax();
}

/** check if ajax request, disable debug
* and cache
* @return unknown_type
*/
function _checkAjax()
{
if ($this->RequestHandler->isAjax())
{
Configure::write(’debug’, 0);
$this->disableCache();
}
return true;
}

/* others …… */

#4
jinhr
2009-09-12 01:18

思想上没错。我在自己的项目中,处理ajax请求时,是这样做的,利用了RequestHandler这个Component.

class AppController extends Controller
{
/*** Components ***/

var $components = array(’RequestHandler’);

function beforeFilter()
{
$this->_checkAjax();
}

/** check if ajax request, disable debug
* and cache
* @return unknown_type
*/
function _checkAjax()
{
if ($this->RequestHandler->isAjax())
{
Configure::write(’debug’, 0);
$this->disableCache();
}
return true;
}

/* others …… */


注意旧版的IE中要它发起ajax请求去拿最新数据,可浏览器总是拿过时的cache中的信息,所以只好在服务器端禁止浏览器的缓存.
我把代码写在/app/app_controller.php中了,所以所有controller中的的所有action都自动事先调用了它.

添加评论

Security Code: