$contentDocument controller action argument with Symfony CMF Dynamic Router

170 Views Asked by At

I've followed the Controllers and templates documentation after having integrated Symfony CMF and the AutoRoutingBundle into my application. My issue is similar to #50450338 but even when naming the argument correctly, I can't solve the problem.

I can create a Page Document and an AutoRoute gets created for it automatically, but when trying to use a controller to show that page, I can't get the $contentDocument parameter into my controller action as the doc suggests. When adding the argument, I get this error:

Could not resolve argument $contentDocument of "App\ContentBundle\Controller\PageController::pageaction()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?

Even when the controller is tagged as suggested, the error persists.

If I render the page template without the $contentDocument argument, I can see it, but I'm then unable to get the Page attributes.

Here is my Page document:

/**
 * Class Page
 * @package App\ContentBundle\Document
 *
 * @Document(translator="attribute", referenceable=true)
 */
class Page implements
    TranslatableInterface,
    RouteReferrersReadInterface
{
    /**
     * @var string
     * @Id()
     */
    protected $id;

    /**
     * @var string
     * @Uuid()
     */
    protected $uuid;

    /**
     * @var object
     * @ParentDocument()
     */
    protected $parent;

    /**
     * @Referrers(
     *     referringDocument="Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route",
     *     referencedBy="content"
     * )
     */
    protected $routes;

    /**
     * @var string|boolean
     * @Locale()
     */
    private $locale;

    /**
     * @var string|null
     * @Nodename()
     */
    private $slug;

    /**
     * @var string|null
     * @Field(type="string", nullable=false)
     */
    private $title;

    /**
     * @var \Sonata\BlockBundle\Model\BlockInterface[]
     * @Children()
     */
    private $blocks;

    public function __construct ()
    {
        $this->blocks = [];
    }

    /**
     * @return string
     */
    public function getId ()
    : string
    {
        return $this->id;
    }

    /**
     * @return object
     */
    public function getParentDocument ()
    : object
    {
        return $this->parent;
    }

    /**
     * @param object $parent
     */
    public function setParentDocument ( $parent )
    : void
    {
        $this->parent = $parent;
    }

    /**
     * @return mixed
     */
    public function getRoutes ()
    {
        return $this->routes;
    }

    /**
     * @return bool|string
     */
    public function getLocale ()
    {
        return $this->locale;
    }

    /**
     * @param bool|string $locale
     */
    public function setLocale ( $locale )
    : void
    {
        $this->locale = $locale;
    }

    /**
     * @return string|null
     */
    public function getSlug ()
    : ?string
    {
        return $this->slug;
    }

    /**
     * @param string|null $slug
     */
    public function setSlug ( ?string $slug )
    : void
    {
        $this->slug = $slug;
    }

    /**
     * @return string|null
     */
    public function getTitle ()
    : ?string
    {
        return $this->title;
    }

    /**
     * @param string|null $title
     */
    public function setTitle ( ?string $title )
    : void
    {
        $this->title = $title;
    }

    /**
     * @return \Sonata\BlockBundle\Model\BlockInterface[]
     */
    public function getBlocks ()
    : array
    {
        return $this->blocks;
    }

    /**
     * @param \Sonata\BlockBundle\Model\BlockInterface[] $blocks
     */
    public function setBlocks ( array $blocks )
    : void
    {
        $this->blocks = $blocks;
    }
}

Symfony CMF config:

cmf_routing:
    chain:
        routers_by_id:
            router.default: 200
            cmf_routing.dynamic_router: 100
    dynamic:
        persistence:
            phpcr:
                enabled: true
                manager_name: default
        controllers_by_class:
            App\ContentBundle\Document\Page: App\ContentBundle\Controller\PageController::pageAction

cmf_routing_auto.yml in my Bundle:

App\ContentBundle\Document\Page:
    definitions:
        main:
            uri_schema: /{slug}
    token_providers:
        slug: [content_method, {method: getSlug}]

And the controller action:

    /**
     * @Template()
     * @param $contentDocument
     *
     * @return array
     */
    public function pageAction ( $contentDocument )
    {
        return [
            'page' => $contentDocument,
        ];
    }
0

There are 0 best solutions below