dunglas / DunglasActionBundle

Symfony controllers, redesigned
https://dunglas.fr/2016/01/dunglasactionbundle-symfony-controllers-redesigned/
MIT License
256 stars 14 forks source link

Upgrade from 0.2 to 0.3 disables autowiring #46

Closed bpolaszek closed 8 years ago

bpolaszek commented 8 years ago

Hello Kévin,

Just tried to upgrade from 0.2 to 0.3 (using Symfony 2.8.6) but I encounter several issues:

Cannot load resource "@SynapseCronManagerBundle/Action/". Make sure the "SynapseCronManagerBundle/Action/" bundle is correctly registered and loaded in the application kernel class. If the bundle is registered, make sure the bundle path "@SynapseCronManagerBundle/Action/" is not empty.

When RTFM README.md, I notice the routing.yml should not be this anymore :

SynapseCronManagerBundle:
    resource: "@SynapseCronManagerBundle/Action/"
    type:     'action-annotation'

but rather:

SynapseCronManagerBundle:
    resource: "@SynapseCronManagerBundle/Action/"
    type:     'annotation'

This indeed fixes the FileLoader exception, but I have another one, that tells me it can't autowire services:

Type error: Argument 1 passed to Synapse\CronManager\Bundle\Action\AbstractCRUDAction::__construct() must implement interface Symfony\Component\Routing\RouterInterface, none given

Reverting back to 0.2.0 solves the problem. Any clue?

No emergency at all, I can stay with 0.2.0, I just wanted to check if a new release would break my existing code (looks like it does) :-)

Thanks ! Ben

GuilhemN commented 8 years ago

Could you provide us more details such as your configuration, your controller and your routing file please ?

bpolaszek commented 8 years ago

Hi Ener-Getick,

This will be a little big, but if you need this:

Here's my routing.yml:

SynapseCronManagerBundle:
    resource: "@SynapseCronManagerBundle/Action/"
    type:     'action-annotation'

fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

config.yml:

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }

# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: en

framework:
    #esi:             ~
    #translator:      { fallbacks: ["%locale%"] }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    #serializer:      { enable_annotations: true }
    templating:
        engines: ['twig']
        #assets_version: SomeVersionScheme
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
        handler_id:  session.handler.native_file
        save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"
    fragments:       ~
    http_method_override: true
    assets: ~

# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    form_themes:
        - bootstrap_3_horizontal_layout.html.twig

# Doctrine Configuration
doctrine:
    dbal:
        # ...

    orm:
        # ...

# Swiftmailer Configuration
swiftmailer:
    # ...

sensio_framework_extra:
    view: { annotations: false }
    router: { annotations: true }
    request: { converters: true }

fos_rest:
    param_fetcher_listener: true
    body_listener: true
    format_listener:
        enabled: true
        rules:
            - { path: '^/', priorities: ['json', 'xml', 'html'], fallback_format: 'html' }
    view:
        view_response_listener: 'force'
        force_redirects:
            html: true
        failed_validation: HTTP_BAD_REQUEST
        default_engine: twig
    disable_csrf_role: ROLE_API

fos_user:
    # ...

guzzle:
    # ...

stof_doctrine_extensions:
    # ...

apy_breadcrumb_trail:
    # ...

And here's Synapse\CronManager\Bundle\Action\AbstractCRUDAction:

<?php

namespace Synapse\CronManager\Bundle\Action;

use BenTools\HelpfulTraits\Symfony\ControllerHelpersTrait;
use BenTools\HelpfulTraits\Symfony\EntityManagerAwareTrait;
use BenTools\HelpfulTraits\Symfony\RouterAwareTrait;
use BenTools\HelpfulTraits\Symfony\SecurityAwareTrait;
use BenTools\HelpfulTraits\Symfony\SessionAwareTrait;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\UnitOfWork;
use FlintLabs\Bundle\FormMetadataBundle\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Synapse\CronManager\Bundle\Entity\EntityInterface;
use Synapse\CronManager\Bundle\Services\TemplateExtension\TemplateDataStorage;

abstract class AbstractCRUDAction {

    use RouterAwareTrait, SessionAwareTrait, SecurityAwareTrait, EntityManagerAwareTrait, ControllerHelpersTrait;

    protected $entityClass;
    protected $entityName;
    protected $entitySlugName;
    protected $pluralEntityName;
    protected $listingRoute;
    protected $singleEntityRoute;

    /**
     * @var FormFactory
     */
    protected $formFactory;

    /**
     * @var FormMapper
     */
    protected $formMapper;

    /**
     * @var TemplateDataStorage
     */
    protected $templateDataStorage;

    /**
     * @var FormBuilderInterface
     */
    protected $formBuilder;

    /**
     * AbstractCRUDAction constructor.
     * @param RouterInterface $router
     * @param TokenStorage $tokenStorage
     * @param AuthorizationChecker $authorizationChecker
     * @param ManagerRegistry $managerRegistry
     * @param SessionInterface $session
     * @param FormFactory $formFactory
     * @param FormMapper $formMapper
     * @param TemplateDataStorage $templateDataStorage
     */
    public function __construct(RouterInterface $router,
                                TokenStorage $tokenStorage,
                                AuthorizationChecker $authorizationChecker,
                                ManagerRegistry $managerRegistry,
                                SessionInterface $session,
                                FormFactory $formFactory,
                                FormMapper $formMapper,
                                TemplateDataStorage $templateDataStorage) {
        $this->router               = $router;
        $this->tokenStorage         = $tokenStorage;
        $this->authorizationChecker = $authorizationChecker;
        $this->managerRegistry      = $managerRegistry;
        $this->session              = $session;
        $this->formFactory          = $formFactory;
        $this->formMapper           = $formMapper;
        $this->templateDataStorage  = $templateDataStorage;
    }

    // ...

}

All Action classes, in the same directory, extend AbstractCRUDAction. This config correctly works with DunglasActionBundle v0.2.0.

GuilhemN commented 8 years ago

The problem is that your bundle is in a sub directory and has an uncommon path. DunglasActionBundle v0.3.0 only supports bundles in the root of your src directory (see the Configuration file.

You can fix this error by updating the bundle config:

dunglas_action:
    directories:
        - ../src/*/*/Bundle/Action
bpolaszek commented 8 years ago

Yep! Got it.

Adding these lines wasn't enough, I also had to update sensio/framework-extra-bundle from 3.0.13 to 3.0.16, remove my own TemplateGuesser class (related to #5) and add the following lines in config.yml under the sensio_framework_extra node:

sensio_framework_extra:
    templating:
        controller_patterns:
            - /Action\\(.+)Action$/

Now everything's OK. :+1:

Thanks! :-) Ben