Laravel Vimeo

repository·master·Indexed 19 days ago

https://github.com/vimeo/laravel

A bridge for the official Vimeo PHP library that integrates the Vimeo API into Laravel applications. It provides a VimeoManager for handling multiple connections, a Vimeo facade for easy client access, and a service provider for automatic registration and configuration publishing.

Tokens
2K
Snippets
8
Records
10
Agent score
65%

What's inside vimeo-laravel

  1. How VimeoManager and connections work

    master

    The VimeoManager is the core class of the package. It manages multiple Vimeo connections and is bound to the IoC container as vimeo.

    Switching Connections

    While the facade uses the default connection automatically, you can explicitly call a specific connection using the connection method. This is useful when you have multiple sets of credentials configured.

    Managing the Default Connection

    You can inspect or change the active default connection at runtime.

    • Vimeo::connection('name') returns the specified connection.
    • Vimeo::getDefaultConnection() returns the name of the current default connection.
    • Vimeo::setDefaultConnection('name') changes the default connection for subsequent calls.
    use Vimeo\Laravel\Facades\Vimeo;
    
    // Explicitly using the 'main' connection
    Vimeo::connection('main')->upload('/bar.mp4');
    
    // Using the default connection (identical to the above if 'main' is default)
    Vimeo::connection()->upload('/bar.mp4');
    
    // Checking and changing the default
    $current = Vimeo::getDefaultConnection(); // returns 'main'
    Vimeo::setDefaultConnection('alternative');
  2. Configure Vimeo connections

    master

    To configure your Vimeo credentials, first publish the vendor assets to create the configuration file:

    $ php artisan vendor:publish --provider="Vimeo\Laravel\VimeoServiceProvider"

    This creates config/vimeo.php. You can define multiple connections in the connections array. The default connection used by the package is specified by the default key (defaults to main).

    Required Environment Variables

    For each connection, you must provide the following credentials (typically via your .env file):

    • VIMEO_CLIENT
    • VIMEO_SECRET
    • VIMEO_ACCESS

    Example for an alternate connection:

    • VIMEO_ALT_CLIENT
    • VIMEO_ALT_SECRET
    • VIMEO_ALT_ACCESS
  3. Install Laravel Vimeo

    master

    Install the package via Composer in your project's root directory:

    $ composer require vimeo/laravel

    Register the Service Provider

    If you are using Laravel 5.5 or higher, the package will be discovered automatically. For older versions, add the service provider to the providers array in config/app.php:

    Vimeo\Laravel\VimeoServiceProvider::class

    Register the Facade (Optional)

    To use the Vimeo facade, add the following alias to the aliases array in config/app.php:

    'Vimeo' => Vimeo\Laravel\Facades\Vimeo::class
  4. Register the Vimeo service provider

    master

    The VimeoServiceProvider handles the registration of the Vimeo package within Laravel and Lumen applications. It manages configuration publishing, singleton registration for the manager and factory, and provides bindings for the Vimeo connection.

    In a standard Laravel installation, the package automatically registers itself. If you are using Lumen, the provider ensures the vimeo configuration is loaded.

  5. Publish the Vimeo configuration file

    master

    To customize the package settings, you can publish the configuration file to your application's config directory. This is supported in Laravel when running via the CLI.

    Run the following Artisan command to publish the config:

    php artisan vendor:publish --provider="Vimeo\Laravel\VimeoServiceProvider"
  6. Inject the VimeoManager via Dependency Injection

    master

    Instead of using facades, you can inject the VimeoManager directly into your class constructors. This is the preferred method for testable code and following Laravel best practices.

    use Vimeo\Laravel\VimeoManager;
    
    class Foo
    {
        protected $vimeo;
    
        public function __construct(VimeoManager $vimeo)
        {
            $this->vimeo = $vimeo;
        }
    
        public function bar()
        {
            $this->vimeo->upload('/foo.mp4');
        }
    }
    
    // Resolving the class
    App::make('Foo')->bar();
  7. Use the Vimeo Facade

    master

    The Vimeo facade provides a simple way to interact with the Vimeo API. It dynamically passes calls to the VimeoManager instance in the IoC container. By default, it uses the main connection.

    Fetching Data

    Use the request method to perform API calls. The signature follows the pattern: request(path, parameters, method).

    Uploading Videos

    Use the upload method to upload a file from a local path.

    Uploading Images

    Use uploadImage to upload an image to a specific Vimeo path.

    use Vimeo\Laravel\Facades\Vimeo;
    
    // Fetching data
    Vimeo::request('/me/videos', ['per_page' => 10], 'GET');
    
    // Uploading a video
    Vimeo::upload('/bar.mp4');
    
    // Uploading an image
    Vimeo::uploadImage('/videos/123/images', '/home/aaron/bar.png', true);
  8. Access Vimeo connections via VimeoManager

    master

    The VimeoManager class is responsible for managing multiple Vimeo connections using Laravel's manager pattern. It uses a configuration key of vimeo to resolve connections. You can access specific Vimeo client instances by calling the manager using the name of the configuration group defined in your Laravel config files.

    // Assuming 'default' is a connection defined in your config/vimeo.php
    $vimeo = app('vimeo')->default();
    
    // Or if you have multiple connections defined
    $vimeoClient = app('vimeo')->connectionName();
  9. Access Vimeo services via the Service Container

    master

    The package registers several key components in the Laravel Service Container. You can resolve these using their class names or their string aliases.

    Available Bindings

    AliasClass / Type
    vimeoVimeo\Laravel\VimeoManager
    vimeo.factoryVimeo\Laravel\VimeoFactory
    vimeo.connectionVimeo\Vimeo

    Usage Examples

    Resolving the Manager Use the vimeo alias to access the VimeoManager, which is responsible for managing connections.

    $manager = app('vimeo');
    // or
    $manager = app(Vimeo\Laravel\VimeoManager::class);

    Resolving the Connection To get a direct instance of the Vimeo client (the connection), resolve the vimeo.connection binding.

    $vimeo = app('vimeo.connection');
    // or
    $vimeo = app(Vimeo\Vimeo::class);
    // Accessing the connection directly
    $vimeo = app(Vimeo\Vimeo::class);
    
    // Accessing the manager
    $manager = app(Vimeo\Laravel\VimeoManager::class);
  10. Use the Vimeo facade for easy client access

    master

    The Vimeo\Laravel\Facades\Vimeo facade provides a convenient, static proxy to the underlying Vimeo client instance registered in the Laravel service container under the vimeo key. Instead of resolving the client from the container manually, you can call methods directly on the Vimeo facade to interact with the Vimeo API.

    use Vimeo\
    Laravel\
    Facades\
    Vimeo;
    
    // Access the Vimeo client methods via the facade
    $client = Vimeo::getClient();