William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

php MVC

According to Wikipedia, Model – View – Controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other. In MVC, the model represents the information (the data) of the application; the view corresponds to elements of the user interface such as text, checkbox items, and so forth; and the controller manages the communication of data and the business rules used to manipulate the data to and from the model. In simpler words:

  1. Model handles all our database logic. Using the model we connect to our database and provide an abstraction layer.
  2. Controller represents all our business logic i.e. all $_REQUEST.
  3. View represents our presentation logic i.e HTML/Javascript code.

Lets dive right in

The Directory Structure

Directory Structure

Although we will not be a couple of directories mentioned above for this tutorial, we should have them in place for future expansion. Let me explain the purpose of each directory:

  • application – application specific code
  • config – database/server configuration
  • db – database backups
  • library – framework code
  • public – application specific js/css/images
  • scripts – command-line utilities
  • tmp – temporary data

Once we have our directory structure ready, let us understand a few coding conventions.

Coding Conventions

  • MySQL tables will always be lowercase and plural e.g. items, cars
  • Models will always be singular and first letter capital e.g. Item, Car
  • Controllers will always have “Controller” appended to them. e.g. ItemsController, CarsController
  • Views will have plural name followed by action name as the file. e.g. items/view.php, cars/buy.php

We first add .htaccess file in the root directory which will redirect all calls to the public folder

1.<IfModule mod_rewrite.c>
2.    RewriteEngine on
3.    RewriteRule    ^$    public/    [L]
4.    RewriteRule    (.*) public/$1    [L]
5. </IfModule>

We then add .htaccess file to our public folder which redirect all calls to index.php. Line 3 and 4 make sure that the path requested is not a filename or directory. Line 7 redirects all such paths to index.php?url=PATHNAME

  • 01.<IfModule mod_rewrite.c>
  • 02.RewriteEngine On
  • 03. 
  • 04.RewriteCond %{REQUEST_FILENAME} !-f
  • 05.RewriteCond %{REQUEST_FILENAME} !-d
  • 06. 
  • 07.RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
  • 08. 
  • 09.</IfModule>

This redirection has many advantages-
a) we can use it for bootstrapping i.e. all calls go via our index.php except for images/js/cs.
b) we can use pretty/seo-friendly URLS
c) we have a single entry point

Now we add index.php to our public folder

1.<?php   
2. 
3.define(‘DS’, DIRECTORY_SEPARATOR);
4.define(‘ROOT’, dirname(dirname(__FILE__)));
5. 
6.$url = $_GET['url'];
7. 
8.require_once (ROOT . DS . ‘library’ . DS . ‘bootstrap.php’);

Notice that I have purposely not included the closing ?>. This is to avoid injection of any extra whitespaces in our output. For more, I suggest you view Zend’s coding style.

Our index.php basically set the $url variable and calls bootstrap.php which resides in our library directory.

Now lets view our bootstrap.php

1.<?php
2. 
3.require_once (ROOT . DS . ‘config’ . DS . ‘config.php’);
4.require_once (ROOT . DS . ‘library’ . DS . ‘shared.php’);

Yes these requires could be included directly in index.php. But have not been on purpose to allow future expansion of code.
Now let us have a look at shared.php, finally something that does some real work

