sonata-project / SonataPageBundle

This bundle provides a Site and Page management through container and block services
https://docs.sonata-project.org/projects/SonataPageBundle
MIT License
216 stars 209 forks source link

Dynamic page doesn´t pass controller variables #1514

Closed mesiarm closed 2 years ago

mesiarm commented 2 years ago
class ProjectController extends AbstractController
{
    /**
     * @Route({"cs": "/projekt/{slug}", "en": "/project/{slug}"}, name="app_project_detail")
     * @ParamConverter("project", options={"mapping": {"slug": "slug"}})
     */
    public function detail(Project $project): Response
    {
        return $this->render('project/detail.html.twig', [
            'project' => $project,
        ]);
    }
}

I created detail action and visited /project/test (got error No page instance available for the url, run the sonata:page:update-core-routes and sonata:page:create-snapshots commands - it was expected)

Then I created page with route_name app_project_detail and created template with {{ project.title }} - I´ve got Variable "project" does not exist. Available variables (obtained running dump(_context|keys) in loop): error_codes, content, page, site, wrap_fields_with_addons, app, settings, sonata_block, sonata_config, sonata_admin, sonata_page, sonata_page_admin, sonata_user, sonata_media, sonata_translation_locales, _parent

Multisite in sonata_page.yml is set to host_with_path

If I add

- ^/project/(.*)
- ^/projekt/(.*)

to ignore_uri_patterns in sonata_page.yaml, variable project exists but block containers (for example sonata_page_render_container('header_logo', 'global') in parent template) won´t render, it renders when not using project variable and ignore_uri_patterns List of available variables, if is ignore_uri_patterns set:

project, wrap_fields_with_addons, app, settings, sonata_block, sonata_config, sonata_admin, sonata_page, sonata_page_admin, sonata_user, sonata_media, sonata_translation_locales, _parent

sonata-project/page-bundle is 3.27

mesiarm commented 2 years ago

It is something in ResponseListener:onCoreResponse. Anyone understands what decorate = true means? It is also here: https://docs.sonata-project.org/projects/SonataPageBundle/en/3.x/reference/advanced_configuration/ but without explanation

haivala commented 2 years ago

decorating is when controller is rendering template it adds base template to it even if the template does not extend it. Never understood how it should be used.

haivala commented 2 years ago

Actually that config option is just adding decoration automatically to pages that are created with the update:core-routes command.

mesiarm commented 2 years ago
// only decorate hybrid page or page with decorate = true
        if (!$page->isHybrid() || !$page->getDecorate()) {
            return;
        }

        $parameters = [
            'content' => $response->getContent(),
        ];

        $response = $this->pageServiceManager->execute($page, $request, $parameters, $response);

        if (!$this->cmsSelector->isEditor() && $page->isCms()) {
            $response->setTtl($page->getTtl());
        }

        $event->setResponse($response);

This is code in ResponseListener:onCoreResponse. Condition doesn´t match comment and I don´t know if code or comment is correct. But in my case dynamic page with decorate = true, fulfills condition and the next decorating code causes controller variables are missing. If I set decorate = false, my dynamic page works. It doesn´t apply to other pages with decorate=true, only to hybrid and dynamic pages with decorate = true due condition.

mesiarm commented 2 years ago

Anyone who understands when decoration should be used and which of the pair: comment and code is correct?

// only decorate hybrid page or page with decorate = true
        if (!$page->isHybrid() || !$page->getDecorate()) {
            return;
        }
jordisala1991 commented 2 years ago

I am not sure about the issue, but the example you gave on your first message contains an i18n route, and that's not really supported on SonataPageBundle, can you check without i18n route? (Normal route, without different locale configuration)

jordisala1991 commented 2 years ago

I have added test for hybrid and dynamic pages on 4.x and it works just fine in both cases.

In both cases when you have a route and you run the update core routes command, a page will be created. And the route will render that page, but in the "content" section it will output the rendered content of the original controller.

Closing for now, since it works but might lack some docs (not sure about that). what it does not work for sure are the i18n routes, and there is another issue for that.

You can check this file for the cases tested on the bundle (4.x branch): https://github.com/sonata-project/SonataPageBundle/blob/4.x/tests/Functional/Frontend/PageTest.php

Also if you have one case that does not work, one option is to try add that case on the test file.