Jump to content

Remove parents from url?


pwFoo
 Share

Recommended Posts

Hello,

I've just had little time... so I write a short post and come back later ;)

I thinking about organize my (visible) pages for example at "/content/*".

So I'll get links like "/content/about" or "/content/myPage". Is it possible to create a path alias like "/about" or "/myPage" without the parent "/content/"?

Finally I try to get a tree like this.

[home]

/content/

/tags/

/categories/

/settings/

  • Like 1
Link to comment
Share on other sites

Link to comment
Share on other sites

i do this sort of thing now a lot, thanks to Ryan's CMS Critic Case Study; this is almost the same as WillyC's code at the above link

/**
 * This hook modifies the default behavior of the Page::path function (and thereby Page::url)
 *
 * The primary purpose is to redefine blog posts to be accessed at a URL off the root level
 * rather than under /posts/ (where they actually live). 
 *
 */
wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  if($page->template == 'post') {
    // ensure that pages with template 'post' live off the root rather than '/posts/'
    $event->replace = true;
    $event->return = "/$page->name/";
  }
});
 
  • Like 9
Link to comment
Share on other sites

  • 4 months later...

i do this sort of thing now a lot, thanks to Ryan's CMS Critic Case Study; this is almost the same as WillyC's code at the above link

/**
 * This hook modifies the default behavior of the Page::path function (and thereby Page::url)
 *
 * The primary purpose is to redefine blog posts to be accessed at a URL off the root level
 * rather than under /posts/ (where they actually live). 
 *
 */
wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  if($page->template == 'post') {
    // ensure that pages with template 'post' live off the root rather than '/posts/'
    $event->replace = true;
    $event->return = "/$page->name/";
  }
});
 

Great stuff, one question, where would I place this to make it work?

thanks!

Link to comment
Share on other sites

A convenient way would be to put the code in a file called _init.php. Create this file in your template directory and edit site/config.php to enable _init.php. The lines in config.php you need to look for are about 'prependTemplateFile', around line 45, read simple instruction.

For some more ideas and insights you can, as Macrura mentioned, look at https://processwire.com/talk/topic/3987-cmscritic-development-case-study/

  • Like 2
Link to comment
Share on other sites

  • 10 months later...

Finally getting around to working on this. But running into trouble.

I have a site that is very important business wise for me, (it does very well in the search engines) and it uses .html extension for all pages directly off root

It now still runs on modx evolution but now is the time to port to pw. I am not ready to try out redirects because no one will guarantee that this will give me the same serp results as I am getting now, so that is not an option.

I have got one part working on a test site, which is getting the url to link to show each article living directly off root, I used this code in _init.php:

wire()->addHookBefore('Page::path', function($event) {
  $page = $event->object;
  if($page->template == 'article') {
    // ensure that pages with template 'article' live off the root rather than '/posts/'
    $event->replace = true;
    $event->return = "/$page->name.html";
  }
}); 

So far so good: when I go to the home page (and any other page: it shows all links to articles as living off root with an .html extension.

But this is where it stops working.

I set allow urlsegmetns to yes on home.php template and added the following to home.php template:

if(strlen($input->urlSegment2)) {
  // we only accept 1 URL segment here, so 404 if there are any more
  throw new Wire404Exception();

} else if(strlen($input->urlSegment1)) {
  // render the blog post named in urlSegment1 
  $name = $sanitizer->pageName($input->urlSegment1);
  $post = $pages->get("/tuning-tips/")->child("name=$name");
  
  if($post->id) echo $post->render();
    else throw new Wire404Exception();
    }

But I keep getting 404 errors, it does not show the page. when going to the /directly-off-root.html url.

My setup on the site is like follows:

home

=>/category1toplevelpage/

=> =>/category1toplevelpage/article1

=> =>/category1toplevelpage/article2

=> =>/category1toplevelpage/article3

=> =>/category1toplevelpage/articleetcetc

=>/category2toplevelpage/

=> =>/category2toplevelpage/article1

=> =>/category2toplevelpage/article2

=> =>/category2toplevelpage/article3

=> =>/category2toplevelpage/articleetcetc

What code should I put where to get this to work properly?

Help much appreciated! Thanks.

Link to comment
Share on other sites

Your selector is wrong "name=$name" would be parsed to "name=directly-off-root.html", but your page's name is still only "directly-off-root" without the html. As you're not using not html'ed names I would suggest just adding it to the name of the pages directly. Your hook to Page::path just changes to url on runtime, but does not modify the "name" field your selector checks for. 

To keep your code clean it's also best to not couple two different things, appending ".html" and masking out a parent page, into a single hook. Then you'll see that Page::path isn't the right place to handle your first issue. 

Link to comment
Share on other sites

@LostKobrakai, thanks, I tried it all without the .html extension and still no luck, any ideas how to get this working?

I am not a coder, I can only copy paste, that is probably why I do not get this going yet.

Anybody willing to get access to the test site to get it working, I will then do a proper write up for other dummies like me who might have use for it.

Thanks!

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