Jump to content

Question: Add field to URL? | Howto modify URL path correctly?


DV-JF
 Share

Recommended Posts

Hi,

for a website witch is supposed to listen some events I'm trying to add the date-field from my "event-template" to the page URL so that the URL is something like this:

example.com/events/2014-02-14_title-of-event/

All single-events are children of the page "events"

I've tried

$pages->addHookAfter('Page::path', null, 'hookPagePath');
function hookPagePath(HookEvent $e) {
  $page = $e->object;
  if($page->template == 'article') $e->return = "/$page->startdate_$page->name/";
}

and the URL is rewritten correctly, but every time I try to reach this URL I get an 404 error. I've read this topic, but I think URL segments won't work for me, cause I try to work with an underscore.

Can you help?

Link to comment
Share on other sites

I'm not sure if your code snippet is right. Is the template name really "article"?

Wouldn't it be easier to hook after a page with the template is saved and then change the name of the page according to the date field? 

1) Hook after page is saved

2) Change name of the page to startdate_oldname

3) save the page.

Link to comment
Share on other sites

Ok again for the record. This hook just changes the page url and not the request of loading the page with that url. You need url segments to do that and there's no problem with underscores.

Philipps solution is also good one.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...