01.<?php
02. 
03./** Check if environment is development and display errors **/
04. 
05.function setReporting() {
06.if (DEVELOPMENT_ENVIRONMENT == true) {
07.    error_reporting(E_ALL);
08.    ini_set(‘display_errors’,’On’);
09.} else {
10.    error_reporting(E_ALL);
11.    ini_set(‘display_errors’,’Off’);
12.    ini_set(‘log_errors’, ‘On’);
13.    ini_set(‘error_log’, ROOT.DS.’tmp’.DS.’logs’.DS.’error.log’);
14.}
15.}
16. 
17./** Check for Magic Quotes and remove them **/
18. 
19.function stripSlashesDeep($value) {
20.    $value = is_array($value) ? array_map(‘stripSlashesDeep’, $value) : stripslashes($value);
21.    return $value;
22.}
23. 
24.function removeMagicQuotes() {
25.if ( get_magic_quotes_gpc() ) {
26.    $_GET    = stripSlashesDeep($_GET   );
27.    $_POST   = stripSlashesDeep($_POST  );
28.    $_COOKIE = stripSlashesDeep($_COOKIE);
29.}
30.}
31. 
32./** Check register globals and remove them **/
33. 
34.function unregisterGlobals() {
35.    if (ini_get(‘register_globals’)) {
36.  $array = array(‘_SESSION’, ‘_POST’, ‘_GET’, ‘_COOKIE’, ‘_REQUEST’, ‘_SERVER’, ‘_ENV’, ‘_FILES’);
37.  foreach ($array as $value) {
38.      foreach ($GLOBALS[$value] as $key => $var) {
39.    if ($var === $GLOBALS[$key]) {
40.  unset($GLOBALS[$key]);
41.    }
42.      }
43.  }
44.    }
45.}
46. 
47./** Main Call Function **/
48. 
49.function callHook() {
50.    global $url;
51. 
52.    $urlArray = array();
53.    $urlArray = explode(“/”,$url);
54. 
55.    $controller = $urlArray[0];
56.    array_shift($urlArray);
57.    $action = $urlArray[0];
58.    array_shift($urlArray);
59.    $queryString = $urlArray;
60. 
61.    $controllerName = $controller;
62.    $controller = ucwords($controller);
63.    $model = rtrim($controller, ‘s’);
64.    $controller .= ‘Controller’;
65.    $dispatch = new $controller($model,$controllerName,$action);
66. 
67.    if ((int)method_exists($controller, $action)) {
68.  call_user_func_array(array($dispatch,$action),$queryString);
69.    } else {
70.  /* Error Generation Code Here */
71.    }
72.}
73. 
74./** Autoload any classes that are required **/
75. 
76.function __autoload($className) {
77.    if (file_exists(ROOT . DS . ‘library’ . DS . strtolower($className) . ‘.class.php’)) {
78.  require_once(ROOT . DS . ‘library’ . DS . strtolower($className) . ‘.class.php’);
79.    } else if (file_exists(ROOT . DS . ‘application’ . DS . ‘controllers’ . DS . strtolower($className) . ‘.php’)) {
80.  require_once(ROOT . DS . ‘application’ . DS . ‘controllers’ . DS . strtolower($className) . ‘.php’);
81.    } else if (file_exists(ROOT . DS . ‘application’ . DS . ‘models’ . DS . strtolower($className) . ‘.php’)) {
82.  require_once(ROOT . DS . ‘application’ . DS . ‘models’ . DS . strtolower($className) . ‘.php’);
83.    } else {
84.  /* Error Generation Code Here */
85.    }
86.}
87. 
88.setReporting();
89.removeMagicQuotes();
90.unregisterGlobals();
91.callHook();

Let me explain the above code briefly. The setReporting() function helps us display errors only when the DEVELOPMENT_ENVIRONMENT is true. The next move is to remove global variables and magic quotes. Another function that we make use of is __autoload which helps us load our classes automagically. Finally, we execute the callHook() function which does the main processing.

First let me explain how each of our URLs will look – yoursite.com/controllerName/actionName/queryString

So callHook() basically takes the URL which we have received from index.php and separates it out as $controller, $action and the remaining as $queryString. $model is the singular version of $controller.

e.g. if our URL is http://www.todo.com/items/view/1/first-item, then
Controller is items
Model is item (corresponding mysql table)
View is delete
Action is delete
Query String is an array (1,first-item)

After the separation is done, it creates a new object of the class $controller.”Controller” and calls the method $action of the class.
Now let us create a few classes first namely our base Controller class which will be used as the base class for all our controllers, our Model class which will be used as base class for all our models.
First the controller.class.php:

01.<?php
02.class Controller {
03. 
04.    protected $_model;
05.    protected $_controller;
06.    protected $_action;
07.    protected $_template;
08. 
09.    function __construct($model, $controller, $action) {
10. 
11.  $this->_controller = $controller;
12.  $this->_action = $action;
13.  $this->_model = $model;
14. 
15.  $this->$model =&amp; new $model;
16.  $this->_template =&amp; new Template($controller,$action);
17. 
18.    }
19. 
20.    function set($name,$value) {
21.  $this->_template->set($name,$value);
22.    }
23. 
24.    function __destruct() {
25.      $this->_template->render();
26.    }
27. 
28.}

