Interesting discoveries #06

Bunch of links this post is. Here lies key things I have read:

1. Marriage equality, same-sex

Ruling that same sex marriage is unconstitutional in America. The New Yorker has a good summary on the key arguments for same-sex marriage.

There really is very little you can argue against same sex marriage without sounding like a selfish jerk.

2. Dog loves guitar

This is too cute to not watch:

Dogs make me laugh.

3. White noise to increase your creativity

This is a website with audio that mimics a cafe. Some Singaporeans would say they get more productive at Starbucks or the likes. Really? Perhaps so. Research has suggested some white noise to be good for creativity. Although I don’t really believe the assessment, here’s a coffee shop background sound you may need one day.

4. On why I start not reading news news

At some point of time in my life, I realized it’s just impossible to catch up with all the news in the world. They are things that are nice to know that is happening but I argue that it is less important than you think. Important news come to you in the old fashion way — word of mouth.

Today’s mainstream news is filled with articles that embraces the negative aspects. There’s always an accident here, a murder case, embezzlement etc.. There just isn’t enough good news except for our Prime Minister planting a lovely bush at the gardens to commemorate some special day that the generation in the future will call history.

News isn’t all that bad though. Just read lesser. For me, it’s a lot lesser. And stop reading about mishaps of others and surround yourself with more positivity. Read social news briefly. Then do your stuff in life. Don’t ever read The Straits Times.

5. The worst URL

Channel News Asia wins this hands down. What is this:

www.channelnewsasia.com/stories/singaporebusinessnews/view/1262412/1/.html

I’m talking about /.html.

Come on, you can do better than this. What kind of CMS output such garbage?!

How to enable Apache mod rewrite in Ubuntu

Actually this probably works for all Linux distributions with Apache 2. Okay enabling mod_rewrite is really easy, you just have to use a2enmod (Apache 2 enable module):

[code lang=”bash”]sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart[/code]

To disable an Apache 2 module, you can run:

[code lang=”bash”]sudo a2dismod rewrite
sudo /etc/init.d/apache2 restart[/code]

That does the reverse.

With mod_rewrite enabled, you can use the .htaccess file.

By the way in your directory permissions, you must set AllowOverride to “all” like this:

[code lang=”ini”]
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
[/code]

Hope it helps.

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”