Home subpages (remove home in the URL)

Hi,

I created some subpages on the homepage.
I used also the code bellow in the config.php

c::set('routes', array(
	array(
		'pattern' => '(:any)',
		'action'  => function($uid) {
			$page = page($uid);

			if(!$page) $page = page('home/' . $uid);
			if(!$page) $page = site()->errorPage();

			return site()->visit($page);
		}
	),
	array(
		'pattern' => 'home/(:any)',
		'action'  => function($uid) {
			go($uid);
		}
	)
));

Now my URL is www.xxx.com/xxx instead of www.xxx.com/home/xxx

My question, when I use $page->url() in my code it still gives me the URL with de home directory.
Also in my Sitemap file.

What can I do to remove the home directory and to be sure no errors for indexing these pages?

Thanks.

You can create a page model that overwrites the default url() method for the child template.

<?php
class ChildtemplatePage extends Page {
  public function url() {
    return $this->site()->url() . '/' . $this->uid();
  }
}

Replace “Childtemplate” with the name of the child template.

1 Like

Interesting approach, thank you!

I am trying to solve the same problem as the original poster.

I am trying to extend Page with HomePage (home.php), but neither does the extended url() function return the new url style, nor does an additional function test() return anything.

This is the content structure:

content/
|-- 1-about
|   `-- about.txt
|-- error
|   `-- error.txt
|-- home
|   |-- 20170514
|   |   `-- article.txt
|   |-- 20170513
|   |   `-- article.txt
|   |-- 20170512
|   |   `-- article.txt
|   `-- home.txt
`-- site.txt

This is the Site structure: (some directories omitted)

site
|-- blueprints
|   |-- article.yml
|   |-- blog.yml
|   |-- default.yml
|   |-- error.yml
|   |-- home.yml
|   `-- site.yml
|-- controllers
|   `-- blog.php
|-- models
|   `-- home.php
|-- plugins
|   `-- index.html
|-- snippets
|   |-- footer.php
|   |-- header.php
|   |-- listing.php
|   `-- menu.php
`-- templates
    |-- blog.php
    |-- default.php
    `-- home.php

For the reasons stated above, my impression is the page model /models/home.php is not executed, but if I include an deliberate syntax error, I do get an error (debugging enabled).

What can I do to correctly override the url function in the model? Thank you so very much!

The home.php model only overwrites the URL when used regarding the home page. This is probably not what you want, because you want this to affect the child pages, not the home page itself. So you would have to create the model for the child pages, i.e. create a model called article.php?

1 Like

Ahaa! Yes, that was very helpful. It worked. Thank you!