Install the Bundle Run:
composer require bmatzner/popcorn-bundle
Update dependencies:
php composer.phar update bmatzner/popcorn-bundle
Register the Bundle
Add to app/AppKernel.php:
public function registerBundles()
{
$bundles = [
// ...
new Bmatzner\PopcornBundle\BmatznerPopcornBundle(),
];
}
Install Assets
Symlink assets to web/ (recommended for development):
php app/console assets:install --symlink web
First Use Case: Embed Popcorn.js in a Twig Template
Include the script in your Twig file (e.g., base.html.twig):
<script src="{{ asset('bundles/bmatznerpopcorn/js/popcorn-complete.min.js') }}"></script>
Test with a basic video element:
<video id="myVideo" controls>
<source src="{{ asset('videos/example.mp4') }}" type="video/mp4">
</video>
<script>
var popcorn = Popcorn('#myVideo');
popcorn.on('play', function() {
console.log('Video started!');
});
</script>
asset() function to reference Popcorn.js and its dependencies (e.g., jQuery if needed).{% popcorn_video '#myVideo' %}
(Requires a custom Twig extension in your bundle/service.)play, pause, timeupdate) to trigger Symfony services or controllers:
var popcorn = Popcorn('#myVideo');
popcorn.on('timeupdate', function(e) {
// Call a Symfony controller via AJAX or WebSocket
fetch('/api/video-progress', { method: 'POST', body: JSON.stringify({ time: e.detail.time }) });
});
<script src="{{ asset('bundles/bmatznerpopcorn/js/popcorn-complete.min.js', {'version': '1.3'}) }}"></script>
if (document.getElementById('myVideo')) {
var script = document.createElement('script');
script.src = "{{ path('bmatzner_popcorn_js') }}";
document.body.appendChild(script);
}
// src/AppBundle/Service/VideoService.php
class VideoService {
public function __construct($twig) {
$this->twig = $twig;
}
public function renderPopcornScript($videoId) {
return $this->twig->render('BmatznerPopcornBundle:Script:popcorn.js.twig', ['videoId' => $videoId]);
}
}
# config/routes.yml
bmatzner_popcorn_js:
path: /bundles/bmatznerpopcorn/js/popcorn-complete.min.js
defaults: { _controller: FrameworkBundle\Controller\RedirectController::urlRedirectAction, path: '@BmatznerPopcornBundle/Resources/public/js/popcorn-complete.min.js' }
--symlink, ensure your server supports symlinks (e.g., not all shared hosts do). Fall back to --no-symlink if issues arise.php app/console assets:install --env=prod --no-debug
popcorn-complete.min.js is missing, check:
Resources/public/js/ directory exists.vendor/bmatzner/popcorn-bundle).<script src="{{ asset('bundles/jquery/jquery.js') }}"></script>
<script src="{{ asset('bundles/bmatznerpopcorn/js/popcorn-complete.min.js') }}"></script>
jquery@1.11.0).Popcorn is not defined: Script not loaded or jQuery missing.Cannot read property 'on' of undefined: Video element not found or Popcorn.js not initialized correctly.Popcorn.prototype.customEvent = function() {
this.on('customEvent', function(e) { /* ... */ });
};
popcorn.on('play', function() {
fetch('/api/video-played', { method: 'POST' });
});
popcorn-text, popcorn-image).{{ parent() }}
{{ include('BmatznerPopcornBundle::script.js') }}
encore or webpack to bundle Popcorn.js with other JS files in production.fancyweb/popcorn-bundle if needed.How can I help you explore Laravel packages today?