The above class is used for all communication between the controller, the model and the view (template class). It creates an object for the model class and an object for template class. The object for model class has the same name as the model itself, so that we can call it something like $this->Item->selectAll(); from our controller.

While destroying the class we call the render() function which displays the view (template) file.
Now let us look at our model.class.php

01.<?php
02.class Model extends SQLQuery {
03.    protected $_model;
04. 
05.    function __construct() {
06. 
07.  $this->connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
08.  $this->_model = get_class($this);
09.  $this->_table = strtolower($this->_model).”s”;
10.    }
11. 
12.    function __destruct() {
13.    }
14.}

The Model class extends the SQLQuery class which basically is an abstraction layer for the mySQL connectivity. Depending on your requirements you can specify any other DB connection class that you may require.

Now let us have a look at the SQLQuery.class.php

01.<?php
02. 
03.class SQLQuery {
04.    protected $_dbHandle;
05.    protected $_result;
06. 
07.    /** Connects to database **/
08. 
09.    function connect($address, $account, $pwd, $name) {
10.  $this->_dbHandle = @mysql_connect($address, $account, $pwd);
11.  if ($this->_dbHandle != 0) {
12.      if (mysql_select_db($name, $this->_dbHandle)) {
13.    return 1;
14.      }
15.      else {
16.    return 0;
17.      }
18.  }
19.  else {
20.      return 0;
21.  }
22.    }
23. 
24.    /** Disconnects from database **/
25. 
26.    function disconnect() {
27.  if (@mysql_close($this->_dbHandle) != 0) {
28.      return 1;
29.  }  else {
30.      return 0;
31.  }
32.    }
33. 
34.    function selectAll() {
35.  $query = ‘select * from `’.$this->_table.’`’;
36.  return $this->query($query);
37.    }
38. 
39.    function select($id) {
40.  $query = ‘select * from `’.$this->_table.’` where `id` = \”.mysql_real_escape_string($id).’\”;
41.  return $this->query($query, 1);
42.    }
43. 
44.    /** Custom SQL Query **/
45. 
46.    function query($query, $singleResult = 0) {
47. 
48.  $this->_result = mysql_query($query, $this->_dbHandle);
49. 
50.  if (preg_match(“/select/i”,$query)) {
51.  $result = array();
52.  $table = array();
53.  $field = array();
54.  $tempResults = array();
55.  $numOfFields = mysql_num_fields($this->_result);
56.  for ($i = 0; $i < $numOfFields; ++$i) {
57.      array_push($table,mysql_field_table($this->_result, $i));
58.      array_push($field,mysql_field_name($this->_result, $i));
59.  }
60. 
61.      while ($row = mysql_fetch_row($this->_result)) {
62.    for ($i = 0;$i < $numOfFields; ++$i) {
63.  $table[$i] = trim(ucfirst($table[$i]),”s”);
64.  $tempResults[$table[$i]][$field[$i]] = $row[$i];
65.    }
66.    if ($singleResult == 1) {
67.  mysql_free_result($this->_result);
68.  return $tempResults;
69.    }
70.    array_push($result,$tempResults);
71.      }
72.      mysql_free_result($this->_result);
73.      return($result);
74.  }
75. 
76.    }
77. 
78.    /** Get number of rows **/
79.    function getNumRows() {
80.  return mysql_num_rows($this->_result);
81.    }
82. 
83.    /** Free resources allocated by a query **/
84. 
85.    function freeResult() {
86.  mysql_free_result($this->_result);
87.    }
88. 
89.    /** Get error string **/
90. 
91.    function getError() {
92.  return mysql_error($this->_dbHandle);
93.    }
94.}

The SQLQuery.class.php is the heart of our framework. Why? Simply because it can really help us reduce our work while programming by creating an SQL abstraction layer. We will dive into an advanced version of SQLQuery.class.php in the next tutorial. For now lets just keep it simple.

