How to Create a Small Web Application with Cakephp on a VPS (Part 1)
Edit Article

  • 1 Editor

CakePHP is a free open source framework for PHP. CakePHP has integrated with several set of structural libraries and classes. It allows the programmer creating web applications.


Let us first find out how cakephp can be useful...

Why cakePHP?

  1. Open source and Active
  2. PHP4 and PHP5 support
  3. Integrated with CRUD database
  4. Robust
  5. Fast, secure, reliable and flexible control lists.
  6. Model View Controller(MVC)
  7. Supports for AJAX,HTML Forms,Javascript and more
  8. High Security
  9. Data Sanitization and flexible caching
  10. Works in site sub-directory with NO Apache configuration

INTRODUCTION:

I am giving a small introduction which allows you to know about MVC concept. MVC is completely and perfectly integrated with the cakePHP patterns. This chapter is definitely for you.

BASIC CONCEPT:

Model View Controller:


Model View Controlle allows the users logically separate the code. This makes it more stable, reliable , maintainable and better.

Input -> Processing -> Output
Controller -> Model -> View

Hope now you have understood the basics ...

OVERVIEW:

When you unpack cakePHP you can see these three main folders.

app
cake
vendors

app folder has your application specific files and folder. cake folder has core libraries and there is no need to touch the folder. vendors have third-party libraries. You can see the file layout below,

/app
    /config          - Contains config files for your database, ACL, etc.

    /controllers     - Controllers go here
        /components  - Components go here

    /index.php       - Allows you to deploy cake with /app as the DocumentRoot

    /models          - Models go here

    /plugins         - Plugins go here

    /tmp             - Used for caches and logs

    /vendors         - Contains third-party libaries for this application

    /views           - Views go here
        /elements    - Elements, little bits of views, go here
        /errors      - Your custom error pages go here
        /helpers     - Helpers go here
        /layouts     - Application layout files go here
        /pages       - Static views go here

    /webroot         - The DocumentRoot for the application
        /css
        /files
        /img
        /js

/cake                - Cake's core libraries. Don't edit any files here.

index.php

/vendors             - Used for server-wide third-party libraries.

VERSION.txt          - Let's you know what version of Cake you're using.

SERVER REQUIREMENTS:

An HTTP server (like Apache) with following enabled: sessions, mod_rewrite (necessary but preferred).


PHP 4.3.2 or greater(CakePHP works great in either PHP 4 or 5)


A database engine (supported only for MySQL 4+, PostgreSQL and a wrapper for ADODB).

INSTALLING CAKEPHP:

Download the cakePHP directly from the

http://cakephp.org/ or from sourceforge http://sourceforge.net/projects/
cakephp/ or from github http://sourceforge.net/projects/cakephp/

Stable or lightly build is recommendable.

UNPACKING:

Place the compressed package in your web server particularly at the webroot


There are two ways,


Use a development setup, view many CakePHP applications under a single domain,


Using the production setup, which allows single CakePHP application on the domain.

SETUP FOR CAKEPHP:

CakePHP is generally recommended for development environment. It is because it is less secure. Second way is more secure for production environment.


Important:/app/tmp must be writable format by the user that your web server run.

DEVELOPMENT SETUP:

Development setup makes the whole installation in the specified directory as below

/wwwroot
/cake
    /app
    /cake
    /vendors
    .htaccess
    index.php

wwwroot folder acts as the web root (it will look like this)

<a href="http://www.example.com/cake/controllerName/actionName/
param1/param2" title="Linkification: http://www.example.com/cake/
controllerName/actionName/param1/param2">www.example.com/cake/
controllerName/actionName/param1/param2</a>

PRODUCTION SETUP:

Production setup needs to have the rights to change the DocumentRoot on your server, you can see that below,

../path_to_cake_install
    /app
        /config
        /controllers
        /models
        /plugins
        /tmp
        /vendors
        /views
        /webroot <-- This should be your new DocumentRoot
        .htaccess
        index.php
    /cake
    /vendors
    .htaccess
    index.php

PRODUCTION httpd.conf:

DocumentRoot /path_to_cake/app/webroot

In this setup the webroot directory as the web root so url may be like this

http://www.example.com/controllerName/actionName/param1/param2

ADVANCED SETUP:

  • Core CakePHP libraries - /cake
  • Application code- /app
  • Application webroot files-/app/webroot

/app/webroot/index.php:

