php 如何在 Laravel 代码中嵌入视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16957861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to embed video in laravel code
提问by Xk0nSid
I am working on a project in which I store youtube video links in a database and then I retrieve those links and using blade template engine i try to embed them into page. I use a loop to put videos in a page. For some reason I'm not getting any video in the browser. It is covering the mentioned space but not rendering anything. I'm using laravel 3. Here's couple of code snippets, if they help.
我正在开发一个项目,在该项目中我将 youtube 视频链接存储在数据库中,然后检索这些链接并使用刀片模板引擎尝试将它们嵌入到页面中。我使用循环将视频放在页面中。出于某种原因,我没有在浏览器中收到任何视频。它覆盖了提到的空间,但没有渲染任何东西。我正在使用 laravel 3。这里有几个代码片段,如果它们有帮助的话。
This is the index.blade.php
这是 index.blade.php
@layout('layouts.master')
@section('content')
@foreach ($videos as $video)
<h4>{{ $video->title }}</h4>
<br>
<div class="media">
<div class="media-body">
<iframe width="560" height="315" src="{{ $video->link }}" frameborder="0" allowfullscreen>
</iframe>
</div>
</div>
<br>
@endforeach
@endsection
The $videos variable is passed from controller.
$videos 变量是从控制器传递的。
class Videos_Controller extends Base_Controller
{
public $restful = true;
public function get_index()
{
$videos = DB::table('videos')->get();
return View::make('videos.index')
->with('title', 'Videos')
->with('videos', $videos);
}
}
All i get is a blank page. I don't understand what i'm doing wrong. I'm using twitter bootstrap for css prototyping. Any help will be appreciated.
我得到的只是一个空白页。我不明白我做错了什么。我正在使用 twitter bootstrap 进行 css 原型设计。任何帮助将不胜感激。
Here's how master.blade.php's 'head' looks like:
master.blade.php 的“头部”如下所示:
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ $title }} </title>
<meta name="viewport" content="width=device-width">
{{ HTML::style('css/bootstrap.min.css') }}
{{ HTML::style('css/bootstrap-responsive.min.css') }}
</head>
This is what comes up in firefox when I inspect element.
这就是我检查元素时在 Firefox 中出现的内容。
<div class="media">
<div class="media-body">
<iframe width="560" height="315" frameborder="0" allowfullscreen="" src="http://www.youtube.com/watch?v=1iBm60uJXvs">
#document
<html>
<head></head>
<body></body>
</html>
</iframe>
</div>
</div>
回答by Gagan
Fortunately , I did manage to find a solution to my problem. Hopefully it should help you as well . Use the url that is present in the embed statement. so for example if you goto youtube and click on the embed link of thisvideo you would get this
幸运的是,我确实设法找到了解决我的问题的方法。希望它也能帮助你。使用 embed 语句中存在的 url。因此,例如,如果您转到 youtube 并单击此视频的嵌入链接,您会得到这个
<iframe width="420" height="315" src="//www.youtube.com/embed/BstTBw6BLrE" frameborder="0" allowfullscreen></iframe>
now use the url that is present in the src tag. This works with other video hosting services, like VEVO as well.
现在使用存在于 src 标签中的 url。这也适用于其他视频托管服务,例如 VEVO。
Hope it helps..
希望能帮助到你..
回答by Artur Grigio
For anyone else looking to Embed Videos in Laravel, have a look at KaneCohen/embed. It's works great with Youtube or Vimeo (haven't tried other sources), and you don't have to mess with any logic. Simply install (installation guide in documentation) and:
对于希望在 Laravel 中嵌入视频的其他人,请查看KaneCohen/embed。它适用于 Youtube 或 Vimeo(还没有尝试过其他来源),而且您不必弄乱任何逻辑。只需安装(文档中的安装指南)并:
@foreach ($videos as $video)
<h4>{{ $video->title }}</h4>
<div class="media">
<div class="media-body">
{!! Embed::make($video->link)->parseUrl()->getIframe() !!}
</div>
</div>
@endforeach
Don't forget to set up the alias in you config/app.php
不要忘记在你里面设置别名 config/app.php
回答by Diego Rodriguez
I have the same issue, but the console catch this error:
我有同样的问题,但控制台捕获此错误:
Refused to display 'URL' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
拒绝在框架中显示“URL”,因为它将“X-Frame-Options”设置为“SAMEORIGIN”。
I look for it here and i found this post
我在这里寻找它,我找到了这篇文章
Refused to display in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'
拒绝在框架中显示,因为它将“X-Frame-Options”设置为“SAMEORIGIN”
and that work for me.
这对我有用。
You should try this
你应该试试这个
@layout('layouts.master')
@section('content')
@foreach ($videos as $video)
<h4>{{ $video->title }}</h4>
<br>
<div class="media">
<div class="media-body">
<iframe width="560" height="315" src={{ $video->link + "&output=embed" }} frameborder="0" allowfullscreen>
</iframe>
</div>
</div>
<br>
@endforeach
@endsection
回答by Pedro Ramos
Use a simple blade tag in the view
在视图中使用简单的刀片标签
{!! $url !!}THIS WILL RENDER THE VIDEO
{!!$url !!}这将呈现视频
instead of
代替
{{ $url }} THIS WILL WRITE AS A STRING (in a input for example)
{{ $url }} 这将作为字符串写入(例如在输入中)
回答by Abdullah Al Mahamud
you can simply use {{}} instead of this use {!! !!}
您可以简单地使用 {{}} 而不是使用 {!! !!}
For example :
例如 :
{!! $video->link !!}
回答by YoungStar7
how does your 'layouts.master' looks like? any html source code in your browser? have a look: your 'index.blade.php' should be encoded in UTF-8 WITHOUT 'BOM' (in your editor)!!
你的“layouts.master”是什么样的?您的浏览器中有任何 html 源代码吗?看看:你的“index.blade.php”应该用UTF-8编码,没有“BOM”(在你的编辑器中)!
'@layout('layouts.master')' has to be the very first (same issue to me yesterday in L4).
'@layout('layouts.master')' 必须是第一个(昨天在 L4 中我遇到了同样的问题)。