Jump to content

Custom URLs - Getting example.com/somename


Vineet Sawant
 Share

Recommended Posts

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.

Link to comment
Share on other sites

@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!

Link to comment
Share on other sites


$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.

Link to comment
Share on other sites

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'); 
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

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.

Link to comment
Share on other sites

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.

  • Like 2
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

×
×
  • Create New...