Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

host_with_path with dynamic page. #1474

Closed
Tracked by #1495
mesiarm opened this issue Jul 19, 2022 · 21 comments
Closed
Tracked by #1495

host_with_path with dynamic page. #1474

mesiarm opened this issue Jul 19, 2022 · 21 comments

Comments

@mesiarm
Copy link

mesiarm commented Jul 19, 2022

I want create sites for diferent locales (example.com/page, example.com/en/page) so I am using host_with_path multisite option. But dynamic page (example.com/project/{slug}) stopped working and when visiting example.com/project/example it redirects back to example.com. When I added dynamic page to ignore_uri_patterns, it works, but blocks in global page doesn´t work.

@mesiarm
Copy link
Author

mesiarm commented Jul 19, 2022

When I try debug method matchRequest in Sonata\PageBundle\Site\BaseSiteSelector $site->getRelativePath() returns "/" sprintf('@^(%s)(/.*|$)@', $site->getRelativePath()) returns "@^(/)(/.*|$)@", and $requestPathInfo contains "/project/example/". Preg_match doesn´t match anything. Is regex correct?

protected function matchRequest(SiteInterface $site, Request $request)
    {
        $results = [];

        // we read the value from the attribute to handle fragment support
        $requestPathInfo = $request->get('pathInfo', $request->getPathInfo());

        if (!preg_match(sprintf('@^(%s)(/.*|$)@', $site->getRelativePath()), $requestPathInfo, $results)) {
            return false;
        }

        return $results[2];
    }

@eerison
Copy link
Contributor

eerison commented Jul 20, 2022

