3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Controller\Plugin
;
12 use Zend\Mvc\Controller\Plugin\AbstractPlugin
;
13 use Zend\Mvc\Exception\RuntimeException
;
14 use Zend\Mvc\InjectApplicationEventInterface
;
16 class Params
extends AbstractPlugin
19 * Grabs a param from route match by default.
21 * @param string $param
22 * @param mixed $default
25 public function __invoke($param = null, $default = null)
27 if ($param === null) {
30 return $this->fromRoute($param, $default);
34 * Return all files or a single file.
36 * @param string $name File name to retrieve, or null to get all.
37 * @param mixed $default Default value to use when the file is missing.
38 * @return array|\ArrayAccess|null
40 public function fromFiles($name = null, $default = null)
43 return $this->getController()->getRequest()->getFiles($name, $default)->toArray();
46 return $this->getController()->getRequest()->getFiles($name, $default);
50 * Return all header parameters or a single header parameter.
52 * @param string $header Header name to retrieve, or null to get all.
53 * @param mixed $default Default value to use when the requested header is missing.
54 * @return null|\Zend\Http\Header\HeaderInterface
56 public function fromHeader($header = null, $default = null)
58 if ($header === null) {
59 return $this->getController()->getRequest()->getHeaders($header, $default)->toArray();
62 return $this->getController()->getRequest()->getHeaders($header, $default);
66 * Return all post parameters or a single post parameter.
68 * @param string $param Parameter name to retrieve, or null to get all.
69 * @param mixed $default Default value to use when the parameter is missing.
72 public function fromPost($param = null, $default = null)
74 if ($param === null) {
75 return $this->getController()->getRequest()->getPost($param, $default)->toArray();
78 return $this->getController()->getRequest()->getPost($param, $default);
82 * Return all query parameters or a single query parameter.
84 * @param string $param Parameter name to retrieve, or null to get all.
85 * @param mixed $default Default value to use when the parameter is missing.
88 public function fromQuery($param = null, $default = null)
90 if ($param === null) {
91 return $this->getController()->getRequest()->getQuery($param, $default)->toArray();
94 return $this->getController()->getRequest()->getQuery($param, $default);
98 * Return all route parameters or a single route parameter.
100 * @param string $param Parameter name to retrieve, or null to get all.
101 * @param mixed $default Default value to use when the parameter is missing.
103 * @throws RuntimeException
105 public function fromRoute($param = null, $default = null)
107 $controller = $this->getController();
109 if (!$controller instanceof InjectApplicationEventInterface
) {
110 throw new RuntimeException(
111 'Controllers must implement Zend\Mvc\InjectApplicationEventInterface to use this plugin.'
115 if ($param === null) {
116 return $controller->getEvent()->getRouteMatch()->getParams();
119 return $controller->getEvent()->getRouteMatch()->getParam($param, $default);