It’s been a long journey and it has finally come to an end. Due to professional changes in my life, it is time that I stop using a PHP based MVC framework for web applications. Where will I go, you ask? Its simple – Ruby on Rails! While the aim of this post is to not convert people who use CakePHP to start using something else, I do plan on elaborating some of my reasonings for this switch.
1. My New Job
It cannot make much more sense than this. I recently got a new job (started Feb 1st). The place I work is a pure ruby shop and support several financial based web applications. I had 10 days to learn the syntax/basics of Ruby and loved every minute of it. I was ready for something new and fresh in my life.
2. Maturity
Rails was first! As a result, the project has matured more and seems to have a lot more support.
3. Package / Plugin management
Ruby has RubyGems, which is amazing. CakePHP just released something to handle plugins, but I have a feeling it is not a full package manager (think apt!).
4. Other Little Differences
Several small differences make it a better choice. I found a recent presentation that quickly explains a few of them.
That being said, PHP with CakePHP and Ruby with Rails both have their pros and cons. Each framework will always have its place in my heart. Hopefully I will have time soon to setup a rails environment and start pushing through some personal projects! Until then, I will continue gaining experience at work whilst I triage the problems that I was hired to fix
Mocha is written entirely in CakePHP and jQuery and is certainly the best CakePHP eCommerce app out there. Why do I say that?
Mocha Shopping Cart is…
If you still don’t believe me, Mocha is offering a limited time only Pre-Launch coupon code that gives you the first month FREE. You can try it for yourself and see how you like it. I think you will agree that it beats CakeCart and BakeSale, HANDS DOWN! And no, Mocha is not free (after the first 30 days). The costs help to speed up development, pay for server and hosting fees, and pay for that cup of coffee that us developers need when its 8 AM and we are having to talk with <insert_API_name_here> first thing in the morning.
Official Website: Mocha Shopping Cart
Blog with coupon code: Get a free trial of Mocha Shopping Cart
I am not sure if this is the fanciest way to get the job done but it works REALLY well. What job am I referring to? I am referring to outputting data as JSON in a CakePHP view and then parsing it in a jQuery AJAX response. Its fairly easy to accomplish with the JSON parser provided by json.org. Scroll to the bottom and download/include json2.js.
Controller example:
$sizeArray = getimagesize('/path/to/really_cool_image.jpg');
$outputArray = array(
'file' => 'really_cool_image.jpg',
'width' => $sizeArray[0],
'height' => $sizeArray[1],
);
$this->set('result', $outputArray);
$this->render(null, 'ajax');
AJAX View example:
Configure::write('debug', 0);
echo $javascript->object($result);
jQuery AJAX response handler example:
function(response, status) {
imageDetails = JSON.parse(response);
imageName = imageDetails.file;
}
There are currently three methods for including and using models inside your controller. I am referring to the case when your controller does not belong to a model or or the model is not related to the model inside the controller you are working in. Although this case should be extremely rare, it seems to happen to me a lot. This post is basically a re-summary of Gwoo’s response in the CakePHP Google Groups discussion.
1. Controller::loadModel();
This method actually calls ClassRegistry::init() and saves the instance of the model as a parameter in the controller.
Example:
Controller::loadModel('Car');
$cars = $this->Car->find('all');
2. ClassRegistry::init()
This method creates an instance of the Model and returns it for use.
Example:
$car = ClassRegistry::init('Car');
$cars = $car->find('all');
3. App:import() (not recommended)
This method only includes the file. This requires you to create an instance each time.
Example:
App::import('Car');
$car = new Car();
$cars = $car->find('all');
As you can see this is really simple and basic stuff. However, I felt the need to re-post this information. If for nothing else, I will be using this as a reference point for myself
Update 5/27/09: Miles Johnson has a nice write up on this on his blog
I was having some issues today with character encoding and I thought I’d post about it. There are three elements that you need to make sure you have if you want to properly use UTF-8 character encoding in your Cake app. My issue was that I did not have #2, although adding #1 didn’t hurt either.
1. Database Config
Add ‘encoding’ => ‘utf8′ to app/config/database.php
var $default = array( 'driver' => 'mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'user', 'password' => 'password', 'database' => 'database', 'encoding' => 'utf8' );
2. Make sure your layout has the charset line in the
<head> <?php echo $html->charset(); ?> Other elements... </head>
3. This one is set by default, but make sure App.encoding is set in app/config/core.php
/**
* Application wide charset encoding
*/
Configure::write('App.encoding', 'UTF-8');
And thats it!