0% found this document useful (0 votes)
80 views4 pages

Rewriteengine On 3. Rewriterule $ Public/ (L) 4. Rewriterule (. ) Public/$1 (L) 5.

The document outlines the directory structure and coding conventions for an MVC framework, including separating models, views, and controllers into different directories, and describes the bootstrap process which loads configuration files, routes all requests through index.php, and calls the appropriate controller and action. It also shows how errors are handled, magic quotes and register globals are removed, and classes are autoloaded to connect the MVC components and handle requests.

Uploaded by

Phong Hoangtu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views4 pages

Rewriteengine On 3. Rewriterule $ Public/ (L) 4. Rewriterule (. ) Public/$1 (L) 5.

The document outlines the directory structure and coding conventions for an MVC framework, including separating models, views, and controllers into different directories, and describes the bootstrap process which loads configuration files, routes all requests through index.php, and calls the appropriate controller and action. It also shows how errors are handled, magic quotes and register globals are removed, and classes are autoloaded to connect the MVC components and handle requests.

Uploaded by

Phong Hoangtu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

MVC - Model handles add our database logic connect database - Controller represents all our business logic

if else - View represents our presentation HTML XML JSON code Cu trc th mc

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 Coding Conventions 1. mySQL tables will always be lowercase and plural e.g. items, cars 2. Models will always be singular and first letter capital e.g. Item, Car 3. Controllers will always have Controller appended to them. e.g. ItemsController, CarsController 4. Views will have plural name followed by action name as the file. e.g. items/view.php, cars/buy.php Step one - Add .htaccess into root directory - To redirect all call to public folder
1.<IfModule mod_rewrite.c> 2. RewriteEngine on 3. RewriteRule ^$ public/ 4. RewriteRule (.*) public/$1 5. </IfModule>

[L] [L]

Step two - Then add .htaccess into public directory to redirect call to index.php
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>

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 - This redirection has many advantagesa) 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 Step Three - Add index.php into 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 Zends coding style. Our index.php basically set the $url variable and calls bootstrap.php which resides in our library directory.

Step four - View our bootstrap.php in library folder


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.

Step five - Example shared.php in library


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),$query String); 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();

You might also like