Example #1
0
 /**
  * Sends the specified data as the specified format.
  *
  * @param string $format the format to be set.
  * @param mixed $data data to be sent.
  * @param array $headers headers to be sent along with the response.
  *
  * @return \yii\web\Response the response object itself.
  */
 protected static function sendFormat($format, $data, array $headers)
 {
     /**
      * @var \yii\web\Response $response
      */
     $response = static::getFacadeComponent();
     $response->format = $format;
     $response->data = $data;
     foreach ($headers as $name => $value) {
         if (strtolower($name) === 'location') {
             /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */
             $value = \yii\helpers\Url::to($value);
             if (strpos($value, '/') === 0 && strpos($value, '//') !== 0) {
                 $value = Request::getHostInfo() . $value;
             }
         }
         $response->getHeaders()->set($name, $value);
     }
     return $response;
 }
Example #2
0
 /**
  * Parses the given request and returns the corresponding route and parameters.
  * @param UrlManager $manager the URL manager
  * @param Request $request the request component
  * @return array|boolean the parsing result. The route and the parameters are returned as an array.
  * If false, it means this rule cannot be used to parse this path info.
  */
 public function parseRequest($manager, $request)
 {
     if ($this->mode === self::CREATION_ONLY) {
         return false;
     }
     if (!empty($this->verb) && !in_array($request->getMethod(), $this->verb, true)) {
         return false;
     }
     $pathInfo = $request->getPathInfo();
     $suffix = (string) ($this->suffix === null ? $manager->suffix : $this->suffix);
     if ($suffix !== '' && $pathInfo !== '') {
         $n = strlen($suffix);
         if (substr_compare($pathInfo, $suffix, -$n) === 0) {
             $pathInfo = substr($pathInfo, 0, -$n);
             if ($pathInfo === '') {
                 // suffix alone is not allowed
                 return false;
             }
         } else {
             return false;
         }
     }
     if ($this->host !== null) {
         $pathInfo = strtolower($request->getHostInfo()) . ($pathInfo === '' ? '' : '/' . $pathInfo);
     }
     if (!preg_match($this->pattern, $pathInfo, $matches)) {
         return false;
     }
     foreach ($this->defaults as $name => $value) {
         if (!isset($matches[$name]) || $matches[$name] === '') {
             $matches[$name] = $value;
         }
     }
     $params = $this->defaults;
     $tr = [];
     foreach ($matches as $name => $value) {
         if (isset($this->_routeParams[$name])) {
             $tr[$this->_routeParams[$name]] = $value;
             unset($params[$name]);
         } elseif (isset($this->_paramRules[$name])) {
             $params[$name] = $value;
         }
     }
     if ($this->_routeRule !== null) {
         $route = strtr($this->route, $tr);
     } else {
         $route = $this->route;
     }
     foreach ($params as $name => &$value) {
         if (!$this->setParamValue($name, $value)) {
             return false;
         }
     }
     Yii::trace("Request parsed with URL rule: {$this->name}", __METHOD__);
     return [$route, $params];
 }