The connect() and disconnect() functions are fairly standard so I will not get into too much detail. Let me specifically talk about the query class. Line 48 first executes the query. Let me consider an example. Suppose our SQL Query is something like:

SELECT table1.field1 , table1.field2, table2.field3, table2.field4 FROM table1,table2 WHERE ….

Now what our script does is first find out all the output fields and their corresponding tables and place them in arrays – $field and $table at the same index value. For our above example, $table and $field will look like

$field = array(field1,field2,field3,field4);
$table = array(table1,table1,table2,table2);

The script then fetches all the rows, and converts the table to a Model name (i.e. removes the plural and capitalizes the first letter) and places it in our multi-dimensional array and returns the result. The result is of the form $var['modelName']['fieldName']. This style of output makes it easy for us to include db elements in our views.

Now let us have a look at template.class.php

01.<?php
02.class Template {
03. 
04.    protected $variables = array();
05.    protected $_controller;
06.    protected $_action;
07. 
08.    function __construct($controller,$action) {
09.  $this->_controller = $controller;
10.  $this->_action = $action;
11.    }
12. 
13.    /** Set Variables **/
14. 
15.    function set($name,$value) {
16.  $this->variables[$name] = $value;
17.    }
18. 
19.    /** Display Template **/
20. 
21.    function render() {
22.  extract($this->variables);
23. 
24.      if (file_exists(ROOT . DS . ‘application’ . DS . ‘views’ . DS . $this->_controller . DS . ‘header.php’)) {
25.    include (ROOT . DS . ‘application’ . DS . ‘views’ . DS . $this->_controller . DS . ‘header.php’);
26.      } else {
27.    include (ROOT . DS . ‘application’ . DS . ‘views’ . DS . ‘header.php’);
28.      }
29. 
30.  include (ROOT . DS . ‘application’ . DS . ‘views’ . DS . $this->_controller . DS . $this->_action . ‘.php’);      
31. 
32.      if (file_exists(ROOT . DS . ‘application’ . DS . ‘views’ . DS . $this->_controller . DS . ‘footer.php’)) {
33.    include (ROOT . DS . ‘application’ . DS . ‘views’ . DS . $this->_controller . DS . ‘footer.php’);
34.      } else {
35.    include (ROOT . DS . ‘application’ . DS . ‘views’ . DS . ‘footer.php’);
36.      }
37.    }
38. 
39.}

The above code is pretty straight forward. Just one point- if it does not find header and footer in the view/controllerName folder then it goes for the global header and footer in the view folder.

Now all we have to add is a config.php in the config folder and we can begin creating our first model, view and controller!

01.<?php
02. 
03./** Configuration Variables **/
04. 
05.define (‘DEVELOPMENT_ENVIRONMENT’,true);
06. 
07.define(‘DB_NAME’, ‘yourdatabasename’);
08.define(‘DB_USER’, ‘yourusername’);
09.define(‘DB_PASSWORD’, ‘yourpassword’);
10.define(‘DB_HOST’, ‘localhost’);

Phew! Now let us create our first mini-todo application. We first create a database “todo” and execute the following SQL queries

1.CREATE TABLE `items` (
2.  `id` int(11) NOT NULL auto_increment,
3.  `item_name` varchar(255) NOT NULL,
4.  PRIMARY KEY  (`id`)
5.);
6. 
7.INSERT INTO `items` VALUES(1, ‘Get Milk’);
8.INSERT INTO `items` VALUES(2, ‘Buy Application’);

Once that is done, we add item.php to our model folder with the following contents

1.<?php
2. 
3.class Item extends Model {
4. 
5.}

Write it is empty, but will have more information when we expand our framework in Part 2.

Now create a file called itemscontroller.php in the controller folder