Hey @mesiarm I use multi site in my project too, but for dynamically routes, I create an action in a controller (a normal symfony's action) to manager this route!
But I guess it's not the best approach, later I found that is possible to use Hybrid or Dynamic pages, look here: https://docs.sonata-project.org/projects/SonataPageBundle/en/3.x/reference/introduction/#a-page

But there isn't too much doc about this, I guess you need to check in the code how it works, But if it works for you provide some Pull request improving the documentation about this please, maybe it can help others.

@eerison
Copy link
Contributor

eerison commented Jul 20, 2022

When I try debug method matchRequest in Sonata\PageBundle\Site\BaseSiteSelector $site->getRelativePath() returns "/" sprintf('@^(%s)(/.*|$)@', $site->getRelativePath()) returns "@^(/)(/.*|$)@", and $requestPathInfo contains "/project/example/". Preg_match doesn´t match anything. Is regex correct?

protected function matchRequest(SiteInterface $site, Request $request)
    {
        $results = [];

        // we read the value from the attribute to handle fragment support
        $requestPathInfo = $request->get('pathInfo', $request->getPathInfo());

        if (!preg_match(sprintf('@^(%s)(/.*|$)@', $site->getRelativePath()), $requestPathInfo, $results)) {
            return false;
        }

        return $results[2];
    }

have you tried to use Hybrid or Dynamic routes?

@mesiarm
Copy link
Author

mesiarm commented Jul 20, 2022

When I try debug method matchRequest in Sonata\PageBundle\Site\BaseSiteSelector $site->getRelativePath() returns "/" sprintf('@^(%s)(/.*|$)@', $site->getRelativePath()) returns "@^(/)(/.*|$)@", and $requestPathInfo contains "/project/example/". Preg_match doesn´t match anything. Is regex correct?

protected function matchRequest(SiteInterface $site, Request $request)
    {
        $results = [];

        // we read the value from the attribute to handle fragment support
        $requestPathInfo = $request->get('pathInfo', $request->getPathInfo());

        if (!preg_match(sprintf('@^(%s)(/.*|$)@', $site->getRelativePath()), $requestPathInfo, $results)) {
            return false;
        }

        return $results[2];
    }

have you tried to use Hybrid or Dynamic routes?

I created App Project Detail page with /project/{slug} page and then created ProjectController with detail action and route setting:

<?php

namespace App\Controller;

use App\Entity\Project;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class ProjectController extends AbstractController
{
    /**
     * @Route("/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', [
            'test' => 'test',
            'project' => $project,
        ]);
    }
}

@haivala
Copy link
Contributor

haivala commented Jul 20, 2022

Just to mention that I have done something like this: #1368

@eerison
Copy link
Contributor

eerison commented Jul 20, 2022

Just to mention that I have done something like this: #1368

Hi @haivala thanks for share your issue here :)

But definitely we need more documentation about this, should be really good if someone that used this can provide some doc, unfortunately I didn't test this functionality yet.

But @mesiarm if you want to test this solution and add some doc, it will be really good :)

@mesiarm
Copy link
Author

mesiarm commented Jul 24, 2022

#752 - simmilar issue. Dynamic page should be definitely better documented, how to use it, maybe someone, who used this functionality in his/her projects could provide more information. I will try to look at source code to understand it better.

@mesiarm
Copy link
Author

mesiarm commented Jul 27, 2022

"@^(/)(/.*|$)@" regex in matchRequest in Sonata\PageBundle\Site\BaseSiteSelector causes / as default site relative path is not supported.

@mesiarm
Copy link
Author

mesiarm commented Aug 1, 2022

So if default site relative path cannot be / - is it bug or is it intended?

@eerison
Copy link
Contributor

eerison commented Aug 1, 2022

So if default site relative path cannot be / - is it bug or is it intended?

Can't it be "/" or "/-"? If you mean it can't be "/-" then I would say it's intend, Because this slug/url is more or less wrong?🤔

Or is it missing the locale like "/de-en"?

@mesiarm
Copy link
Author

mesiarm commented Aug 1, 2022

I mean only / (without dash). I wanted to use / for default site and /en for English site.

@eerison
Copy link
Contributor

eerison commented Aug 1, 2022

I mean only / (without dash). I wanted to use / for default site and /en for English site.

Well if you can't use with / and /en ,I would say it's a bug 🤔, what do you think?

I use this functionality but with multisite

@mesiarm
Copy link
Author

mesiarm commented Aug 1, 2022

Just to clarify: /en works, /anything works, but / doesn´t. Regex is sprintf('@^(%s)(/.*|$)@', $site->getRelativePath())

@eerison
Copy link
Contributor

eerison commented Aug 1, 2022

Just to clarify: /en works, /anything works, but / doesn´t. Regex is sprintf('@^(%s)(/.*|$)@', $site->getRelativePath())

I guess you can create a site with relativePath = null

@mesiarm
Copy link
Author

mesiarm commented Aug 1, 2022

Just to clarify: /en works, /anything works, but / doesn´t. Regex is sprintf('@^(%s)(/.*|$)@', $site->getRelativePath())

I guess you can create a site with relativePath = null

I think it wouldn´t match mentioned regex which is in method matchRequest in Sonata\PageBundle\Site\BaseSiteSelector

@mesiarm
Copy link
Author

mesiarm commented Aug 1, 2022

correction - I tried it with null and it works (probably due $request->setPathInfo($pathInfo ?: '/'); in Sonata\PageBundle\Site\HostPathSiteSelector) . It should be corrected in documentation https://docs.sonata-project.org/projects/SonataPageBundle/en/3.x/reference/getting_started/ because there is stated default site /, but in CreateSiteCommand it is converted to empty string ($site->setRelativePath('/' === $values['relativePath'] ? '' : $values['relativePath']);) It works, because Sonata\PageBundle\Site\HostPathSiteSelector accepts falsy values, but it is confusing, when creating sites manually.

@eerison
Copy link
Contributor

eerison commented Aug 1, 2022

correction - I tried it with null and it works (probably due $request->setPathInfo($pathInfo ?: '/'); in Sonata\PageBundle\Site\HostPathSiteSelector) . It should be corrected in documentation https://docs.sonata-project.org/projects/SonataPageBundle/en/3.x/reference/getting_started/ because there is stated default site /, but in CreateSiteCommand it is converted to empty string ($site->setRelativePath('/' === $values['relativePath'] ? '' : $values['relativePath']);) It works, because Sonata\PageBundle\Site\HostPathSiteSelector accepts falsy values, but it is confusing, when creating sites manually.

But the question is, is it the correct way 👀 , I don't know why the doc says that the default value is /, it's strange :/

@mesiarm
Copy link
Author

mesiarm commented Aug 1, 2022

correction - I tried it with null and it works (probably due $request->setPathInfo($pathInfo ?: '/'); in Sonata\PageBundle\Site\HostPathSiteSelector) . It should be corrected in documentation https://docs.sonata-project.org/projects/SonataPageBundle/en/3.x/reference/getting_started/ because there is stated default site /, but in CreateSiteCommand it is converted to empty string ($site->setRelativePath('/' === $values['relativePath'] ? '' : $values['relativePath']);) It works, because Sonata\PageBundle\Site\HostPathSiteSelector accepts falsy values, but it is confusing, when creating sites manually.

But the question is, is it the correct way 👀 , I don't know why the doc says that the default value is /, it's strange :/

Yes, any falsy value in relativePath works, / doesn´t work.

@eerison
Copy link
Contributor

eerison commented Aug 1, 2022

correction - I tried it with null and it works (probably due $request->setPathInfo($pathInfo ?: '/'); in Sonata\PageBundle\Site\HostPathSiteSelector) . It should be corrected in documentation https://docs.sonata-project.org/projects/SonataPageBundle/en/3.x/reference/getting_started/ because there is stated default site /, but in CreateSiteCommand it is converted to empty string ($site->setRelativePath('/' === $values['relativePath'] ? '' : $values['relativePath']);) It works, because Sonata\PageBundle\Site\HostPathSiteSelector accepts falsy values, but it is confusing, when creating sites manually.

But the question is, is it the correct way 👀 , I don't know why the doc says that the default value is /, it's strange :/

Yes, any falsy value in relativePath works, / doesn´t work.

well in this case we should provide a fix for this! like doesn't accept "/" in relativePath we could use symfony assert for this

@eerison
Copy link
Contributor

eerison commented Aug 1, 2022

Yeah I checked now and in the project that I'm working it's null

@jordisala1991
Copy link
Member

Does #1606 fix this issue?

@eerison eerison mentioned this issue Sep 13, 2022
36 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants