Laravel / PHP - 将Youtube视频嵌入到图像阵列中

时间:2018-04-11 09:28:45

标签: php laravel youtube

我是PHP和Laravel的新手,我有这个登陆页面,在页面主体上显示图像。

我的客户想要添加Youtube视频而不是其中一张图片,我不太清楚该怎么做。

我尝试使用$ counter在foreach循环中添加一个if语句,当$ counter等于$ i时会显示嵌入代码,但我做错了或者只是不知道怎么做正确,因为身体没有任何东西,而不是视频和图像......

非常感谢所有的帮助,谢谢!

以下是代码:

page.php:

public function index($path = "")
{

    $data = $this->data;

        $images = [
            "images/portraits/1.jpg",
            "images/portraits/2.jpg",
            "images/portraits/3.jpg",
            "images/portraits/4.jpg"
        ];

    $data['images'] = $images;
    $counter = 0;
    $data['counter'] = $counter;
    return view($this->folderPath."main",$data);
}

main.blade.php:

    @foreach($images as $i)
       <?php if ($counter == 2)?>
         <?php if ($counter == 2) ?>
            <iframe
             width="560" height="315" 
            src="https://www.youtube.com/embed/VIDEOID? 
            rel=0&amp;controls=0" 
            frameborder="0" allow="autoplay; encrypted-media" 
            allowfullscreen>
            </iframe>
        <div class="cbp-item">
            <a href="{{$i}}" class="cbp-caption cbp-lightbox">
                <div class="cbp-caption-defaultWrap">
                    <img src="{{str_replace(".jpeg","_mini.jpeg",str_replace(".jpg","_mini.jpg",$i))}}" alt="">
                </div>
                <div class="cbp-caption-activeWrap cbp-gray">
                    <div class="cbp-l-caption-alignCenter">
                        <div class="cbp-l-caption-body">
                            <i class="fa fa-expand enlarge-icon"></i>
                        </div>
                    </div>
                </div>
            </a>
        </div>
    @endforeach

1 个答案:

答案 0 :(得分:2)

你可以根据自己的要求做这样的事情

page.php:

public function index($path = "")
{

    $data = $this->data;

        $images = [
            ["type" => "image", "file" => "images/portraits/1.jpg"],
            ["type" => "image", "file" => "images/portraits/2.jpg"],
            ["type" => "image", "file" => "images/portraits/3.jpg"],
            ["type" => "video", "file" => "https://www.youtube.com/embed/tgbNymZ7vqY"]
        ];

    $data['images'] = $images;
    return view($this->folderPath."main",$data);
}
main.blade.php :

    @foreach($images as $i)
        <div class="cbp-item">
            <a href="{{$i['file']}}" class="cbp-caption cbp-lightbox">
                <div class="cbp-caption-defaultWrap">
                    @if($i['type'] == 'image')
                      <img src="{{str_replace(".jpeg","_mini.jpeg",str_replace(".jpg","_mini.jpg",$i['file']))}}" alt="">
                    @else
                      <iframe width="420" height="315"
src="{{ $i['file'] }}">
</iframe>
                    @endif
                </div>
                <div class="cbp-caption-activeWrap cbp-gray">
                    <div class="cbp-l-caption-alignCenter">
                        <div class="cbp-l-caption-body">
                            <i class="fa fa-expand enlarge-icon"></i>
                        </div>
                    </div>
                </div>
            </a>
        </div>
    @endforeach