01.<?php
02. 
03.class ItemsController extends Controller {
04. 
05.    function view($id = null,$name = null) {
06. 
07.  $this->set(‘title’,$name.’ – My Todo List App’);
08.  $this->set(‘todo’,$this->Item->select($id));
09. 
10.    }
11. 
12.    function viewall() {
13. 
14.  $this->set(‘title’,’All Items – My Todo List App’);
15.  $this->set(‘todo’,$this->Item->selectAll());
16.    }
17. 
18.    function add() {
19.  $todo = $_POST['todo'];
20.  $this->set(‘title’,’Success – My Todo List App’);
21.  $this->set(‘todo’,$this->Item->query(‘insert into items (item_name) values (\”.mysql_real_escape_string($todo).’\’)’));
22.    }
23. 
24.    function delete($id = null) {
25.  $this->set(‘title’,’Success – My Todo List App’);
26.  $this->set(‘todo’,$this->Item->query(‘delete from items where id = \”.mysql_real_escape_string($id).’\”));
27.    }
28. 
29.}

Finally create a folder called items in the views folder and create the following files in it-

view.php

1.<h2><?php echo $todo['Item']['item_name']?></h2>
2. 
3.    <a class=”big” href=”../../../items/delete/<?php echo $todo['Item']['id']?>”>
4.    <span class=”item”>
5.    Delete this item
6.    </span>
7.    </a>

viewall.php

01.<form action=”../items/add” method=”post”>
02.<input type=”text” value=”I have to…” onclick=”this.value=”” name=”todo”> <input type=”submit” value=”add”>
03.</form>
04.<br/><br/>
05.<?php $number = 0?>
06. 
07.<?php foreach ($todo as $todoitem):?>
08.    <a class=”big” href=”../items/view/<?php echo $todoitem['Item']['id']?>/<?php echo strtolower(str_replace(” “,”-”,$todoitem['Item']['item_name']))?>”>
09.    <span class=”item”>
10.    <?php echo ++$number?>
11.    <?php echo $todoitem['Item']['item_name']?>
12.    </span>
13.    </a><br/>
14.<?php endforeach?>

delete.php

1.<a class=”big” href=”../../items/viewall”>Todo successfully deleted. Click here to go back.</a>

add.php

1.<a class=”big” href=”../items/viewall”>Todo successfully added. Click here to go back.</a>

header.php

01.<html>
02.<head>
03.<title><?php echo $title?></title>
04.<style>
05..item {
06.width:400px;
07. 
08.}
09. 
10.input {
11.    color:#222222;
12.font-family:georgia,times;
13.font-size:24px;
14.font-weight:normal;
15.line-height:1.2em;
16.    color:black;
17.}
18. 
19. a {
20.    color:#222222;
21.font-family:georgia,times;
22.font-size:24px;
23.font-weight:normal;
24.line-height:1.2em;
25.    color:black;
26.    text-decoration:none;
27. 
28.}
29. 
30.a:hover {
31.    background-color:#BCFC3D;
32.}
33.h1 {
34.color:#000000;
35.font-size:41px;
36.letter-spacing:-2px;
37.line-height:1em;
38.font-family:helvetica,arial,sans-serif;
39.border-bottom:1px dotted #cccccc;
40.}
41. 
42.h2 {
43.color:#000000;
44.font-size:34px;
45.letter-spacing:-2px;
46.line-height:1em;
47.font-family:helvetica,arial,sans-serif;
48. 
49.}
50.</style>
51.</head>
52.<body>
53.<h1>My Todo-List App</h1>

footer.php

1.</body>
2.</html>

Now assuming you have uploaded the directory structure to the todo folder, point your browser to:

http://localhost/todo/items/viewall

Todo List App

Where do we go from here?

Firstly we have achieved a lot by completing this tutorial – we have been able to separate our presentation logic from our business logic and database logic. We have also been able to provide for pretty-urls and extensibility..

The above is part 1, the source can be download here. The following is the new version which we have implemented a simple e-commerce website consisting of categories, subcategories, products and tags (for products). The example will help you understand the various relationships between the tables and most of the new functionality provided by this part of the framework.

What’s New

config/inflection.php
The inflection configuration file enables us to use irregular words i.e. words which do not have a standard plural name. This file is used in conjunction with library/inflection.class.php

