25
CakePHP Framework Cake chefs: Mohamed Samir Moustafa Badawy

Introduction to CakePHP

Embed Size (px)

DESCRIPTION

The presentation is an introduction to cakePHP after our first project with cakePHP, we decided to share the knowledge.

Citation preview

Page 1: Introduction to CakePHP

CakePHP Framework

Cake chefs:

Mohamed Samir

Moustafa Badawy

Page 2: Introduction to CakePHP

Agenda

• Introduction.

• MVC model of cakePHP.

• Folders structure and configurations.

• Controllers, Models and Views.

• Final look on the model.

• A big demo.

Page 3: Introduction to CakePHP

Introduction

• Cakephp = Free + OpenSource

• Some Features:

– Active, friendly community

– Compatible with versions 4 and 5 of PHP

– MVC architecture

– Built-in validation

– Works from any web site directory, with little to no Apache configuration involved.

Page 4: Introduction to CakePHP

MVC

• CakePHP follows the MVC software design pattern.

– The Model represents the application data

– The View renders a presentation of model data

– The Controller handles and routes requests made by the client

Page 5: Introduction to CakePHP

MVC (cont.)

http://www.example.com/cakes/buy

Page 6: Introduction to CakePHP

MVC (cont.)http://www.example.com/cakes/buy

Page 7: Introduction to CakePHP

Folder Structure

• Cakephp folder:

– App

– Cake [The core file of cake is here]

– Vendors [ Third-party PHP libraries is here]

– .htaccess

– index.php

– README

Page 8: Introduction to CakePHP

Folder Structure (cont.)

• Any application:

Page 9: Introduction to CakePHP

CakePHP Conventions

• Conventions => you may feel that it will waste your time while it is actually saving it !! ?

• Because it gives you free functionality

• Conventions for:

– Model and database

– Controller

– Views

Page 10: Introduction to CakePHP

CakePHP Conventions (cont.)

• Any PHP file > underscored• Like> any_file.php

• Any Class > CamelCased• Like> anyClass

• Model classes > singular and CamelCased• Like > Person, BigPerson

• Database tables > plural and underscored• Like> persons, big_persons

• Controllers> CamelCased, and end ‘Controller’• Like>PersonController

Page 11: Introduction to CakePHP

CakePHP Conventions (cont.)

• Database table: • "people"

• Model class: • "Person", found at /app/models/person.php

• Controller class:• "PeopleController", found at /app/controllers/people_controller.php

• View template, found at• /app/views/people/index.ctp

• Using these conventions, CakePHP knows that a request to http://example.com/people/ maps to a call on the index() function of the PeopleController, where the Person model is automatically available (and automatically tied to the ‘people’ table in the database), and renders to a file. None of these relationships have been configured by any means other than by creating classes and files that you’d need to create anyway

Page 12: Introduction to CakePHP

CakeConfiguration

• Configurations needed:

– Database configuration – To be able to connect a database to your application by giving

the parameters needed like username…etc

– Core configuration – Here you configure application settings like sessions,

caching,…etc

– Routes configurations– Here you configure how to parse a Url and what actions to

take then.

Page 13: Introduction to CakePHP

Database Configurations

• Can be found at > app/config/database.php

Page 14: Introduction to CakePHP

Core Configurations

Variable Description

debug 0 = Production mode. No output.1 = Show errors and warnings.2 = Show errors, warnings, and SQL.3 = Show errors, warnings, SQL, and complete controller dump.

Session.save -php = Use the default PHP session storage.-cake = Store session data in /app/tmp-database = store session data in a database table.

Cache.disable When set to true, caching is disabled site-wide.

Session.table The name of the table (not including any prefix) that stores session information.

Session.*

Page 15: Introduction to CakePHP

Routes Configurations

• Found at > app/config/routes.php• Example:

Page 16: Introduction to CakePHP

Enough Configurations :)

• Main components structure:

• Controller classes structure

• Model classes structure

• Views structure

Page 17: Introduction to CakePHP

Controller classes structure

Controller

AppController

Any controller

shares its attributes and parameters with all its children controllers

Page 18: Introduction to CakePHP

Controller classes structure

• Attributes:

• Interacting with views & Model:

Model Interaction

View Interaction

Page 19: Introduction to CakePHP

Model classes structure

Model

AppModel

Any model

Page 20: Introduction to CakePHP

Model classes structure

• Example:

Page 21: Introduction to CakePHP

Views structure

• Example:

Header for all pages

Page content

Footer for all pages

Page 22: Introduction to CakePHP

Final view before the Demo

Controller Model

View

data

23

1

6

5

4

Find()Save(data)

set(data)Render(view)

Logic

Page 23: Introduction to CakePHP

Demo

Show time

Page 24: Introduction to CakePHP

References

• http://book.cakephp.org

• http://aidanlister.com/2009/05/creating-a-community-in-five-minutes-with-cakephp/

Page 25: Introduction to CakePHP

Thanks

Any Questions ?