Slug system with unique id β€” Notion.so like

Hello there !

I would like to create a URL system like Notion where you can change the page title and be redirected to the right page from the ID present in the slug. This could fix SEO problems and some issues when the user change the title of a page.

The URL could be something like that
https://mydomain.com/title-of-the-page-[$page unique ID]

I think the router part is not too complicated, my problem is for the page creation and the renaming of a page with hooks.

With the hooks, page.create:after and page.changeTitle:after I update the slug with the page unique ID but this throw an error of redirection in the panel. I can’t find any solution to redirect to the updated URL.

Is there a way to achieve this URL system ?

Thanks for your help :slightly_smiling_face:

Unfortunately, it is not possible to redirect from a hook yet. While you could circumvent the page.create:after issue with using a page model, this won’t help for the changeTitle stuff.

Have you thought about overriding the url method in a page model to return the scheme you are seeking for? This way you wouldn’t have to actually rename any pages and only have to set up routes to display the correct pages?

class NotionPage extends Page
{
    public function url($options = null): string
    {
        $url = $this->parent()->url() . '/' . $this->slug() . '-' . $this->someIdMethod();

        return Url::to($url, $options);
    }
}
<?php

return [    
    'routes' => [
        [
            'pattern' => '(:any)',
            'method' => 'GET',
            'action' => function ($notion) {
                $parts = explode('-', $notion);
                $id = array_pop($parts)

                if ($page = page($id)) {
                    return $page->render();
                }

                return false;
            }
        ]
    ]
]

Not tested.

1 Like

Thanks @nilshoerrmann I will try and give you a feedback :slight_smile: