CakePHP中文手册【翻译】-请求处理组件

请求处理组件

1

介绍

Cake里,使用请求处理组件来决定关于进来的HTTP请求信息。你可以使用它更好的通知你的controller关于AJAX请求,得到关于远程客户端IP地址,以及请求的内容类型,或者去除输出数据中你不想要的数据的信息。为了使用请求组件,你需要确保在Controller$component数组中指定它。.

class ThingsController extends AppController

{

    var $components = array('RequestHandler');

 

    // ...

}

2

获取客户/请求信息

让我们更深入一步:

accepts

  • string $type

返回客户接受的content-type信息,这取决于$type值。如果传入null或无值,它将返回一个客户端接受的content-type数组。如果传入一字符串,而且在content-type中检验$type(参看setContent()),客户端接受它,它返回true。如果$type是一个数组,每个字符串将分别计算,如果只要他们中的一个符合接受的content-type类型,accepts()方法返回true。例如:

class PostsController extends AppController

{

    var $components = array('RequestHandler');

 

    function beforeFilter ()

    {

        if ($this->RequestHandler->accepts('html'))

        {

            // Execute code only if client accepts an HTML (text/html) response

        }

        elseif ($this->RequestHandler->accepts('rss'))

        {

            // Execute RSS-only code

        }

        elseif ($this->RequestHandler->accepts('atom'))

        {

            // Execute Atom-only code

        }

        elseif ($this->RequestHandler->accepts('xml'))

        {

            // Execute XML-only code

        }

 

        if ($this->RequestHandler->accepts(array('xml', 'rss', 'atom')))

        {

            // Executes if the client accepts any of the above: XML, RSS or Atom

        }

    }

}

  • getAjaxVersion

如果你正在使用Prototype JS库,你可以获取一个特殊的头,此头在AJAX请求中设定。本函数返回使用的Prototype版本。

  • getClientIP

返回远程客户端的IP地址。

  • getReferrer

返回请求起源的服务器名。

  • isAjax

如果当前的请求是一个XMLHttpRequest,返回true

  • isAtom

如果客户端接受Atom feed内容(application/atom+xml),返回true

  • isDelete

如果当前的请求通过DELETE,返回true

  • isGet

如果当前请求通过GET,返回true

  • isMobile

如果user agent字符串符合一个移动的web浏览器,返回true

  • isPost

如果当前请求通过POST,返回true

  • isPut

如果当前请求通过PUT,返回true

  • isRss

如果客户端接受RSS feed内容(application/rss+xml),返回true

  • isXml

.如果客户端接受XMLapplication/xml or text/xml),返回true

setContent

  • string $name
  • string $type

对于accepts() prefers()的使用,增加一个content-type别名映射,在这里$name是映射(字符串)的名字,$type是一个字符串或者一个字符串数组。它们每个都是一个MIME类型。内建的类型映射如下:

// Name     => Type

  'js'      => 'text/javascript',

  'css'     => 'text/css',

  'html'    => 'text/html',

  'form'    => 'application/x-www-form-urlencoded',

  'file'    => 'multipart/form-data',

  'xhtml'   => array('application/xhtml+xml', 'application/xhtml', 'text/xhtml'),

  'xml'     => array('application/xml', 'text/xml'),

  'rss'     => 'application/rss+xml',

  'atom'    => 'application/atom+xml'

3

清除数据

你偶尔想删除来自一个请求或输出的数据,使用下面的请求处理函数来完成这些操作吧。

stripAll

  • string $str

删除$str中的空格,图像,以及脚本(using stripWhitespace(), stripImages(), stripScripts()).

stripImages

  • string $str

$str中删除任何嵌入图像的HTML

stripScripts

  • string $str

$str中删除任何与<script> <style>相关的标签。

stripTags

  • string $str
  • string $tag1
  • string $tag2...

$str中删除$tag1, $tag2等指定的标签。

$someString = '<font color="#FF0000"><bold>Foo</bold></font> <em>Bar</em>';

 

echo $this->RequestHandler->stripTags($someString, 'font', 'bold');

 

// 输出: Foo <em>Bar</em>

stripWhiteSpace

  • string $str

$str中删除空格。

4

其他有用的函数

当你的应用程序包含AJAX请求时,请求处理组件非常有用。使用setAjax()函数来自动检测AJAX请求,并为请求将Controller的布局设置为AJAX布局。这样的好处是你可以创建小的分块的view,这些view作为AJAX view也可能是双倍的。

// list.thtml

<ul>

<? foreach ($things as $thing):?>

<li><?php echo $thing;?></li>

<?endforeach;?>

</ul>

 

//-------------------------------------------------------------

 

//The list action of my ThingsController:

function list()

{

    $this->RequestHandler->setAjax($this);

    $this->set('things', $this->Thing->findAll());

}

当一个普通的浏览器请求为/things/list时,对于应用程序而言,无序列会在缺省的布局里render。如果把URL作为一个AJAX操作的一部分请求时,列表会自动在空的AJAX布局里render

Last Updated:2006年12月03日
posted @ 2006-11-30 09:51  张太国  阅读(1094)  评论(0编辑  收藏  举报