├── .htaccess ├── app ├── controllers │ ├── app_controller.php │ └── welcome_controller.php ├── models │ └── .gitignore └── views │ ├── layouts │ └── application.html.php │ └── welcome │ ├── add.html.php │ ├── edit.html.php │ └── index.html.php ├── config └── routes.php ├── index.php ├── libraries ├── controller.php ├── core.php ├── object.php ├── router.php └── sammy.php └── tmp └── .gitignore /.htaccess: -------------------------------------------------------------------------------- 1 | RewriteEngine On 2 | RewriteBase / 3 | 4 | RewriteCond %{REQUEST_FILENAME} !-f 5 | RewriteCond %{REQUEST_FILENAME} !-d 6 | RewriteRule (.*) index.php/$1 -------------------------------------------------------------------------------- /app/controllers/app_controller.php: -------------------------------------------------------------------------------- 1 | name = 'Baylor'; 7 | } 8 | 9 | public function add() { 10 | // when creating a new db record 11 | echo 'add action'; 12 | } 13 | 14 | public function create() { 15 | // actually create the record 16 | echo 'create action'; 17 | } 18 | 19 | public function edit() { 20 | // show the edit form 21 | echo 'edit action'; 22 | } 23 | 24 | public function update() { 25 | // update the record 26 | echo 'update action'; 27 | } 28 | 29 | public function destroy() { 30 | // delete a record 31 | echo 'destroy action'; 32 | } 33 | } -------------------------------------------------------------------------------- /app/models/.gitignore: -------------------------------------------------------------------------------- 1 | # allows github to show this empty folder -------------------------------------------------------------------------------- /app/views/layouts/application.html.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | <?php echo $site_title ?> 5 | 6 | 7 | 8 | 9 | 10 | {PAGE_CONTENT} 11 | 12 |

https://github.com/BaylorRae/PHP-MVC-Tutorial

13 | 14 | -------------------------------------------------------------------------------- /app/views/welcome/add.html.php: -------------------------------------------------------------------------------- 1 |
2 | 3 |
-------------------------------------------------------------------------------- /app/views/welcome/edit.html.php: -------------------------------------------------------------------------------- 1 |
2 | 3 | 4 | 5 |
-------------------------------------------------------------------------------- /app/views/welcome/index.html.php: -------------------------------------------------------------------------------- 1 | My name is -------------------------------------------------------------------------------- /config/routes.php: -------------------------------------------------------------------------------- 1 | run(); -------------------------------------------------------------------------------- /libraries/object.php: -------------------------------------------------------------------------------- 1 | $action(); 78 | }else 79 | die('The action ' . $action . ' could not be called from the controller ' . $class_name . ''); 80 | }else 81 | die('The class ' . $class_name . ' could not be found in
' . APP_PATH . 'controllers/' . $controller . '_controller.php
'); 82 | 83 | self::get_user_vars($tmp_class); 84 | 85 | // include the view file 86 | // self::load_view($controller, $action, $format); 87 | 88 | // load the layout 89 | $layout_path = self::get_layout($controller, $action, $format); 90 | if( !empty($layout_path) ) { 91 | $layout = file_get_contents($layout_path); 92 | 93 | // replace {PAGE_CONTENT} view file 94 | $view_path = self::view_path($controller, $action, $format); 95 | if( !empty($view_path) ) 96 | $layout = str_replace('{PAGE_CONTENT}', file_get_contents($view_path), $layout); 97 | else 98 | $layout = str_replace('{PAGE_CONTENT}', '', $layout); 99 | 100 | $filename = BASE_PATH . 'tmp/' . time() . '.php'; 101 | 102 | $file = fopen($filename, 'a'); 103 | fwrite($file, $layout); 104 | fclose($file); 105 | 106 | self::load_layout($filename); 107 | 108 | unlink($filename); 109 | } 110 | } 111 | 112 | public static function load_controller($name) { 113 | $controller_path = APP_PATH . 'controllers/' . $name . '_controller.php'; 114 | if( file_exists($controller_path) ) 115 | include_once $controller_path; 116 | else 117 | die('The file ' . $name . '_controller.php could not be found at
' . $controller_path . '
'); 118 | } 119 | 120 | public static function load_view($controller, $action, $format) { 121 | $view_path = self::view_path(); 122 | if( !empty($view_path) ) { 123 | unset($controller, $action, $format); 124 | 125 | foreach( self::$__user_vars as $var => $value ) { 126 | $$var = $value; 127 | } 128 | 129 | include_once $view_path; 130 | } 131 | } 132 | 133 | public static function get_layout($controller, $action, $format) { 134 | // controller-action.format.php 135 | $controller_action_path = APP_PATH . 'views/layouts/' . $controller . '-' . $action . '.' . $format . '.php'; 136 | 137 | // controller.format.php 138 | $controller_path = APP_PATH . 'views/layouts/' . $controller . '.' . $format . '.php'; 139 | 140 | // application.format.php 141 | $application_path = APP_PATH . 'views/layouts/application.' . $format . '.php'; 142 | 143 | $path_to_use = null; 144 | 145 | // find the path to use 146 | if( file_exists($controller_action_path) ) 147 | $path_to_use = $controller_action_path; 148 | 149 | elseif( file_exists($controller_path) ) 150 | $path_to_use = $controller_path; 151 | 152 | elseif( file_exists($application_path) ) 153 | $path_to_use = $application_path; 154 | 155 | return $path_to_use; 156 | } 157 | 158 | public static function view_path($controller, $action, $format) { 159 | $view_path = APP_PATH . 'views/' . $controller . '/' . $action . '.' . $format . '.php'; 160 | $path = null; 161 | 162 | if( file_exists($view_path) ) 163 | $path = $view_path; 164 | 165 | return $path; 166 | } 167 | 168 | public static function load_layout($filename) { 169 | foreach( self::$__user_vars as $var => $value ) { 170 | $$var = $value; 171 | } 172 | 173 | include $filename; 174 | } 175 | 176 | } -------------------------------------------------------------------------------- /libraries/sammy.php: -------------------------------------------------------------------------------- 1 | get_uri(false)); 37 | } 38 | 39 | ob_end_flush(); 40 | } 41 | 42 | public static function process($route, $type) { 43 | $sammy = static::instance(); 44 | 45 | // Check for ajax 46 | if( $type == 'XMLHttpRequest' ) 47 | $sammy->method = isset($_SERVER['HTTP_X_REQUESTED_WITH']) ? $_SERVER['HTTP_X_REQUESTED_WITH'] : 'GET'; 48 | 49 | if( static::$route_found || (!preg_match('@^'.$route.'(?:\.(\w+))?$@uD', $sammy->uri, $matches) || $sammy->method != $type) ) { 50 | return false; 51 | } 52 | 53 | // Get the extension 54 | $extension = $matches[count($matches)-1]; 55 | $extension_test = substr($sammy->uri, -(strlen($extension)+1), (strlen($extension)+1)); 56 | 57 | if( $extension_test == '.' . $extension ) 58 | $sammy->format = $extension; 59 | else 60 | $sammy->format = 'html'; 61 | 62 | static::$route_found = true; 63 | Map::dispatch($sammy->format); 64 | } 65 | 66 | public function __construct() { 67 | ob_start(); 68 | $this->uri = $this->get_uri(); 69 | $this->segments = explode('/', trim($this->uri, '/')); 70 | $this->method = $this->get_method(); 71 | } 72 | 73 | public function segment($num) { 74 | $num--; 75 | 76 | // Remove the extension 77 | $this->segments[$num] = isset($this->segments[$num]) ? rtrim($this->segments[$num], '.' . $this->format) : null; 78 | 79 | return isset($this->segments[$num]) ? $this->segments[$num] : null; 80 | } 81 | 82 | protected function get_method() { 83 | return isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : 'GET'; 84 | } 85 | 86 | protected function get_uri($prefix_slash = true) { 87 | if( isset($_SERVER['PATH_INFO']) ) { 88 | $uri = $_SERVER['PATH_INFO']; 89 | }elseif( isset($_SERVER['REQUEST_URI']) ) { 90 | $uri = $_SERVER['REQUEST_URI']; 91 | 92 | if( strpos($uri, $_SERVER['SCRIPT_NAME']) === 0 ) { 93 | $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); 94 | }elseif( strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0 ) { 95 | $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); 96 | } 97 | 98 | // This section ensures that even on servers that require the URI to be in the query string (Nginx) a correct 99 | // URI is found, and also fixes the QUERY_STRING server var and $_GET array. 100 | if( strncmp($uri, '?/', 2) === 0 ) { 101 | $uri = substr($uri, 2); 102 | } 103 | 104 | $parts = preg_split('#\?#i', $uri, 2); 105 | $uri = $parts[0]; 106 | 107 | if( isset($parts[1]) ) { 108 | $_SERVER['QUERY_STRING'] = $parts[1]; 109 | parse_str($_SERVER['QUERY_STRING'], $_GET); 110 | }else { 111 | $_SERVER['QUERY_STRING'] = ''; 112 | $_GET = array(); 113 | } 114 | $uri = parse_url($uri, PHP_URL_PATH); 115 | }else { 116 | // Couldn't determine the URI, so just return false 117 | return false; 118 | } 119 | 120 | // Do some final cleaning of the URI and return it 121 | return ($prefix_slash ? '/' : '').str_replace(array('//', '../'), '/', trim($uri, '/')); 122 | } 123 | 124 | public function format($name, $callback) { 125 | $sammy = static::instance(); 126 | if( !empty($sammy->format) && $name == $sammy->format ) 127 | echo $callback($sammy); 128 | else 129 | return false; 130 | } 131 | } 132 | 133 | $sammy = Sammy::instance(); -------------------------------------------------------------------------------- /tmp/.gitignore: -------------------------------------------------------------------------------- 1 | # allows github to show this empty folder --------------------------------------------------------------------------------