get() public method

- 'data': Retrieves values from POST data. - 'params': Retrieves query parameters returned from the routing system. - 'query': Retrieves values from GET data. - 'env': Retrieves values from the server or environment, such as 'env:https', or custom environment values, like 'env:base'. See the env() method for more info. - 'http': Retrieves header values (i.e. 'http:accept'), or the HTTP request method (i.e. 'http:method'). This method is used in several different places in the framework in order to provide the ability to act conditionally on different aspects of the request. See Media::type() (the section on content negotiation) and the routing system for more information. _Note_: All keys should be _lower-cased_, even when getting HTTP headers.
See also: lithium\action\Request::env()
See also: lithium\net\http\Media::type()
See also: lithium\net\http\Router
public get ( string $key ) : string
$key string A prefixed key indicating what part of the request data the requested value should come from, and the name of the value to retrieve, in lower case.
return string Returns the value of a GET, POST, routing or environment variable, or an HTTP header or method name.
 public function testGetMethod()
 {
     $_SERVER['PHP_SELF'] = '/lithium.com/app/webroot/index.php';
     $_POST['Article']['title'] = 'cool';
     $request = new Request();
     $expected = array('title' => 'cool');
     $result = $request->get('data:Article');
     $this->assertEqual($expected, $result);
     $expected = null;
     $result = $request->get('not:Post');
     $this->assertEqual($expected, $result);
     $expected = '/lithium.com';
     $result = $request->get('env:base');
     $this->assertEqual($expected, $result);
     unset($_POST, $request);
 }
Example #2
0
 public function testGetMethod()
 {
     $request = new Request(array('env' => array('PHP_SELF' => '/lithium.com/app/webroot/index.php', 'HTTP_ACCEPT' => 'text/html,application/xml,image/png,*/*', 'HTTP_ACCEPT_LANGUAGE' => 'da, en-gb;q=0.8, en;q=0.7')));
     $request->data = array('Article' => array('title' => 'cool'));
     $expected = array('title' => 'cool');
     $result = $request->get('data:Article');
     $this->assertEqual($expected, $result);
     $result = $request->get('not:Post');
     $this->assertNull($result);
     $expected = '/lithium.com';
     $result = $request->get('env:base');
     $this->assertEqual($expected, $result);
     $accept = $request->get('http:accept');
     $this->assertEqual('text/html,application/xml,image/png,*/*', $accept);
     $this->assertEqual($request->get('http:method'), $request->env('REQUEST_METHOD'));
 }
Example #3
0
 /**
  * Assists `Media::negotiate()` in processing the negotiation conditions of a content type, by
  * iterating through the conditions and checking each one against the `Request` object.
  *
  * @see lithium\net\http\Media::negotiate()
  * @see lithium\net\http\Media::type()
  * @see lithium\action\Request
  * @param \lithium\action\Request $request The request to be checked against a
  *        set of conditions (if applicable).
  * @param array $config Represents a content type configuration, which is an array containing 3
  *              keys:
  *              - `'name'` _string_: The type name, i.e. `'html'` or `'json'`.
  *              - `'content'` _mixed_: One or more content types that the configuration
  *                represents, i.e. `'text/html'`, `'application/xhtml+xml'` or
  *                `'application/json'`, or an array containing multiple content types.
  *              - `'options'` _array_: An array containing rendering information, and an
  *                optional `'conditions'` key, which contains an array of matching parameters.
  *                For more details on these matching parameters, see `Media::type()`.
  * @return boolean Returns `true` if the information in `$request` matches the type
  *         configuration in `$config`, otherwise false.
  */
 public static function match($request, array $config)
 {
     if (!isset($config['options']['conditions'])) {
         return true;
     }
     $conditions = $config['options']['conditions'];
     foreach ($conditions as $key => $value) {
         switch (true) {
             case $key === 'type':
                 if ($value !== ($request->type === $config['name'])) {
                     return false;
                 }
                 break;
             case strpos($key, ':'):
                 if ($request->get($key) !== $value) {
                     return false;
                 }
                 break;
             case $request->is($key) !== $value:
                 return false;
                 break;
         }
     }
     return true;
 }