Cake\View\Helper\HtmlHelper::media PHP Метод

media() публичный Метод

### Usage Using an audio file: echo $this->Html->media('audio.mp3', ['fullBase' => true]); Outputs: Using a video file: echo $this->Html->media('video.mp4', ['text' => 'Fallback text']); Outputs: Using multiple video files: echo $this->Html->media( ['video.mp4', ['src' => 'video.ogv', 'type' => "video/ogg; codecs='theora, vorbis'"]], ['tag' => 'video', 'autoplay'] ); Outputs: ### Options - tag Type of media element to generate, either "audio" or "video". If tag is not provided it's guessed based on file's mime type. - text Text to include inside the audio/video tag - pathPrefix Path prefix to use for relative URLs, defaults to 'files/' - fullBase If provided the src attribute will get a full address including domain name
public media ( string | array $path, array $options = [] ) : string
$path string | array Path to the video file, relative to the webroot/{$options['pathPrefix']} directory. Or an array where each item itself can be a path string or an associate array containing keys `src` and `type`
$options array Array of HTML attributes, and special options above.
Результат string Generated media element
    public function media($path, array $options = [])
    {
        $options += ['tag' => null, 'pathPrefix' => 'files/', 'text' => ''];
        if (!empty($options['tag'])) {
            $tag = $options['tag'];
        } else {
            $tag = null;
        }
        if (is_array($path)) {
            $sourceTags = '';
            foreach ($path as &$source) {
                if (is_string($source)) {
                    $source = ['src' => $source];
                }
                if (!isset($source['type'])) {
                    $ext = pathinfo($source['src'], PATHINFO_EXTENSION);
                    $source['type'] = $this->response->getMimeType($ext);
                }
                $source['src'] = $this->Url->assetUrl($source['src'], $options);
                $sourceTags .= $this->formatTemplate('tagselfclosing', ['tag' => 'source', 'attrs' => $this->templater()->formatAttributes($source)]);
            }
            unset($source);
            $options['text'] = $sourceTags . $options['text'];
            unset($options['fullBase']);
        } else {
            if (empty($path) && !empty($options['src'])) {
                $path = $options['src'];
            }
            $options['src'] = $this->Url->assetUrl($path, $options);
        }
        if ($tag === null) {
            if (is_array($path)) {
                $mimeType = $path[0]['type'];
            } else {
                $mimeType = $this->response->getMimeType(pathinfo($path, PATHINFO_EXTENSION));
            }
            if (preg_match('#^video/#', $mimeType)) {
                $tag = 'video';
            } else {
                $tag = 'audio';
            }
        }
        if (isset($options['poster'])) {
            $options['poster'] = $this->Url->assetUrl($options['poster'], ['pathPrefix' => Configure::read('App.imageBaseUrl')] + $options);
        }
        $text = $options['text'];
        $options = array_diff_key($options, ['tag' => null, 'fullBase' => null, 'pathPrefix' => null, 'text' => null]);
        return $this->tag($tag, $text, $options);
    }

Usage Example

Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function media($path, array $options = array())
 {
     if ('text' == $this->getType()) {
         return;
     }
     return parent::media($path, $this->_mergeAttributes($options, $this->config('attributes.media')));
 }