PHPackages                             dunglas/action-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. dunglas/action-bundle

Abandoned → [symfony/dependency-injection](/?search=symfony%2Fdependency-injection)ArchivedSymfony-bundle[Framework](/categories/framework)

dunglas/action-bundle
=====================

Symfony controllers, redesigned

v0.4.1(9y ago)264410.7k↑21.1%14[7 issues](https://github.com/dunglas/DunglasActionBundle/issues)3MITPHPPHP &gt;=5.5.9

Since Jan 21Pushed 8y ago14 watchersCompare

[ Source](https://github.com/dunglas/DunglasActionBundle)[ Packagist](https://packagist.org/packages/dunglas/action-bundle)[ Docs](https://dunglas.fr/2016/01/dunglasactionbundle-symfony-controllers-redesigned/)[ RSS](/packages/dunglas-action-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (6)Dependencies (12)Versions (7)Used By (3)

DunglasActionBundle: Symfony controllers, redesigned
====================================================

[](#dunglasactionbundle-symfony-controllers-redesigned)

[![Build Status](https://camo.githubusercontent.com/820903e61589d0c5275df8d52e60beb4d50ba3780c424021f010ae8c7807f89a/68747470733a2f2f7472617669732d63692e6f72672f64756e676c61732f44756e676c6173416374696f6e42756e646c652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dunglas/DunglasActionBundle)[![Build status](https://camo.githubusercontent.com/7a403453b4f3af75014edeaaa453ae888d96eb23a71c1e3c0057ea98ca39be9d/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f6a706a73617378353973796b6e6768653f7376673d74727565)](https://ci.appveyor.com/project/dunglas/dunglasactionbundle)[![SensioLabsInsight](https://camo.githubusercontent.com/f1c362bd7dee4896d07b003b457ffa465ed489c4c7a24ec24716ab778ac0d730/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f37303232626365342d396436372d346164652d396231392d6366376534313763306138302f6d696e692e706e67)](https://insight.sensiolabs.com/projects/7022bce4-9d67-4ade-9b19-cf7e417c0a80)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/6d1e56df6677aaf233aa83511ea0b25dd490d2d32de522912016f51d48435f8e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f64756e676c61732f44756e676c6173416374696f6e42756e646c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/dunglas/DunglasActionBundle/?branch=master)[![StyleCI](https://camo.githubusercontent.com/91ae56728daa0342cba28e42826f7ef0e15c808bbbb2ce65da0e4100702371d1/68747470733a2f2f7374796c6563692e696f2f7265706f732f35303034383635322f736869656c64)](https://styleci.io/repos/50048652)

This bundle is a replacement for [the controller system](https://symfony.com/doc/current/book/controller.html) of the [Symfony framework](https://symfony.com) and for its [command system](https://symfony.com/doc/current/cookbook/console/console_command.html).

It is as convenient as the original but doesn't suffer from its drawbacks:

- Action and console classes are automatically **registered as services** by the bundle
- Their dependencies are **explicitly injected** in the constructor (no more ugly access to the service container) using the [autowiring feature of the Dependency Injection Component](https://dunglas.fr/2015/10/new-in-symfony-2-83-0-services-autowiring/)
- Only one action per class thanks to the [`__invoke()` method](http://php.net/manual/en/language.oop5.magic.php#object.invoke)(but you're still free to create classes with more than 1 action if you want to)
- 100% compatible with common libraries and bundles including [SensioFrameworkExtraBundle](https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/)annotations

DunglasActionBundle allows to create **reusable**, **framework agnostic** (especially when used with [the PSR-7 bridge](https://dunglas.fr/2015/06/using-psr-7-in-symfony/)) and **easy to unit test** classes.

See [symfony/symfony#16863 (comment)](https://github.com/symfony/symfony/pull/16863#issuecomment-162221353) for the history behind this bundle.

Note for Symfony &gt;=3.3 users
-------------------------------

[](#note-for-symfony-33-users)

If you use Symfony at version 3.3 or superior, you do not need to use this bundle as all the features were ported in Symfony. You can learn more about it in the [Symfony blog](http://symfony.com/blog/the-new-symfony-3-3-service-configuration-changes-explained)or in the [Symfony documentation](http://symfony.com/doc/current/service_container/3.3-di-changes.html).

Installation
------------

[](#installation)

Use [Composer](https://getcomposer.org/) to install this bundle:

```
composer require dunglas/action-bundle

```

Add the bundle in your application kernel:

```
// app/AppKernel.php

public function registerBundles()
{
    return [
        // ...
        new Dunglas\ActionBundle\DunglasActionBundle(),
        // ...
    ];
}
```

Optional: to use the `@Route` annotation add the following lines in `app/config/routing.yml`:

```
app:
    resource: '@AppBundle/Action/' # Use @AppBundle/Controller/ if you prefer
    type:     'annotation'
```

If you don't want to use annotations but prefer raw YAML, use the following syntax:

```
foo:
    path:      /foo/{bar}
    defaults:  { _controller: 'AppBundle\Action\Homepage' } # this is the name of the autoregistered service corresponding to this action
```

Usage
-----

[](#usage)

1. Create [an invokable class](http://www.lornajane.net/posts/2012/phps-magic-__invoke-method-and-the-callable-typehint)in the `Action\` namespace of your bundle:

```
// src/AppBundle/Action/MyAction.php

namespace AppBundle\Action;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class Homepage
{
    private $router;
    private $twig;

    /**
     * The action is automatically registered as a service and dependencies are autowired.
     * Typehint any service you need, it will be automatically injected.
     */
    public function __construct(RouterInterface $router, \Twig_Environment $twig)
    {
        $this->router = $router;
        $this->twig = $twig;
    }

    /**
     * @Route("/myaction", name="my_action")
     *
     * Using annotations is not mandatory, XML and YAML configuration files can be used instead.
     * If you want to decouple your actions from the framework, don't use annotations.
     */
    public function __invoke(Request $request)
    {
        if (!$request->isMethod('GET')) {
            // Redirect to the current URL using the the GET method if it's not the current one
            return new RedirectResponse($this->router->generateUrl('my_action'), 301);
        }

        return new Response($this->twig->render('mytemplate.html.twig'));
    }
}
```

Alternatively, you can create a typical Symfony controller class with several `*Action` methods in the `Controller` directory of your bundle, it will be autowired the same way.

**There is no step 2! You're already done.**

All classes inside `Action/` and `Controller/` directories of your project bundles are automatically registered as services. By convention, the service name is the Fully Qualified Name of the class.

For instance, the class in the example is automatically registered with the name `AppBundle\Action\Homepage`.

There are other classes/tags supported:

Class NameTag automatically addedDirectoryCommandconsole.commandCommandEventSubscriberInterfacekernel.event\_subscriberEventSubscriberTwig\_ExtensionInterfacetwig.extensionTwigThanks to the [autowiring feature](http://symfony.com/blog/new-in-symfony-2-8-service-auto-wiring) of the Dependency Injection Component, you can just typehint dependencies you need in the constructor, they will be automatically initialized and injected.

Service definition can easily be customized by explicitly defining a service named according to the same convention:

```
# app/config/services.yml

services:
    # This is a custom service definition
    'AppBundle\Action\MyAction':
        arguments: [ '@router', '@twig' ]

    'AppBundle\Command\MyCommand':
        arguments: [ '@router', '@twig' ]
        tags:
            - { name: console.command }

    # With Symfony < 3.3
    'AppBundle\EventSubscriber\MySubscriber':
        class: 'AppBundle\EventSubscriber\MySubscriber'
        tags:
            - { name: kernel.event_subscriber }
```

This bundle also hooks into the Routing Component (if it is available): when the `@Route` annotation is used as in the example, the route is automatically registered: the bundle guesses the service to map with the path specified in the annotation.

[Dive into the TestBundle](Tests/Fixtures/TestBundle) to discover more examples such as using custom services with ease (no configuration at all) or classes containing several actions.

Using the Symfony Micro Framework
---------------------------------

[](#using-the-symfony-micro-framework)

You might be interested to see how this bundle can be used together with [the Symfony "Micro" framework](https://symfony.com/doc/current/cookbook/configuration/micro-kernel-trait.html).

Here we go:

```
// MyMicroKernel.php

use AppBundle\Action\Homepage;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

final class MyMicroKernel extends Kernel
{
    use MicroKernelTrait;

    public function registerBundles()
    {
        return [
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Dunglas\ActionBundle\DunglasActionBundle(),
            new AppBundle\AppBundle(),
        ];
    }

    protected function configureRoutes(RouteCollectionBuilder $routes)
    {
        // Specify explicitly the controller
        $routes->add('/', Homepage::class, 'my_route');
        // Alternatively, use @Route annotations
        // $routes->import('@AppBundle/Action/', '/', 'annotation');
    }

    protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
    {
        $c->loadFromExtension('framework', ['secret' => 'MySecretKey']);
    }
}
```

Amazing isn't it?

Want to see a more advanced example? [Checkout our test micro kernel](Tests/Fixtures/TestKernel.php).

Configuration
-------------

[](#configuration)

```
# app/config/config.yml

dunglas_action:
    directories: # List of directories relative to the kernel root directory containing classes to auto-register.
        - '../src/*Bundle/{Controller,Action,Command,EventSubscriber}'
        # This one is not registered by default
        - '../src/*Bundle/My/Uncommon/Directory'
    tags:
        'Symfony\Component\Console\Command\Command': console.command
        'Symfony\Component\EventDispatcher\EventSubscriberInterface': kernel.event_subscriber
        'My\Custom\Interface\To\Auto\Tag':
            - 'my_custom.tag'
            - [ 'my_custom.tag_with_attributes', { attribute: 'value' } ]
```

Credits
-------

[](#credits)

This bundle is brought to you by [Kévin Dunglas](https://dunglas.fr) and [awesome contributors](https://github.com/dunglas/DunglasActionBundle/graphs/contributors). Sponsored by [Les-Tilleuls.coop](https://les-tilleuls.coop).

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community27

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 87.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~95 days

Recently: every ~117 days

Total

6

Last Release

3294d ago

PHP version history (3 changes)v0.1.0PHP &gt;=5.6

v0.3.0PHP &gt;=5.5.9

v0.3.1PHP &gt;=5.5.9 &lt;7.2.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/57224?v=4)[Kévin Dunglas](/maintainers/dunglas)[@dunglas](https://github.com/dunglas)

---

Top Contributors

[![dunglas](https://avatars.githubusercontent.com/u/57224?v=4)](https://github.com/dunglas "dunglas (106 commits)")[![stof](https://avatars.githubusercontent.com/u/439401?v=4)](https://github.com/stof "stof (4 commits)")[![theofidry](https://avatars.githubusercontent.com/u/5175937?v=4)](https://github.com/theofidry "theofidry (4 commits)")[![kix](https://avatars.githubusercontent.com/u/345754?v=4)](https://github.com/kix "kix (2 commits)")[![amenophis](https://avatars.githubusercontent.com/u/2158235?v=4)](https://github.com/amenophis "amenophis (1 commits)")[![marcw](https://avatars.githubusercontent.com/u/160332?v=4)](https://github.com/marcw "marcw (1 commits)")[![mickaelandrieu](https://avatars.githubusercontent.com/u/1247388?v=4)](https://github.com/mickaelandrieu "mickaelandrieu (1 commits)")[![ismailbaskin](https://avatars.githubusercontent.com/u/358830?v=4)](https://github.com/ismailbaskin "ismailbaskin (1 commits)")[![bpolaszek](https://avatars.githubusercontent.com/u/5569077?v=4)](https://github.com/bpolaszek "bpolaszek (1 commits)")

---

Tags

adrcontrollerphpsymfonysymfony-bundlesymfonyAutowiringroutingcontrollersaction

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dunglas-action-bundle/health.svg)

```
[![Health](https://phpackages.com/badges/dunglas-action-bundle/health.svg)](https://phpackages.com/packages/dunglas-action-bundle)
```

###  Alternatives

[symfony/framework-bundle

Provides a tight integration between Symfony components and the Symfony full-stack framework

3.6k235.4M9.7k](/packages/symfony-framework-bundle)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[prestashop/prestashop

PrestaShop is an Open Source e-commerce platform, committed to providing the best shopping cart experience for both merchants and customers.

9.0k15.4k](/packages/prestashop-prestashop)[contao/core-bundle

Contao Open Source CMS

1231.6M2.4k](/packages/contao-core-bundle)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
