fudge/silex-container-aware

Provides Symfony2 Container Aware controllers within Silex

Installs: 150

Dependents: 0

Suggesters: 0

Security: 0

Stars: 6

Watchers: 1

Forks: 1

Open Issues: 0

Type:service-provider

0.1.0 2014-03-18 11:35 UTC

This package is not auto-updated.

Last update: 2024-04-23 04:22:26 UTC


README

Provides Symfony2 Container Aware controllers within Silex.

This may seem redudant as you are given the Application instance per method call, but this allows you to keep your code DRY as reusable methods can be implemented involving external services, such as redirects and rendering.

Rest assured that this package is covered 100% by Unit Tests, and does follow Semantic Versioning (unlike others!).

Examples below.

Requirements

  • PHP 5.4+
  • Silex ~1 (Obviously!)

Examples

This is a very naive example, showing the accessibility of the Container within a ContainerAware Controller

Silex - index.php

<?php

require __DIR__.'/vendor/autoload.php';

use Silex\Application;
use Silex\Provider\ServiceControllerServiceProvider;
use Fudge\SilexComponents\ContainerAware\ContainerAwareServiceProvider;

$app = new Application;
$app->register(new ContainerAwareServiceProvider);
$app->register(new ServiceControllerServiceProvider);
$app->get("/", "IndexController::hello");

IndexController.php

<?php

class IndexController extends \Fudge\SilexComponents\ContainerAware\Controller
{
    public function hello()
    {
        return $this->render("foo.html.twig");
    }

    protected function render($templateName)
    {
        return $this->get("twig")->render($templateName);
    }

    protected function get($service)
    {
        return $this->getContainer()[$service];
    }
}

Roadmap

  • Implement more features to the ContainerAware Controller
  • Potentially extend the functionality of the Silex\Application to allow automatic dependency injection, similar to Laravel