config/routing.php
The routing configuration file enables us to specify default controller and action. We can also specify custom redirects using regular expressions. Currently I have specified only one redirect i.e. http://localhost/framework/admin/categories/view will become http://localhost/framework/admin/categories_view where admin is the controller and categories_view is the action. This will enable us to create an administration centre with pretty URLs. You can specify others as per your requirements.

library/cache.class.php
The cache class is in its infancy. Currently there are two simple functions- set and get. The data is stored in a flat text-file in the cache directory. Currently only the describe function of the SQLQuery class uses this cache function.

library/html.class.php
The HTML class is used to aid the template class. It allows you to use a few standard functions for creating links, adding javascript and css. I have also added a function to convert links to tinyurls. This class can be used only in the views e.g. $html->includeJs(’generic.js’);

library/inflection.class.php
In the previous part, plural of words were created by adding only “s” to the word. However, for a more full-fledged version, we now use the inflection class with slight modifications. If you have a look at the class, it makes use of simple regular expressions. It would be nice to add a cache function to this class in future.

library/template.class.php
I have added a new variable called doNotRenderHeader which will enable you to not output headers for a particular action. This can be used in AJAX calls when you do not want to return the headers. It has to be called by the controller e.g. $this->doNotRenderHeader = 1;

library/vanillacontroller.class.php & vanillamodel.class.php
Pretty much unchanged, but I have added a prefix vanilla to them to emphasize on its simplicity library/sqlquery.class.php
The SQLQuery class is the heart of this framework. This class will enable you to use your tables as objects.

Let us understand the easy functions first – save() and delete()

The save() function must be used from the controller. The save() function can have two options- if an id is set, then it will update the entry; if it is not set, then it will create a new entry. For example let us consider the categories class i.e. application/controllers/categoriescontroller.php. We can have a function like the one below.

1.function new() {
2.   $this->Category->id = $_POST['id'];
3.   $this->Category->name = $_POST['name'];
4.   $this->Category->save();
5.}

If $this->Category->id = null; then it will create a new record in the categories table.

The delete() function enables you to delete a record from the table. A sample code could be as below. Although you should use POST instead of GET queries for deleting records. As a rule idempotent operations should use GET. Idempotent operations are those which do not change the state of the database (i.e. do not update/delete/insert rows or modify the database in anyway)

1.function delete($categoryId) {
2.   $this->Category->id = $categoryId;
3.   $this->Category->delete();
4.}

Now let us look at the search() function. I know it is a bit intimidating at first. But it is pretty straight forward after we break it down. During database design, we use the following convention:

1:1 Relationship
For a one is to one relationship, suppose we have two tables students and mentors and each student hasOne mentor, then in the students table we will have a field called ‘mentor_id’ which will store the id of the mentor from the mentors table.

1:Many Relationship
For a one is to many relationship, suppose we have two tables parents and children and each parent hasMany children, then in the children table we will have a field called ‘parent_id’ which will store the id of the parent from the parents table.

Many:Many Relationship
For a many is to many relationship, suppose we have two tables students and teachers and each student hasManyAndBelongsToMany teachers, then we create a new table called students_teachers with three fields: id, student_id and teacher_id. The naming convention for this table is alphabetical. i.e. if our tables are cars and animals, then the table should be named animals_cars and not cars_animals.

Now once we have created our database as per these conventions, we must tell our framework about their existence. Let us have a look at models/product.php.

1.class Product extends VanillaModel {
2.    var $hasOne = array(‘Category’ => ‘Category’);
3.    var $hasManyAndBelongsToMany = array(‘Tag’ => ‘Tag’);
4.}

The first Category is the alias and the second Category is the actual model. In most cases both will be the same. Let us consider the models/category.php where they are not.

1.<?php
2. 
3.class Category extends VanillaModel {
4.  var $hasMany = array(‘Product’ => ‘Product’);
5.  var $hasOne = array(‘Parent’ => ‘Category’);
6. 
7.}

Here each category has a parent category, thus our alias is Parent while model is Category. Thus we will have a field called parent_id in the categories table. To clearly understand these relationships, I suggest you create a couple of tables and test them out for yourself. In order to see the output, use code similar to the following in your controller.

