Giter Club home page Giter Club logo

laravel's Introduction

Description

A Laraval Package for use together with the php-tmdb/api TMDB Wrapper. This package comes with a service provider that configures the Tmdb\Client and registers it to the IoC container. Both Laravel 5 and 4 are supported.

Installation

Install Composer

$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

Add the following to your require block in composer.json config

"php-tmdb/laravel": "~1.0"

Configuration

Add to your app/config/app.php (Laravel 4) or 'config/app.php' (Laravel 5) the service provider:

'providers' => array(
    // other service providers

    'Tmdb\Laravel\TmdbServiceProvider',
)

Then publish the configuration file:

Laravel 4:

php artisan config:publish php-tmdb/laravel

Laravel 5:

php artisan vendor:publish --provider="Tmdb\Laravel\TmdbServiceProviderLaravel5"

Next you can modify the generated configuration file tmdb.php accordingly.

That's all! Fire away!

Usage

We can choose to either use the Tmdb Facade, or to use dependency injection.

Facade example

The example below shows how you can use the Tmdb facade. If you don't want to keep adding the use Tmdb\Laravel\Facades\Tmdb; statement in your files, then you can also add the facade as an alias in config/app.php file.

use Tmdb\Laravel\Facades\Tmdb;

class MoviesController {

    function show($id)
    {
        // returns information of a movie
        return Tmdb::getMoviesApi()->getMovie($id);
    }
}

Dependency injection example

use Tmdb\Repository\MovieRepository;

class MoviesController {

    private $movies;

    function __construct(MovieRepository $movies)
    {
        $this->movies = $movies;
    }

    function index()
    {
        // returns information of a movie
        return $this->movies->getPopular();
    }
}

Listening to events

We can easily listen to events that are dispatched using the Laravel event dispatcher that we're familiar with. The following example listens to any request that is made and logs a message.

use Log;
use Event;
use Tmdb\Event\TmdbEvents;
use Tmdb\Event\RequestEvent;

Event::listen(TmdbEvents::REQUEST, function(RequestEvent $event) {
    Log::info("A request was made to TMDB");
    // do stuff with $event
});

In Laravel 5 instead of using the Event facade we could also have used the EventServiceProvider to register our event listener.

Image helper

You can easily use the ImageHelper by using dependency injection. The following example shows how to show the poster image of the 20 most popular movies.

namespace App\Http\Controllers;

use Tmdb\Helper\ImageHelper;
use Tmdb\Repository\MovieRepository;

class WelcomeController extends Controller {

    private $movies;
    private $helper;

    public function __construct(MovieRepository $movies, ImageHelper $helper)
    {
        $this->movies = $movies;
        $this->helper = $helper;
    }

    /**
     * Show the application welcome screen to the user.
     *
     * @return Response
     */
    public function index()
    {
        $popular = $this->movies->getPopular();

        foreach ($popular as $movie)
        {
            $image = $movie->getPosterImage();
            echo ($this->helper->getHtml($image, 'w154', 260, 420));
        }
    }

}

The Configuration used by the Tmdb\Helper\ImageHelper is automatically loaded by the IoC container. If you are a Laravel 5.1 user you could also use the blade's @inject functionality,

@inject('image', 'Tmdb\Helper\ImageHelper')

@foreach ($movies as $movie)
    {!! $image->getHtml($movie->getPosterImage(), 'w154', 260, 420) !!}
@endforeach

Registering plugins

Plugins can be registered in a service provider using the boot() method.

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Tmdb\HttpClient\Plugin\LanguageFilterPlugin;

class TmdbServiceProvider extends ServiceProvider {

    /**
     * Add a Dutch language filter to the Tmdb client
     *
     * @return void
     */
    public function boot()
    {
        $plugin = new LanguageFilterPlugin('nl');
        $client = $this->app->make('Tmdb\Client');
        $client->getHttpClient()->addSubscriber($plugin);
    }

    /**
     * Register services
     * @return void
     */
    public function register()
    {
        // register any services that you need
    }
}

For all all other interactions take a look at php-tmdb/api.

laravel's People

Contributors

markredeman avatar wtfzdotnet avatar hiddeco avatar okaufmann avatar florentsorel avatar

Watchers

Kemal Yenilmez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.