Cake PHP Tutorial Day-2

Posted: July 20, 2014 in Uncategorized

<< Go to Cake PHP Tutorial Day-1 Part-1
<< Go to Cake PHP Tutorial Day-1 Part-2


Hopefully, you have created all tables in the database. Now, we are going to create Models, Views and Controllers.

1. Create Model files at tut/app/Model
2. All Model classes, we are going to create should extended from class “AppModel”



3.  Now, lets start creating Controllers & Views for each section of our application.


To begin we need to create a new controller file called FormatsController.php and place this in the controllers directory.

Generally a Controller takes care of all the business logic of the application with each method inside the file taking care of an action that the system does.

For example each method inside a controller corresponds to a URL of the system, if a user visits localhost/admin/formats/add then the logic inside the admin_add() method of the Formats Controller would be processed. This makes it very easy to organise and maintain your code and is one of the main benefits of using the MVC software pattern.

When creating a new controller I like to code all of the methods I’m likely to use and for the Formats Controller I will need the following:

For User:

  • index()
  • view()

For Admin:

  • admin_index()
  • admin_add()
  • admin_edit()
  • admin_delete()


Next, we need to create new View files called index.ctp and admin_index.ctp in the/app/View/Formats/ folder. The views files are pretty basic and will output the formats in a table.

Note: .ctp indicated CakePHP Template

To access, you can browse localhost\tut\admin\formats\index or localhost\tut\admin\formats
This will execute admin_index() function in the controller and the view “admin_index.ctp” will be called.
Before browsing, open app\Config\core.php and uncomment line No: 152 as shown below;

Now, browse  localhost\tut\admin\formats\index
If your list shows empty, then do not worry. Because, you have no records in “formats” table yet.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.