3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 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\Exception\RuntimeException
;
13 use Zend\Mvc\InjectApplicationEventInterface
;
15 class Params
extends AbstractPlugin
18 * Grabs a param from route match by default.
20 * @param string $param
21 * @param mixed $default
24 public function __invoke($param = null, $default = null)
26 if ($param === null) {
29 return $this->fromRoute($param, $default);
33 * Return all files or a single file.
35 * @param string $name File name to retrieve, or null to get all.
36 * @param mixed $default Default value to use when the file is missing.
37 * @return array|\ArrayAccess|null
39 public function fromFiles($name = null, $default = null)
42 return $this->getController()->getRequest()->getFiles($name, $default)->toArray();
45 return $this->getController()->getRequest()->getFiles($name, $default);
49 * Return all header parameters or a single header parameter.
51 * @param string $header Header name to retrieve, or null to get all.
52 * @param mixed $default Default value to use when the requested header is missing.
53 * @return null|\Zend\Http\Header\HeaderInterface
55 public function fromHeader($header = null, $default = null)
57 if ($header === null) {
58 return $this->getController()->getRequest()->getHeaders($header, $default)->toArray();
61 return $this->getController()->getRequest()->getHeaders($header, $default);
65 * Return all post parameters or a single post parameter.
67 * @param string $param Parameter name to retrieve, or null to get all.
68 * @param mixed $default Default value to use when the parameter is missing.
71 public function fromPost($param = null, $default = null)
73 if ($param === null) {
74 return $this->getController()->getRequest()->getPost($param, $default)->toArray();
77 return $this->getController()->getRequest()->getPost($param, $default);
81 * Return all query parameters or a single query parameter.
83 * @param string $param Parameter name to retrieve, or null to get all.
84 * @param mixed $default Default value to use when the parameter is missing.
87 public function fromQuery($param = null, $default = null)
89 if ($param === null) {
90 return $this->getController()->getRequest()->getQuery($param, $default)->toArray();
93 return $this->getController()->getRequest()->getQuery($param, $default);
97 * Return all route parameters or a single route parameter.
99 * @param string $param Parameter name to retrieve, or null to get all.
100 * @param mixed $default Default value to use when the parameter is missing.
102 * @throws RuntimeException
104 public function fromRoute($param = null, $default = null)
106 $controller = $this->getController();
108 if (!$controller instanceof InjectApplicationEventInterface
) {
109 throw new RuntimeException(
110 'Controllers must implement Zend\Mvc\InjectApplicationEventInterface to use this plugin.'
114 if ($param === null) {
115 return $controller->getEvent()->getRouteMatch()->getParams();
118 return $controller->getEvent()->getRouteMatch()->getParam($param, $default);