Geoffrey Garbers Thoughts on everything from PHP to photography

Static pages in CakePHP

Posted 2 weeks from now.

I was doing some general Twittering this morning, and came across nuts-and-bolts-of-cakephp.com. I’ve been on this site quite a few times before, and I’ve really found the posts in it to be really useful.

However, there was a specific post that I came across that really piqued my interest. This was how to deal with static pages in CakePHP. You can read the post here. As useful as I found the post, I’m all for massive automation. So I did some more digging into the Router class, to figure out how I could wildcard this and use it for any static page (without having to manually specify the list of pages).

Unfortunately, due to the way the regular expressions for matching routes are compiled, it’s not possible. So, I came up with a more automated way of generating the list. It requires a bit more PHP processing than the manual specified by teknoid, but it makes the process far more automatic (assuming you always have a view file in VIEWS/pages for the page).

$availablePages = glob(VIEWS . 'pages' . DS . '*.ctp');
if ($availablePages) {
	$extensions = array_pad(array(), count($availablePages), '.ctp');
	$availablePages = array_map('basename', $availablePages, $extensions);
	Router::connect('/:page', array('controller' => 'pages', 'action' => 'display'), array('page' => implode('|', $availablePages), 'pass' => array('page')));
}

And that, right there, is my more automated solution. It now helps with the automagic creation of static page URLs, without the “/page/page-name” in the URL.

Thanks to teknoid for the original blog post.

Comments

Jparker 1 month from now
Hi there,

I found teknoid post, and then your answer. I liked yours because of the automation thing and that I'm currently working on a project that has a lot of static pages.

I adapted it to have it working with folders hierarchy in pages folder.

The code is not the cleanest, but it works...
Here is the code :

$folder = new Folder();
$available = $folder->tree( VIEWS . 'pages' . DS, true );
$availableFolder = $available[ 0 ];
$availablePages = $available[ 1 ];
if( $availablePages ) {
$extensions = array_pad( array( ), count( $availablePages ), '.ctp' );
$availablePages = array_map( 'basename', $availablePages, $extensions );
}
if( $availablePages ) {
if( $availableFolder ) {
foreach( $availableFolder as $key => $folder ) {
$availableFolder[ $key ] = substr( $folder, strlen( VIEWS . 'pages' . DS ) );
}
Router::connect(
'/:folder/:page.html', array( 'controller' => 'pages', 'action' => 'display' ), array(
'folder' => implode( '|', $availableFolder ),
'page' => '(?i)' . implode( '|', $availablePages ),
'pass' => array( 'folder', 'page' )
)
);
}
Router::connect( '/:page.html', array( 'controller' => 'pages', 'action' => 'display' ), array( 'page' => implode( '|', $availablePages ), 'pass' => array( 'page' ) ) );
}
Geoff Garbers 1 month from now
That's a great idea, Jparker. Thanks for that!

Leave a reply

Your email address will not be published. Required fields are marked *

*