Developed with love by KnpLabs Hire us for your project!
21

KnpMediaExposerBundle

by KnpLabs

Integration of the MediaExposer library for your Symfony project. -- THIS PROJECT IS NO LONGER MAINTAINED by KnpLabs -- Contact us if you want to be the...

KnpMediaExposerBundle

The KnpMediaExposerBundle provides a simple integration of the MediaExposer
library for your Symfony project.

The MediaExposer Library

The MediaExposer library allows you to easily expose you
medias to the users of your application by computing their urls or paths.

You can find more informations on the official page.

Installation

The bundle depends on the MediaExposer library, so you
need to copy it under the vendor/media-exposer directory of your Symfony
project.

Grab the sources

Using the deps file

You can add the following lines to your deps file:

[media-exposer]
    git=http://github.com/KnpLabs/MediaExposer.git

[KnpMediaExposerBundle]
    git=http://github.com/KnpLabs/KnpMediaExposerBundle.git
    target=/bundles/Knp/Bundle/MediaExposerBundle

And run the command:

./bin/vendors install

Using git submodules

You can run the following git commands to add both library and bundle as
submodules:

git submodule add https://github.com/KnpLabs/MediaExposer.git vendor/media-exposer
git submodule add https://github.com/KnpLabs/KnpMediaExposerBundle.git vendor/bundle/Knp/Bundle/MediaExposerBundle

Update your autoloader & kernel

Once you have copied the sources in your project, you must update your
autoloader:

<?php // app/autoload.php

$loader->registerNamespaces(array(
    // ... other namespaces
    'Knp\\Bundle'   => __DIR__.'/vendor/bundles',
    'MediaExposer'  => __DIR__.'/vendor/media-exposer/src'
));

Finally, register the bundle to your kernel:

<?php // app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ... the other bundles
        new Knp\Bundle\MediaExposerBundle\KnpMediaExposerBundle()
    );
}

You can now proceed to the configuration.

Configuration

To "absolutify" urls, the media exposer needs a base url that will be prepended
to the relatives sources. By default, the host of the request will be used,
but you can also specify it in your configuration:

# app/config/config.yml

knp_media_exposer:
    base_url:   'http://the-base.url'

Registering Resolvers

After the installation & configuration, the media exposer is almost ready
for use. But there is still one step: registering your resolvers.

With this bundle, adding a resolver to the exposer is as simple as registering
a service having the knp_media_exposer.resolver tag.

Here is an exemple of resolver service registration in yml:

services:
    foo.bar_resolver:
        class:  'Foo\BarResolver'
        tags:
            - { name: 'knp_media_exposer.resolver' }

An optional priority can also be specified:

services:
    foo.bar_resolver:
        class:  'Foo\BarResolver'
        tags:
            - { name: 'knp_media_exposer.resolver', priority: 10 }

Note: Don't forget that the highest priority is the first and the lowest is the last.

Usage

Twig integration

The bundle registers a Twig extension adding the necessary to use the Exposer
in your templates.

The media_has_source function

The media_has_source function indicates whether the resolver can return
a source for the given media:

{{ media_has_source(picture) }}

You can pass a hash of options as second argument:

{{ media_has_source(picture, {'foo':'bar'}) }}

The media_source function

The media_source function returns the source for the given media:

{{ media_source(picture) }}

You can specify an options hash as second argument:

{{ media_source(picture, {'foo':'bar'}) }}

If you want the Exposer to generate absolute sources (URLs), you can force
it passing true as third argument:

{{ media_source(picture, {}, true) }}

The media_has_path function

The media_has_path function indicates whether the resolver can return
a path for the given media:

{{ media_has_path(picture) }}

An hash of options can be passed as second argument:

{{ media_has_path(picture, {'foo':'bar'}) }}

The media_path function

The media_path function is responsible of returning a path for the given
media:

{{ media_path(picture) }}

You can also specifiy options as second argument:

{{ media_path(picture, {'foo':'bar'}}

PHP templating integration

The bundle registers an extension for the PHP templating engine. You can
access it using $view['media_exposer'] in your templates. It only contains
proxy methods for the Exposer instances:

  • ->getSource($media [, array $options [, $forceAbsolute]])
  • ->hasSource($media [, array $options])
  • ->getPath($media [, array $options])
  • ->hasPath($media [, array $options])
Copyright (c) 2010 Antoine Hérault

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

knplabs_media_exposer:
base_url: ~
  • Make base url listner service public
    By Herzult, 5 years ago
  • Merge pull request #1 from opsone/master
    By Herzult, 5 years ago
  • Add base_url argument to media_exposer service
    By kevynlebouille, 5 years ago
  • Fix case in repo urls
    By lwagner, 5 years ago
  • Fix case in repo urls
    By lwagner, 5 years ago
  • Fix tag name in readme
    By Herzult, 6 years ago
  • Add composer.json
    By Herzult, 6 years ago
  • Update readme
    By Herzult, 6 years ago
  • Update templating helpers
    By Herzult, 6 years ago
  • Fix compiler pass to get the actual resolver's priority
    By Palleas, 6 years ago
  • Update vendors
    By Herzult, 6 years ago
  • Update readme
    By Herzult, 6 years ago
  • Update container tag name
    By Herzult, 6 years ago
  • Add license
    By Herzult, 6 years ago
  • Add readme
    By Herzult, 6 years ago
  • Add some tests
    By Herzult, 6 years ago
  • Replace Knplabs namespace by Knp
    By Herzult, 6 years ago
  • Initial commit
    By Herzult, 6 years ago