charset() публичный метод

Sets the response charset if $charset is null the current charset is returned
public charset ( string | null $charset = null ) : string
$charset string | null Character set string.
Результат string Current charset
 /**
  * Sets the response header based on type map index name. This wraps several methods
  * available on Cake\Network\Response. It also allows you to use Content-Type aliases.
  *
  * @param string|array $type Friendly type name, i.e. 'html' or 'xml', or a full content-type,
  *    like 'application/x-shockwave'.
  * @param array $options If $type is a friendly type name that is associated with
  *    more than one type of content, $index is used to select which content-type to use.
  * @return bool Returns false if the friendly type name given in $type does
  *    not exist in the type map, or if the Content-type header has
  *    already been set by this method.
  * @see RequestHandlerComponent::setContent()
  */
 public function respondAs($type, array $options = array())
 {
     $defaults = array('index' => null, 'charset' => null, 'attachment' => false);
     $options += $defaults;
     $cType = $type;
     if (strpos($type, '/') === false) {
         $cType = $this->response->getMimeType($type);
     }
     if (is_array($cType)) {
         if (isset($cType[$options['index']])) {
             $cType = $cType[$options['index']];
         }
         if ($this->prefers($cType)) {
             $cType = $this->prefers($cType);
         } else {
             $cType = $cType[0];
         }
     }
     if (!$type) {
         return false;
     }
     if (empty($this->request->params['requested'])) {
         $this->response->type($cType);
     }
     if (!empty($options['charset'])) {
         $this->response->charset($options['charset']);
     }
     if (!empty($options['attachment'])) {
         $this->response->download($options['attachment']);
     }
     return true;
 }
 /**
  * Checks if the response can be considered different according to the request
  * headers, and the caching response headers. If it was not modified, then the
  * render process is skipped. And the client will get a blank response with a
  * "304 Not Modified" header.
  *
  * - If Router::extensions() is enabled, the layout and template type are
  *   switched based on the parsed extension or `Accept` header. For example,
  *   if `controller/action.xml` is requested, the view path becomes
  *   `app/View/Controller/xml/action.ctp`. Also if `controller/action` is
  *   requested with `Accept: application/xml` in the headers the view
  *   path will become `app/View/Controller/xml/action.ctp`. Layout and template
  *   types will only switch to mime-types recognized by Cake\Network\Response.
  *   If you need to declare additional mime-types, you can do so using
  *   Cake\Network\Response::type() in your controller's beforeFilter() method.
  * - If a helper with the same name as the extension exists, it is added to
  *   the controller.
  * - If the extension is of a type that RequestHandler understands, it will
  *   set that Content-type in the response header.
  *
  * @param \Cake\Event\Event $event The Controller.beforeRender event.
  * @return bool false if the render process should be aborted
  */
 public function beforeRender(Event $event)
 {
     $isRecognized = !in_array($this->ext, ['html', 'htm']) && $this->response->getMimeType($this->ext);
     if (!empty($this->ext) && $isRecognized) {
         $this->renderAs($event->subject(), $this->ext);
     } else {
         $this->response->charset(Configure::read('App.encoding'));
     }
     if ($this->_config['checkHttpCache'] && $this->response->checkNotModified($this->request)) {
         return false;
     }
 }
Пример #3
0
 /**
  * Tests the charset method
  *
  * @return void
  */
 public function testCharset()
 {
     $response = new Response();
     $this->assertEquals('UTF-8', $response->charset());
     $response->charset('iso-8859-1');
     $this->assertEquals('iso-8859-1', $response->charset());
     $this->assertEquals('UTF-16', $response->charset('UTF-16'));
 }
Пример #4
0
 /**
  * Add in the Content-Type header if necessary.
  *
  * @param array $headers The headers to update
  * @param \Cake\Network\Response $response The CakePHP response to convert
  * @return array The updated headers.
  */
 protected static function setContentType($headers, $response)
 {
     if (isset($headers['Content-Type'])) {
         return $headers;
     }
     if (in_array($response->statusCode(), [204, 304])) {
         return $headers;
     }
     $whitelist = ['application/javascript', 'application/json', 'application/xml', 'application/rss+xml'];
     $type = $response->type();
     $charset = $response->charset();
     $hasCharset = false;
     if ($charset && (strpos($type, 'text/') === 0 || in_array($type, $whitelist))) {
         $hasCharset = true;
     }
     $value = $type;
     if ($hasCharset) {
         $value = "{$type}; charset={$charset}";
     }
     $headers['Content-Type'] = $value;
     return $headers;
 }