Welcome

Hello my name is Andrew Allen, freelancer and student in the state of Colorado. This is my little contribution to the CakePHP community. I've long been sucking the intelligence of men much smarter than I, and I've decided to share what little tidbits of knowledge that I can with anyone who happens to grace my pages.

Much in the same fashion of Felix Geisendörfer I feel that I too (along with my new semester starting next monday) will start blogging again. I'm going to throw out whatever Cake tidbits that I can to see if any of it become useful to anyone. All of this will culminate in the release of a behavior that provide you the ability to treat remote xml feeds as you would regular models.

This being the first real post on Life Is Cake I would like to take this opportunity to say that any code released here is licensed under the MIT license unless otherwise stated.

One of the things I often deal with are seemingly insignificant requirements for page url's. However, that said, the client is always right. One of my recent projects asked me to modify the urls so that prefix routing would occur based on a variable in the session ("Auth.User.type"). If the user is logged in as admin and goes to /posts/edit/2 they get routed as if they went to /admin/posts/edit/2. This (in the minds of the client) meant that it was more secure. After attempting to assure them that that was not the case and that it would be secure either way I started drafting a little code.

What I did was essentially override the default route that is implied at the bottom of /config/routes.php with a route that pulled the user's type in and I arrived at this (rather hackish) solution at the bottom of my router file.

<?php
/*
... Some routes that are important to your project
*/
/**
* Connect the prefixes
*/
$session = new CakeSession();
$session->start();
$prefix = @$session->read("Auth.User.type"); //Don't show any warning if it doesn't exist
Router::connect('/:controller/:action/*', array('prefix' => $prefix));
Configure::write('RouterPrefixing', true); //Used possibly to check against in some of the controllers

?>

One of the unforeseen benefits of this is that it's no matter if Router::url decides to forget to put the prefix onto the link destined for the user, as it will get routed magically to the right place based on their user type.

Day 1 – check.

Sincerely,~Andrew Allen