CakePHP TMDB
, (*1)
This CakePHP 3 plugin provides integration with the TMDB API for retrieving data on movies and television from themoviedb.org. It makes use of an established TMDB API wrapper and the Webservice plugin for CakePHP., (*2)
Requirements
- CakePHP 3.x
- A valid TMDB API key
Installation
Install using composer: composer require drmonkeyninja/cakephp-tmdb:3.0.*
, (*3)
Then add the following lines to bootstrap.php to load the plugin:, (*4)
Plugin::load('Muffin/Webservice');
Plugin::load('CakeTmdb');
Configuration
You will need to configure a new webservice in config/app.php using your TMDB API key:, (*5)
'Webservices' => [
'Tmdb' => [
'className' => 'Muffin\Webservice\Connection',
'service' => 'CakeTmdb\Lib\Tmdb\Driver\Tmdb',
'api_key' => 'your_tmdb_api_key'
]
]
Then in bootstrap.php instruct the ConnectionManager
to consume the webservice:, (*6)
ConnectionManager::setConfig(Configure::consume('Webservices'));
Usage
This plugin uses the TMDB API library so you have full access to all of the methods provided there., (*7)
For example, to search the database for movies with the title 'Toy Story':, (*8)
$tmdb = \Cake\Datasource\ConnectionManager::get('Tmdb');
$data = $tmdb->getSearchApi()->searchMovies('Toy Story');
TmdbHelper
A handy little helper comes with the plugin for rendering TMDB images using the paths returned by the API. To use include the Tmdb
helper in your controller as normal:-, (*9)
public $helpers = ['CakeTmdb.Tmdb'];
Then in your view:-, (*10)
<?= $this->Tmdb->image($movie->poster, 'w154'); ?>
The first parameter needs to be the image path (provided by TMDB); the second parameter is the TMDB size. You can also pass an optional array of image attributes as the third parameter:-, (*11)
<?= $this->Tmdb->image($movie->poster, 'w154', ['alt' => $movie->title]); ?>