Creating URL with correct language using FilterRoute

7 years ago

uestla
Backer | 740
+
0
-

Hi.

I'm having a trouble while trying to make the use of FilterRoute work…

In the database there is a table for pages. Each page has its unique ID and a unique pair of language and slug (URL).

I think the code will say more:

bootstrap.php:

$pageFactory = callback($container, 'createPage');

$pageRoute = new FilterRoute("[<lang=cs cs|en|de>/][!<id=>]", 'Front:Homepage:default');
$pageRoute->addFilter('id',

    function ($slug, Request $request) use ($pageFactory) {
        $page = $pageFactory()
            ->where('lang', $request->parameters['lang'])
            ->where('slug', $slug)
            ->fetch();

        if (!$page) return NULL;
        return $page->id;
    },

    function ($id, Request $request) use ($pageFactory) {
        $page = $pageFactory()
            ->get($id);

        if (!$page) return NULL;
        return $page->slug;
    }
);

What I'm trying to do here is to make the FilterRoute getting an ID from a lang & slug (which works correctly), but also create lang & slug from an ID – but I'm unable to do so, because the ‘out’ filter returns only one parameter…

Is there any way how to set language in the out filter?

Thanks.


EDIT: I'm sorry to forgot to mention that the slug might be empty, that's why there's the empty default value (<id=>) in the route mask.

Last edited by uestla (2012-04-16 05:26)

7 years ago

uestla
Backer | 740
+
0
-

Just an addition: It is possible for two page records in the database to have the same slug. The unique pair is relly lang + slug I think it should be also possible for the router to construct the URL just from the given ID of the page, because I'm able to find the lang & slug just by the page ID.