Get support for KnpLabs/ControllerBehaviors

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

KnpLabs/ControllerBehaviors

Symfony2 controller Traits

Build Status

This php 5.4+ library is a collection of traits that adds behaviors to Symfony2 controllers.

It currently handles:

Usage

All you have to do is to define a Controller and use some traits.

crudable:

Crudable trait is an abstract trait used internally by ORMBehavior and ODMBehavior.

  • To use Doctrine ORM persistence in your CRUD, just use ORMBehavior like below.
  • To use Propel ORM persistence in your CRUD, just use PropelBehavior.

<?php

namespace Acme\DemoBundle\Controller;

use Knp\ControllerBehaviors\Crudable\Doctrine\ORMBehavior;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class MemberController extends Controller
{
    // make aliases of actions to make them FOSRestBundle compliant

    use ORMBehavior {
        ORMBehavior::getListResponse   as getMembersAction;
        ORMBehavior::getShowResponse   as getMemberAction;
        ORMBehavior::getNewResponse    as newMembersAction;
        ORMBehavior::getEditResponse   as editMemberAction;
        ORMBehavior::getCreateResponse as postMembersAction;
        ORMBehavior::getUpdateResponse as putMemberAction;
        ORMBehavior::getDeleteResponse as deleteMemberAction;
    }
}

Form

The create, edit and update actions of Crudable
will search for a <Object>Type in the Form folder of the bundle.

In the examples below, it would be in src/Acme/DemoBundle/Form/MemberType.

To modify this behavior, just override the default implementation of the trait, like this:


<?php

    protected function createNewForm($object)
    {
        return $this->createForm('my_super_type_id', $object, ['some_option' => true]);
    }

Templates

Templates are also searched using conventions. By default it will search in the Resources\views/<ControllerName> folder of your bundle.

<ControllerName> here can be contain a subfolder (think of an Admin subfolder for example).

To modify this behavior, just override the default implementation of the trait, like this:


<?php

    protected function getViewsPathPrefix()
    {
        return '::';
    }

Filterable

Filterable behavior is a simple trait that stores and retrieves some informations for a given controller, like filter form data.

Once you posted data to postFilterMembersAction, you can retrieve it later by using the getFilters method.

It also provides a way to handle a filter form, whose type yould be defined in src/Acme/DemoBundle/Form/MemberFilterType in this example.


<?php

use Knp\ControllerBehaviors\FilterableBehavior;

class MemberController extends Controller
{
    // make aliases of actions to make them FOSRestBundle compliant

    use FilterableBehavior {
        FilterableBehavior::getFilterResponse as postFilterMembersAction;
    }


}

In order to make this filter form visible in the view, you can override default view parameters handling:


<?php

    protected function getListViewParameters(array $parameters)
    {
        return array_merge([
            'filterForm'  => $this->createFilterForm()->createView(),
        ], $parameters);
    }

Paginable

Paginable behavior is a simple trait that uses Knp paginator to paginate a resultset.


<?php

use Knp\ControllerBehaviors\Paginatable\KnpPaginatorBehavior;

class MemberController extends Controller
{
    use KnpPaginatorBehavior;

    public function getObjectsToList()
    {
        return $this->paginateQueryBuilder(
            $this->getObjectRepository()->getJoinAllFilteredQB($this->getFilters()) // returns an ORM Query Builder
        );
    }
}

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2