CakePHP’s built-in Pages Controller

CakePHP 1.2 comes with in built PagesController which helps you easily create static pages. It’s easy to set up, you just have to put your pages view in /views/pages/. All the codes are for routes.php.

[code lang=”php”] ‘pages’, ‘action’ => ‘display’, ‘home’));
?>[/code]

The page title ($pageTitle) would be ‘Home’ for the above.

If you want your URL to be /about/my-company, you can do the following:

[code lang=”php”] ‘pages’,
‘action’ => ‘display’,
‘about’,
‘my_company’,
)
);
?>[/code]

The page title would be ‘My Company’ this time since underscores are converted to spaces. Pretty useful.

Finally did some permenant redirection work

This is long overdue work, just one line, okay two lines of code, but it took me two months to get this done. I always wanted to redirect justrealized.com to i.justrealized.com because some people prefer to type ‘justrealized’ and hit CTRL+Enter on their browsers.

Anyway, this is more of a personal note. To do a permanent redirect using htaccess, you create a file called ‘.htaccess’ with the dot in front and upload it to your Apache web server. Make sure you have the modrewrite enabled. Put the following lines into your .htaccess file and change i.justrealized.com to your new domain.

[code lang=”apache”]RewriteEngine on
RewriteRule (.*) http://i.justrealized.com/$1 [R=301,L][/code]

301 means permanent redirect. There are lots of ways to do permanent redirects but .htaccess is the most straight forward method if you have the mod_rewrite working for you.

[ad#simple]

This redirects all your old URLs to your new domain.

How to perform routing in CakePHP 1.2

CakePHP introduces a new way of writing your routes, you can now restrict your URLs at parameter level, meaning that we can now specify URLS like /date/:year/:month instead of /date/*. This post compares CakePHP 1.1 and 1.2 routing differences. I’ll start with showing the old way first:

CakePHP 1.1 way

In CakePHP 1.1, routings looks like this in /app/config/routes.php:

[code lang=”php”]Router->connect(‘/date/*’, array(‘controller’ => ‘posts’, ‘action’ => ‘p_date’));[/code]

And the corresponding method in the controller would look something like this:

[code lang=”php”]
function p_date($year, $month) {
$posts = $this->Post->findAll(
“MONTH(pub_date)=$month AND YEAR(pub_date)=$year”
);
$this->set(‘posts’, $posts);
}
[/code]

CakePHP 1.2 way

There isn’t much changes in CakePHP 1.2’s routing handling statement except that now it uses 2 colons.

[code lang=”php”]Router::connect(‘/date/*’, array(‘controller’ => ‘posts’, ‘action’ => ‘p_date’));[/code]

No change in the controller’s method. Continue reading “How to perform routing in CakePHP 1.2”