Reputation: 12945
I have set up a resource in Laravel 4 for an entity "Artists"; Within the ArtistController, I have added a function of my own, youtube_embed. When I call this function in my view (the show view), it is saying that it is undeclared. Do you have any idea why I am receiving this error? Thank you.
Here is the code:
in ArtistController:
public function youtube_embed($vari) {
$step1=explode('v=', $vari);
$step2 =explode('&',$step1[1]);
$iframe ='<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';
return $iframe;
}
in the show.blade.php:
{{youtube_embed($artist->video_path);}}
Thank you again for your help.
Upvotes: 0
Views: 5825
Reputation: 3247
There's lot of way to achieve this.
You could include the file at the bottom of app/start/global.php
You could make a helpers class
in app/helpers/Embed.php, you could do something like this
class Embed {
public static function youtube($vari) {
$step1 = explode('v=', $vari);
$step2 = explode('&', $step1[1]);
$iframe = '<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';
return $iframe;
}
}
And use it in blade like this
{{Embed::youtube($artist->video_path)}}
That way, if you want to add embed vimeo, you could also add it and call it like
{{Embed::vimeo($artist->video_path)}}
You could make a custom form macro
Form::macro('youtube', function($vari) {
$step1 = explode('v=', $vari);
$step2 = explode('&', $step1[1]);
$iframe = '<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';
return $iframe;
});
and call it like this
{{ Form::youtube($artist->video_path) }}
So many possibilities! :)
Upvotes: 1
Reputation: 130
Looks like all you need is just a helper. I would do this in my app.
Firstly, create a folder "helpers" in your "app" folder.
Then create a file named "common.php" inside the "helpers" folder. Put your function in this file:
<?php
if ( ! function_exists('image'))
{
function youtube_embed($vari)
{
$step1=explode('v=', $vari);
$step2 =explode('&',$step1[1]);
$iframe ='<iframe style="border:4px solid #41759d" width="460" height="259" src="http://www.youtube.com/embed/'.$step1[1].'" frameborder="0" allowfullscreen></iframe>';
return $iframe;
}
}
Next, include this file in your route.php.
<?php
include('libraries/common.php');
And you can use your function inside your application.
Upvotes: 0