Tuesday, July 27, 2010

Utility method 公共方法

_forward utility method

_forward()公共方法是用来呼叫actions,这可以使我们更容易的在action之间进行传值。
_forward($action, $controller = null, $module = null,  array $params = null)




我们看到_forward()包含四部分,其中三部份是可选的:

  • $action (string required): The action to call.
  • $controller (string optional): The controller the action is in.
  • $module (string optional): The module the controller is in.
  • $params (array optional): User parameters to send with the request.
当只提供$action时,方法会在该方法所在的controller里查找该action.

例如:
我们新建一个controller名为 contact:

Utility method 公共方法




在index controller里做如下更改:
Utility method 公共方法



打开 http://127.0.0.1:8080/index/index 时,会看到:
Utility method 公共方法



_redirect() utility method

跟_forward()功能相反。

The _redirect() utility method is in a way the opposite to _forward().
Where _forward() calls an action within the same request, _redirect()  performs an HTTP redirect creating a new request.
redirect() accepts the following arguments:
  • $url (string required): The URL to redirect to
  • $options (Array Optional)
And the $options can be:
  • exit (Boolean): Whether to exit straight away, or not
  • prependBase (Boolean): Prepend the base URL, or not
  • code (String): The HTTP code to use
By default, _redirect() will do a 302 redirect.

0 评论:

Post a Comment