Convert Microsoft to LINUX: CakePHP MVC Framework

There has been tremendous progress in the PHP world in the last five years. CakePHP is a program that can very quickly generate PHP Code for you, creating modules and functions in an object oriented programming format. It is very easy for you to modify this code. It is based on the Model, View, Controller (MVC) philosophy:

MVC was first described in 1979 by Trygve Reenskaug , then working on Smalltalk at Xerox PARC.

To install CakePHP get all the usual LAMP modules, i.e.:
apt-get install apache2 php5 mysql-client mysql-server php-mysql
Restart Apache2 to be safe with the command:
/etc/init.d/apache2 reload
mkdir /var/www/cakephp
Fetch the tarred file with the command below:
wget http://cakeforge.org/frs/download.php/734/cake_1.2.5.tar.gz
Note, your version may be newer, at the time of this writing 1.2.5 was the most recent version.
tar -zxvpf cake_1.2.5.tar.gz
After untaring using the above command, this will create a new directory cake_1.2.5/ Now, type:
mv cake_1.2.5/* cake_1.2.5/.htaccess /var/www/cakephp
to move all the cakephp files and the .htaccess file to the web directory above. The CakePHP documentation is really good so I suggest following it to install a practice blog using this. Follow all the procedures you used when installing Drupal and Joomla to create a SQL database, define a user and a password. You can see this by clicking on the Drupal link. Note, a few of the modules have blank lines in the PHP scripts. Make sure you delete all the blank lines especially at the end of the PHP page as this can cause problems later. Note, once you are all done, you can remove the sql query line at the bottom by going to the /var/www/cakephp/app/config/core.php and changing the debug level to 0 as in:
Configure::write('debug', 0);
You will also need to modify the routes.php file to make it point to your database and not the default built in one for displaying CakePHP documentation as in:
Router::connect ('/', array('controller'=>'posts', 'action'=>'index'));
Lastly for your reference here are all the files documented that I used . Note, all the files are under the /var/www/cakephp directory:
/app/views/posts/index.ctp
/app/views/posts/add.ctp
/app/views/posts/edit.ctp
/app/models/post.php
/app/controllers/posts_controller.php
/app/config/core.php
/app/config/routes.php
/app/views/posts/view.ctp

Note, I had trouble getting the CakePHP program to recognize that I had Mod_Rewrite enabled in Apache2. So I used the known work around that you will see in the core.php file:
* To configure CakePHP *not* to use mod_rewrite and to
* use CakePHP pretty URLs, remove these .htaccess
* files:
*
* /.htaccess
* /app/.htaccess
* /app/webroot/.htaccess
*
* And uncomment the App.baseUrl below:
*/
Configure::write('App.baseUrl', env('SCRIPT_NAME'));

Once you have mastered the above. The next step is to use the built in bake code generator. It is amazing what you can do. Here is an incredibly well written book on it.
Your first command at the terminal will be:
cake bake -app /var/www/cakephp/app
If you did not create a database.php file, start off by selecting D for Database configuration file
Next, select C for Controller file
When it asks: Would you like to use scaffolding? (y/n) , select [n] for no .
When it asks: Would you like to include some basic class methods (index(), add(), view(), edit())? (y/n) , select [y] for yes.
Say no to admin routing , when prompted.
Say no to helpers and componets and yes to sessions when prompted.
Next, you will create the Model .
Say Yes to validation putting in appropriate values, i.e. body must be not-empty etc.
Say No to model associations for now, keeping it simple. You can modify later.
Next, you will create the View .
Say No to scaffolding , when it asks you. For a camel cased Function Name pick something like:
Action Name? (use camelCased function name) > AddPost You will then see:
The following view will be created:
---------------------------------------------------------------
Controller Name: Posts
Action Name: AddPost
Path: app/posts/add_post.ctp
---------------------------------------------------------------
The last thing to do, is make the tmp directory under app be writeable with:
/var/www/cakephp/app/# chmod 777 -R tmp/
To launch type:
http://localhost/cakephp/index.php Note, this is similar to what we did, by hand earlier. I am attaching all the files here so you can compare them.
Enjoy!