Vineet Sawant Posted January 6, 2014 Posted January 6, 2014 Hello everyone, I'm working on a project where people can create their own pages on the site. Whenever anyone registers, a page with same name as username is created. Currently I'm having set up as follows: Home - Profiles - - somename - - someothername But what I need is people should be able to get a URL of their own such as example.com/somename Is there any way I can have example.com/profiles/somename to display example.com/somename? I suppose this has something to do with Hooks & routing but I'm no expert so I guess you guys can help. And happy new year to all of you. Thanks.
Soma Posted January 6, 2014 Posted January 6, 2014 http://processwire.com/talk/topic/1799-routes-and-rewriting-urls/ If you search a little more there's a couple threads about this subject.
Vineet Sawant Posted January 6, 2014 Author Posted January 6, 2014 @Soma Thanks, you are very fast! Actually I was reading the same article before I posted, but I didn't understand how it can solve my query. Nevermind, I'm already reading some more documentation about routing. This clarifies one thing for me and that's routing is something I must be looking for to solve this issue!
Soma Posted January 6, 2014 Posted January 6, 2014 Some more http://processwire.com/talk/topic/3275-hide-parent-page-from-url/ http://processwire.com/talk/topic/4725-remove-part-of-url-with-htaccess/ http://processwire.com/talk/topic/2984-controlling-page-url-structure/ 1
Vineet Sawant Posted January 6, 2014 Author Posted January 6, 2014 $pages->addHookAfter('Page::path', null, 'hookPagePath'); function hookPagePath(HookEvent $e) { $page = $e->object; if($page->template == 'u_profile') $e->return = "/$page->name/"; } For some reason, this code is not working for me. u_profile is the template of the profile page.
ryan Posted January 11, 2014 Posted January 11, 2014 Make sure that you are putting that code somewhere that gets executed on every request, and before any code that needs to use it. Also, attaching the hook to $pages may be the problem in your case, because Pages is different from Page in terms of the underlying PHP classes. A better approach would be this: wire()->addHookAfter('Page::path', null, 'hookPagePath'); 1
Vineet Sawant Posted January 23, 2014 Author Posted January 23, 2014 Make sure that you are putting that code somewhere that gets executed on every request, and before any code that needs to use it. Also, attaching the hook to $pages may be the problem in your case, because Pages is different from Page in terms of the underlying PHP classes. A better approach would be this: wire()->addHookAfter('Page::path', null, 'hookPagePath'); Hey Ryan, Thanks for the code. Can you please check following code and tell me why it's not working for me? wire()->addHookAfter('Page::path', null, 'hookPagePath'); function hookPagePath(HookEvent $e) { $page = $e->object; if($page->template == 'u_profile') $e->return = "/$page->name/"; } If it helps you to understand, I've page structure as follows: //example.com/profiles/apple where 'apple' has template 'u_profile'. I've placed above code in head.inc before any other code. Still //example.com/apple/ gives me 404 error.
Soma Posted January 23, 2014 Posted January 23, 2014 Can you clarify 'not working' ? If you do echo a url of a page with the template what do you get? Also sometimes a exit; in your method can help see if its really getting called.
Soma Posted January 23, 2014 Posted January 23, 2014 Ah just read end of post again.. mobile. So just to clarify. This hook only modifies the url returned from a $page->url. Not handle the request of that url. You only half way there. To catch that url which doesn't exist you would need enable url segments on home template and write a handler in template file. Check for first segment and if a name found render that page instead. For any other you throw a 404. 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now