19-04-2011, 11:52 AM
CakePHP.ppt (Size: 285.5 KB / Downloads: 67)
PHP HyperText Preprocessor
PHP’s support for Apache(Server) and MySQL(Database) further increases its popularity. Apache is now the most-used web-server in the world. MySQL is a powerful free SQL database, and PHP provides a comprehensive set of functions for working with it. The combination of Apache, MySQL and PHP is all but unbeatable.
CakePHP
History of CakePHP
In 2005, Michal Tatarynowicz wrote a minimal version of a Rapid Application Framework in PHP. He found that it was the start of a very good framework. Michal published the framework under the MIT license, dubbing it Cake, and opened it up to a community of developers, who now maintain Cake under the name CakePHP.
Why CakePHP?
The MVC Pattern
Controllers contain the logic of your application. Each controller can offer different functionality; controllers retrieve and modify data by accessing database tables through models; and they register variables and objects, which can be used in views.
Models are active representations of database tables: they can connect to your database, query it (if instructed to do so by a controller) and save data to the database. It is important to note that in order to correctly apply the MVC architecture, there must be no interaction between models and views: all the logic is handled by controllers.
Views can be described as template files that present their content to the user: variables, arrays and objects that are used in views are registered through a controller. Views should not contain complex business logic; only the elementary control structures necessary to perform particular operations, such as the iteration of collected data through a foreach construct, should be contained within a view.
Cake File Layout
App Layout
Installing CakePHP
Server Requirements
Here are the requirements for setting up a server to run CakePHP:
1. An HTTP server (like Apache) with the following enabled: sessions, mod_rewrite (not absolutely necessary but preferred)
2. PHP 4.3.2 or greater. Yes, CakePHP works great in either PHP 4 or 5.
3. A database engine (right now, there is support for MySQL 4+, PostgreSQL and a wrapper for ADODB).
To download a stable version of code, check out the files section of the CakePHP project at CakeForge by going to http://cakeforgeprojects/cakephp/.
Setting Up CakePHP
For development we can place the whole Cake installation directory inside the specified DocumentRoot like this:
/wwwroot
/cake
/app
/cake
/vendors
.htaccess
index.php
NOTE: /app/tmp must be writable by the user that your web server runs as.
Database Configuration
A fresh install doesn't have a database.php, so you'll need to make a copy of /app/config/database.php.default. Once you've made a copy and renamed it you'll see the following:
Example: app/config/database.php
var $default = array('driver' => 'mysql',
'connect' => 'mysql_connect',
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'project_name',
'prefix' => '');
Routes Configuration
Routes are individual rules that map matching URLs to specific controllers and actions. Routes are configured in the app/config/routes.php file. They are set-up like this:
Example:
<?php $Route->connect ( 'URL', array('controller'=>'controllername', 'action'=>'actionname', 'firstparam') ); ?>
Example
If you now try to access your application, the CakePHP default page will be displayed, warning you that a database connection could not be established. We need to create a new MySQL database named memo that's accessible by a user named root
Create a table named notes
CREATE TABLE notes ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), body TEXT, created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL );
Now If you refresh the default page, CakePHP will notify you that the database is now accessible
CakePHP is now configured properly, and we can turn to the development of our application
Model
Create a file named note.php and save it in your /app/models/ directory. You need only include the following lines in it:
<?php class Note extends AppModel { var $name = 'Note';
} ?>