1.function view() {
2.   $this->Category->id = 1;
3.   $this->Category->showHasOne();
4.   $this->Category->showHasMany();
5.   $this->Category->showHMABTM();
6.   $data = $this->Category->search();
7.   print_r($data);
8.}

Now let us try and understand the search() function. If there are no relationships, then the function simply does a select * from tableName (tableName is same as controllerName). We can influence this statement, by using the following commands:

where(’fieldName’,’value’) => Appends WHERE ‘fieldName’ = ‘value’
like(’fieldName’,’value’) => Appends WHERE ‘fieldName’ LIKE ‘%value%’
setPage(’pageNumber’) => Enables pagination and display only results for the set page number
setLimit(’fieldName’,’value’) => Allows you to modify the number of results per page if pageNumber is set. Its default value is the one set in config.php.
orderBy(’fieldName’,’Order’) => Appends ORDER BY ‘fieldName’ ASC/DESC
id = X => Will display only a single result of the row matching the id

Now let us consider when showHasOne() function has be called, then for each hasOne relationship, a LEFT JOIN is done (see line 91-99).

Now if showHasMany() function has been called, then for each result returned by the above query and for each hasMany relationship, it will find all those records in the second table which match the current result’s id (see line 150). Then it will push all those results in the same array. For example, if teachers hasMany students, then $this->Teacher->showHasMany() will search for teacher_id in the students table.

Finally if showHMABTM() function has been called, then for each result returned by the first query and for each hasManyAndBelongsToMany relationship, it will find all those records which match the current result’s id (see line 200-201). For example, if teachers hasManyAndBelongsToMany students, then $this->Teacher->showHMABTM() will search for teacher_id in students_teachers table.

On line 236, if id is set, then it will return a single result (and not an array), else it will return an array. The function then calls the clear() function which resets all the variables (line 368-380).

Now let us consider an example to enable pagination on products. The code in the controllers/productscontroller.php should look something similar to the following.

01.function page ($pageNumber = 1) {
02.  $this->Product->setPage($pageNumber);
03.  $this->Product->setLimit(’10′);
04.  $products = $this->Product->search();
05.  $totalPages = $this->Product->totalPages();
06.  $this->set(‘totalPages’,$totalPages);
07.  $this->set(‘products’,$products);
08.  $this->set(‘currentPageNumber’,$pageNumber);
09.}

Now our corresponding view i.e. views/products/page.php will be something like below.

01.<?php foreach ($products as $product):?>
02.<div>
03.<?php echo $product['Product']['name']?>
04.</div>
05.<?php endforeach?>
06. 
07.<?php for ($i = 1; $i <= $totalPages; $i++):?>
08.<div>
09.<?php if ($i == $currentPageNumber):?>
10.<?php echo $currentPageNumber?>
11.<?php else: ?>
12.<?php echo $html->link($i,’products/page/’.$i)?>
13.<?php endif?>
14.</div>
15.<?php endfor?>

Thus with a few lines of code we have enabled pagination on our products page with pretty URLs!
(/products/page/1, products/page/2 …)

If you look at the totalPages() function on line 384-397, you will see that it takes the existing query and strips of the LIMIT condition and returns the count. This count/limit gives us the totalPages.

Now suppose that we are in the categories controller and we want to implement a query on the products table. One way to implement this is using a custom query i.e. $this->Category->custom(‘select * from products where …’);

Alternatively, we can use the performAction function (line 49-57 in shared.php) to call an action of another controller. An example call in categoriescontroller.php would be something like below.

1.function view($categoryId = null, $categoryName = null) {
2.  $categories = performAction(‘products’,’findProducts’,array($categoryId,$categoryName));
3.}

And the corresponding code in productscontroller.php should be as below.

1.function findProducts ($categoryId = null, $categoryName = null) {
2.  $this->Product->where(‘category_id’,$categoryId);
3.  $this->Product->orderBy(‘name’);
4.  return $this->Product->search();
5.}

The above more or less sums up all the functionality of the SQLQuery class. You can easily extend this as per your requirements e.g. to cache results, add conditions to hasMany, hasOne, hasMABTM queries also etc.

