symfony权威指南(4、1)之动作

在之前的页面创建基础一文中简要的介绍了一下关于动作的相关知识,在这篇文章中我们将继续深入讨论一下symfony的动作。


1、通常情况下动作都写在actions.class.php文件中,但是当action过多的时候,可以考虑将动作拆分,一个动作独立成一个文件,如下所示:

之前的版本:

actions.class.php

<?php

/**
 * php actions.
 *
 * @package    blog
 * @subpackage php
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class phpActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }

  public function executeTest()
  {
  	$this->hour = date("H", time());
  }

  public function executeAnother()
  {
  	$this->name = $this->getRequestParameter('name');
  }
}
现在的版本:

actions.class.php

<?php

/**
 * php actions.
 *
 * @package    blog
 * @subpackage php
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class phpActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $this->forward('default', 'module');
  }
}
anotherAction.class.php

<?php
class anotherAction extends sfAction
{

  public function execute(sfWebRequest $request)
  {
  	$this->name = $this->getRequestParameter('name');
  }
  
}

testAction.class.php

<?php
class testAction extends sfAction
{
  public function execute($request)
  {
    $this->hour = date("H", time());
  }
  
}



2、在动作类里你可以获取控制器相关的信息和symfony 的核心对象,如下所示:
// Retrieving request parameters
$password = $this->getRequestParameter('password');
// Retrieving controller information
$moduleName = $this->getModuleName();
$actionName = $this->getActionName();
// Retrieving framework core objects
$request = $this->getRequest();
$userSession = $this->getUser();
$response = $this->getResponse();
$controller = $this->getController();
$context = $this->getContext();
// Setting action variables to pass information to the template
$this->setVar('foo', 'bar');
$this->foo = 'bar'; // Shorter version

sfController: 控制器对象(->getController()) 

sfRequest: 请求对象(->getRequest()) 

sfResponse: 应答对象 (->getResponse()) 

sfUser: 用户session 对象 (->getUser()) 

sfDatabaseConnection: 数据库链接对象(->getDatabaseConnection()) 

sfLogger: 日志对象 (->getLogger()) 

sfI18N:国际化对象 (->getI18N())


3、symfony中可以使用sfview类中的常量决定用哪个模板展示动作的结果,分为以下几种:


return sfView::SUCCESS;
这是动作的默认方式,即使省略不写,symfony一样会去找寻actionNameSuccess模板


return sfView::ERROR;
当有错误发生时,这样结尾, symfony就会去找寻actionNameError模板

return 'MyResult';

这个是自定义模板,这样返回时,symfony将会去找寻actionNameMyResult模板

return sfView::NONE;
有时我们并不需要试图展示,比如,这个动作是一个接口的时候,就可以使用如上代码,告诉symfony不要展示视图。

return sfView::HEADER_ONLY;
在一些情况下,你需要回复一个空的应答但要有HTTP 头(特别是X-JSONHTTP头)。返回HTTP 头sfView::HEADER_ONLY 常量,下面是一个示例:

public function executeRefresh()
{
$output = '<"title","My basic letter"],["name","Mr Brown">';
$this->getResponse()->setHttpHeader("X-JSON", '('.$output.')');
return sfView::HEADER_ONLY;
}
如果动作需要一个特殊的模板,去掉return 声明, 用 setTemplate()方法。
$this->setTemplate('myCustomTemplate');


4、动作跳转

symfony中提供了两个方法可以执行另一个动作,redirect和forward:

如果动作转发给另一个动作:

$this->forward('otherModule', 'index');
如果跳转到另一个动作:

$this->redirect('otherModule/index');
$this->redirect('http://www.google.com/');


其中forward方法有以下几种变种:

forward404()
forwardIf()
forwardUnless()
forward404If()
forward404Unless()

redirect方法也有相似的变种:

redirectIf()
redirectUnless()

这些方法接受一个参数并且对它进行判断,如果判断结果是true,xxxIf() 方法将被执行;如果判断结果是false,xxxUnless() 方法将被执行。
下面是示例:

public function executeShow()
{
$article =
ArticlePeer::retrieveByPK($this->getRequestParameter('id'));
$this->forward404If(!$article);
}
public function executeShow()
{
$article =
ArticlePeer::retrieveByPK($this->getRequestParameter('id'));
$this->forward404Unless($article);
}


5、动作共享代码

如果你需要在执行每个动作前都要执行一段代码,你可以把这段代码放入动作类里的preExecute()方法里。你可能猜到如果你想在执行每个动作后执行一段代
码,那你可以把这段代码放入postExecute()方法里。如下示例所示:

class mymoduleActions extends sfActions
{
public function preExecute()
{
// 这里的代码会在每个动作之前执行
...
}
public function executeIndex()
{
...
}
public function executeList()
{
...
$this->myCustomMethod(); // 可以使用动作类里定义的方法
}
public function postExecute()
{
// 这里的代码会在每次执行完动作之后执行
...
}
protected function myCustomMethod()
{
// 还可以添加自己的方法,只要不以"execute"开头
// 最好把这样的方法声明成protected 或者private
...
}
}

注:如果访问的动作做了跳转,preExecute和postExecute将不会执行。

6、访问请求

如果请求有附件,sfWebRequest 对象可以访问或移动这些文件,如下示例:

sfWebRequest 对象知道如何处理附件
class mymoduleActions extends sfActions
{
public function executeUpload()
{
if ($this->getRequest()->hasFiles())
{
foreach ($this->getRequest()->getFileNames() as $fileName)
{
$fileSize = $this->getRequest()->getFileSize($fileName);
$fileType = $this->getRequest()->getFileType($fileName);
$fileError = $this->getRequest()->hasFileError($fileName);
$uploadDir = sfConfig::get('sf_upload_dir');
$this->getRequest()->moveFile('file',
$uploadDir.'/'.$fileName);
}
}
}
}

以下是sfWebRequest 对象里的方法列表:

名称 功能 输出例子
getMethod()请求的方返回sfRequest::GET 或者sfRequest::POST常量
getMethodName()请求方法'POST'
getHttpHeader('Server')某个指定HTTP头的值
'Apache/2.0.59 (Unix) DAV/2 PHP/5.1.6'
getCookie('foo')某个cookie 的'bar'
isXmlHttpRequest()是否Ajaxtrue
isSecure()是否SSLtrue
hasParameter('foo')请求里是否包含某个参数?true
getParameter('foo')某个参数的值'bar'
getParameterHolder()->getAll()包含所有参数的数 
getUri()完整URI'http://localhost/myapp_dev.php/mymodule/myaction'
getPathInfo()路径信息'/mymodule/myaction'
getReferer()来源'http://localhost/myapp_dev.php/'
getHost() 主机'localhost'
getScriptName()前端控制器的路径和名'myapp_dev.php'
getLanguages()所有可接受的语言组成的数Array( [0] => fr [1] => fr_FR [2] => en_US
[3] => en )
getCharsets()所有可接受的字符集组成的数组Array( [0] => ISO-8859-1 [1] => UTF-8 [2]
=> * )
getAcceptableContentType()所有可接受的内容类型组成的数 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值