Routing Questions

Is it possible to add a custom route to Kirby so that when accessing a certain page I can add to the URL?

For example if I’m accessing the following page

http://website.com/dashboard

I can add the current logged in username before the dashboard part of the URL

http://website.com/USERNAME/dashboard

Thanks :slightly_smiling:

You mean a page on the website frontend, right? Then you can use a simple route like this:

c::set('routes', array(
  array(
    'pattern' => 'dashboard',
    'action'  => function() {
    	if(kirby()->site()->user()) {
    	  $user = kirby()->site()->user()->current();
          go($user . '/dashboard');
        } else {
          // do something else
        }
    }
  )
));
1 Like

Hi @texnixe,

Yes, from the front end. If a user is logged in if they try to access the dashboard page it will show their username in the URL so it would output - USERNAME/DASHBOARD.

I’ve just tried the code you posted and it still redirects to just /DASHBOARD.

It should redirect to the user if the user is logged in. Otherwise it does nothing in this case. At least it does in my test install :pensive:

Sorry my bad, Yes it does indeed work, although it redirects to an error page but the URL is now showing as I want it.

How would i go about showing the content from the dashboard page but upon a different URL? I wont be able to create a new page for each user if this makes sense?

Maybe an easier way would be to say if a user was logged in then always add their username to the url and instead of going to say website.com/dashboard or website.com/page it could show website.com/username/page.

Does this make sense?

Since username always points to the current user (which is determined by the session), the user does not need to be in the URL (this actually only creates a possible security issue when the name is not validated correctly).

I would create a page dashboard that dynamically inserts the user data into the page.

I think the main question is really what you want to achieve. Because even if you would get routing to work this way, it would just add an unnecessary layer of complexity and @lukasbestle’s suggestion sounds a lot more reasonable.