CakePHP is an open-source PHP framework used for developing web applications. The document outlines how to install CakePHP using Composer, create a project, configure databases, implement CRUD functionality, use caching, and deploy applications. Key aspects include using Composer to install dependencies, setting the database connection in config/app.php, generating controllers and models with bake, and clearing cache groups when data changes.
2. Requirements
1. PHP 5.6.0 or Greater
2. Webserver (Apache, Nginx) preferably or Microsoft IIS
3. MySQL (5.1.10 or greater), PgSQL, Microsoft SQL Server, SQLite 3
4. PDO is default required.
3. Installation
1. Install composer
2. Once the installation is done you need to move your composer.phar file to
/usr/local/bin/composer
3. You can also install cakePHP using Oven
4. Oven is a simple PHP script which checks necessary system requirements
and will help install CakePHP with click.. Click.. Click⊠Please refer
https://github.com/CakeDC/oven to install it.
4. Create a CakePHP Project
=> If your composer has been installed globally on your server or local machine
please use âcomposer self-update && composer create-project --prefer-dist
cakephp/app my_app_nameâ else use âphp composer.phar create-project --
prefer-dist cakephp/app my_app_nameâ
=> my_app_name is the name of the directory/project.
=> Now if you will run localhsot/my_app_name you can see the default
application running on your local machine/server.
5. Few other things to note
1. Make sure mode_rewrite is enabled
2. If you wish to keep your codebase up to date with latest CakePHP changes,
you need to change the CakePHP version in composer.json. Like wise
â"cakephp/cakephp": "3.4.*" to â"cakephp/cakephp": "3.x.*", so that when
you update your composer by composer update you will get the latest fixes
on your local machine/server.
3. You need to have/install correct PDO extension for database that you are
using.
6. Database Configuration
=> Go to config/app.php, You can set the host, database, username and
password. The actual connection information is fed in
âCakeDatasourceConnectionManager â.
=> You can also define as many connection as you want using
âCakeDatasourceConnectionManagerâ.
=> In this connection manager you can also set timezone, connection type
wheather to keep it persistent or not.
8. Manage Connections
1. Using CakeDatasourceConnectionManager, you can access the
connection available in your application
2. You can get the connection using
âCakeDatasourceConnectionManager::get($name)â. $name can be
default or any other cases that you have declared.
3. You can also create new connection at run time using config() and get()
methods.
4. âConnectionManager::config('my_connection', $config);
9. Simple CRUD with and without Bootstrap
1. Create Controller with proper naming convention.(XxxsController.php)
2. Create a Model File (XxxTable.php)
3. Create template file (CTP) inside Template (Xxxs/index.ctp)
4. Change default page in routes.php
5. Using Bootstrap, for sample lets see (http://bootswatch.com)
6. Lets checkâŠâŠ..
10. Middleware
CakePHP provides several middleware few of them are :
1. CakeErrorMiddlewareErrorHandlerMiddleware (
=> It traps exceptions from the wrapped middleware and renders an error
page using the Error & Exception Handling Exception handler.
1. CakeRoutingAssetMiddleware
=> It checks the request are are for cake theme or from plugin assets,
wheateher its on themes webroot or in plugins webroot
12. Deployment
1. To implement the project in 2 or 3 different server, you have to send the
codebase through git and rest thing can be done by composer (Composer
install or Composer Update). It automatically check the dependencies and
folder permission etc.
2. If anyone working on FTP, he has to upload the library manually and have
to give the folder permission as expected.
3. You can set the debug mode True/False based on server to server by
environment variable in your apache settings.
13. Deployment
SetEnv CAKEPHP_DEBUG 1 (In your apache configuration)
And then you can set the debug level dynamically in app.php:
$debug = (bool)getenv('CAKEPHP_DEBUG');
return [
'debug' => $debug,
];
14. Deployment
4. To increase your application performance in production once deployment is
done through
php composer.phar dumpautoload -o
After doing this you need set your js/css/images etc path (webroot) through
symlink
bin/cake plugin assets symlink (This command will symlink your webroot
directory)
15. Cache
1. Default CakePHP cache flused once in a year, but you can manage it
through config/app.php
2. We can also set the cache for schema description and table listings.
3. We can also flush individual cache if we know the key
Cache::delete() will allow you to completely remove a cached object
Cache::delete('my_key');
16. Cache
4. If you wish to clear all cache data that may be configured in APC, Memcache
etc
Cache::clear(true); (Will only clear expired keys.)
Cache::clear(false); (Will clear all keys.)
5. We can also use Cache to store/remove common find data. Example :
18. Cache
6. Enable or Disable Cache globally
Cache::disable(); (Disable all cache reads, and cache writes.)
Cache::enable(); (Re-enable all cache reads, and cache writes.)
19. Other Important thing
1. You can create your own data type in CakePHP using toPHP, toDatabase,
toStatement and marshal, once you are done you can map your Custom
data type to SQL expression
(https://book.cakephp.org/3.0/en/orm/database-
basics.html#CakeDatasourceConnectionManager)
2. In CakePHP DRY(Donât repeat yourself) is used to allow you to code things
once and re-use them across your application.
3. CDN is default configured you can define the path of cdn in configuration
20. Other Important thing
4. How to write custom sql query
use CakeDatasourceConnectionManager;
$connection = ConnectionManager::get('default');
$results = $connection
->execute('SELECT * FROM articles WHERE id = :id', ['id' => 1])
->fetchAll('assoc');
22. 5. Create Relationship between Tables
Relationship Association Type Example
one to one hasOne A user has one profile.
one to many hasMany A user can have multiple recipes.
many to one belongsTo Many recipes belong to a user.
many to many hasAndBelongsToMany Recipes have, and belong to, many
ingredients.
23. Example :
class IngredientsTable extends Table
{
public function initialize(array $config)
{
// have additional data on your IngredientsProducts table
$this->belongsToMany('Products', [
24. class ProductsTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Ingredients', [
'through' => 'IngredientsProducts',
]);
}
25. class IngredientsProductsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Ingredients');
$this->belongsTo('Products');
}
}
26. Migration
Laravel VS Cakephp
Migration needs to be enabled through composer, by default its enabled
through the setup
Else
Plugin::load('Migrations'); in your bootsrap.php file
27. Once you have created in the config/Migrations folder, you will be able to
execute the following migrations command to create the table in your database:
bin/cake migrations migrate