if (!defined('ROOT'))
{
    define('ROOT', dirname(dirname(dirname(__FILE__))));
}

if (!defined('APP_DIR'))
{
    define ('APP_DIR', basename(dirname(dirname(__FILE__))));
}

if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
    define('CAKE_CORE_INCLUDE_PATH', ROOT);
}

Example:

  • /usr/lib/cake My Cake libraries
  • /var/www/mysite/ My Cake webroot directory
  • /home/me/mysite My application files
Here's what the file setup looks like:

/home
    /me
        /mysite                  <-- Used to be /cake_install/app
            /config
            /controllers
            /models
            /plugins
            /tmp
            /vendors
            /views
            index.php
/var
    /www
        /mysite                  <-- Used to be /cake_install/app/webroot
            /css
            /files
            /img
            /js
            .htaccess
            css.php
            favicon.ico
            index.php
/usr
    /lib
        /cake                    <-- Used to be /cake_install/cake
            /cake
                /config
                /docs
                /libs
                /scripts
                app_controller.php
                app_model.php
                basics.php
                bootstrap.php
                dispatcher.php
            /vendors

Use ‘DS’ constant instead slashed

<?php
if (!defined('ROOT'))
{
    define('ROOT', DS.'home'.DS.'me');
}

if (!defined('APP_DIR'))
{
    define ('APP_DIR', 'mysite');
}

if (!defined('CAKE_CORE_INCLUDE_PATH'))
{
    define('CAKE_CORE_INCLUDE_PATH', DS.'usr'.DS.'lib'.DS.'cake');
}

app/config/database.php

<?php
var $default = array('driver'   => 'mysql',
                     'connect'  => 'mysql_connect',
                     'host'     => 'localhost',
                     'login'    => 'user',
                     'password' => 'password',
                     'database' => 'project_name',
                     'prefix'   => '');

cake supports

  • mysql
  • postgres
  • sqlite
  • pear-drivername (so you might enter pear-mysql, for example)
  • adodb-drivername
<?php
var $useDbConfig = 'test';

ROUTE CONFIGURATION:

app/config/routes.php

Route Pattern

<?php
$Route->connect (
    'URL',
    array('controller'=>'controllername',
    'action'=>'actionname', 'firstparam')
);
?>

Route Example

<?php
$Route->connect ('/blog/:action/*', array('controller'=>'Blog', 'action'=>'index'));

Route Handling in a Controller

<?php
class BlogController extends AppController
{
 function history ($year, $month=null)
 {
   // .. Display appropriate content
 }
}

Setting the Default Route

<?php
$Route->connect ('/', array('controller'=>'Pages', 
'action'=>'display', 'home'));

Admin Routing and Webservices:

some settings in /app/config/core.php you can take advantage...


Enable this CAKE_ADMIN line in your /app/config/core.php file

/admin/products/add          CAKE_ADMIN = 'admin'
                             name of action in ProductsController = 'admin_add()'

/superuser/news/post         CAKE_ADMIN = 'superuser'
                             name of action in NewsController = 'superuser_post()'

/admin/posts/delete          CAKE_ADMIN = 'admin'
                             name of action in PostsController = 'admin_delete()'
Using admin routes
<?php
$this->params[CAKE_ADMIN];
<?php
$this->params['admin'];

Please note this

  • rss
  • xml
  • rest
  • soap
  • xmlrpc
/app/config/core.php (partial)

<?php
/**
 *  The define below is used to turn cake built webservices
 *  on or off. Default setting is off.
 */
    define('WEBSERVICES', 'on');

Next, we need component for the type of web service


once done structure is completed


messages_controller.php

<?php
class PhonesController extends AppController
{
    function doWhosOnline()
    {
        // this action is where we do all the work of seeing 
who's on the phone...

        // If I wanted this action to be available via Cake's 
xml webservices route,
        // I'd need to include a view at /app/views/phones/xml/
do_whos_online.thtml.
        // Note: the default view used here is at /app/views/layouts/
xml/default.thtml.

        // If a user requests /phones/doWhosOnline, they will get an 
HTML version.
        // If a user requests /xml/phones/doWhosOnline, they will 
get the XML version.
    }
}

CONCLUSION:

Here we have just completed the basic configuration. Hope you have clearly understood and refreshed your knowledge with the info provided. This is just the first part, the advanced configuration will be carried out in the next part.

Article Tools

Did this article help you?

YesNo

Become
an Author!

Write an Article