I have also laid the foundation for implementing user registration functionality by specifying a beforeAction and afterAction function which will be executed for each controller action. This will enable us to call a function which checks whether the user cookie is set and is a valid user.

One more feature that I have implemented is to have an administration centre. For this purpose we create a dummy model called models/admin.php and set a variable called $abstract = true. This will tell our VanillaModel to not look for a corresponding table in the database. As stated before I have created a routing for admin/X/Y as admin/X_Y. Thus we can have URLs like admin/categories/delete/15 which will actually call the categories_delete(15) function in the controllers/admincontroller.php file.

Where do we go from here?

I hope this tutorial is not too complicated. I have tried to break it down as much as possible. A couple of functions that we need to implement include -> redirectAction which will be similar to performAction, but instead redirect. performAction can only return results. redirectAction will perform only the redirected action and display that corresponding view only. This will be helpful during user authentication. If the user is not logged in, then we call a function like -> redirectAction(’user’,’login’,array($returnUrl));

Various classes are in their infancy like the cache class, HTML class etc. These classes can be expanded to add more functionality.

More scripts can be added like dbimport, clearcache etc. can be added. Currently there is only one script dbexport.php which dumps the entire database. Classes for managing sessions, cookies etc. also need to be developed.

12 responses to “php MVC

  1. extipthem 01/08/2011 at 1:09 pm

    thanks an eye to this tips
    pmu
    turf

  2. IceseeBut 02/07/2011 at 5:01 am

    Thanks for an idea, you sparked at thought from a angle I hadn’t given thoguht to yet. Now lets see if I can do something with it.

    ____________________________________
    non prescription diet pills non prescription diet pills online antibiotics no prescription
    pet meds no prescription needed pharmacy usa pharmacies online
    prescription medications online prescription meds online ultram with no prescription
    antibiotics no prescription canaural ear drops for dogs no prescription buying medication online
    direct pharmacy ionamin no prescription generic pharmacies
    no prescription cheap phentermine licensed online pharmacy no prescription needed pharmacy

  3. Augmepappeday 02/09/2011 at 5:29 am

    It had been some time since I visited website with such high quality information. Thansk rather a lot for the useful data

    ____________________________________
    amoxicillin price
    there

  4. FLEKGIPPILK 02/16/2011 at 7:37 pm

    Can I put up your submit to my wordpress blog? I will add a back-link to your forum. That’s one actually great post.

    ____________________________________
    my profile

  5. ziculasEO 02/23/2011 at 1:33 am

    thanks for this tips

  6. score 08/04/2011 at 1:45 pm

    Hi there, I found your blog via Google while searching for a related topic, your site came up, it looks good.

  7. RewsZerrels 10/20/2011 at 5:20 pm

    keep posting like this it’s really very good idea, you are awesome.
    burberry handabags outlet

  8. powercashadvance.com 10/28/2011 at 10:23 am

    Almost all I can comment on is, I don’t know what to really say! Except obviously, for the amazing tips which are shared on this blog. I could think of a trillion fun ways to read the content articles on this site. I do think I will ultimately take action employing your tips on those issues I could never have been able to touch alone. You were so clever to allow me to be one of those to profit from your handy information. Please know how great I enjoy the whole thing.

  9. http://www.blockedear.net/ 07/13/2012 at 11:38 pm

    Hey very cool website!! Guy .. Beautiful .. Superb .. I will bookmark your website and take the feeds additionally?I am glad to seek out so many helpful info here in the post, we need develop extra strategies on this regard, thank you for sharing. . . . . .

  10. copy cat 11/26/2012 at 7:22 am

    good job on the copy and paste! :)

  11. Health tips 01/27/2013 at 2:57 am

    Thanks all in your exciting along with topical feedback
    thus far. Considerably food items with regard to thought.
    Many attention seeking suggestions plus viewpoints all-around.
    I am unable to declare that Certainly with everything you
    say however, if i didnt admit the actual legitamecy of your
    respective write-up I might end up being not aware for your knowledge.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 75 other followers

%d bloggers like this: