Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .
- +-- classes
- | +-- Database.php
- | +-- Route.php
- +-- controllers
- | +-- AboutUs.php
- | +-- Controller.php
- +-- views
- | +-- about-us.php
- +-- .htaccess
- +-- index.php
- +-- routes.php
- <?php
- class Database{
- public static $host = 'localhost';
- public static $dbName = 'cms';
- public static $username = 'root';
- public static $password = '';
- private static function connect(){
- $pdo = new PDO('mysql:host='.self::$host.';dbname='.self::$dbName.';charset=utf8', self::$username, self::$password);
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- return $pdo;
- }
- public static function query($query, $params = array()){
- $statement = self::connect()->prepare($query);
- $statement->execute($params);
- if(explode(' ', $query)[0] == 'SELECT'){
- $data = $statement->fetchAll();
- return $data;
- }
- }
- }
- ?>
- <?php
- class Route {
- public static function get($route, $function){
- //get method, don't continue if method is not the
- $method = $_SERVER['REQUEST_METHOD'];
- if($method !== 'GET'){ return; }
- //check the structure of the url
- $struct = self::checkStructure($route, $_SERVER['REQUEST_URI']);
- //if the requested url matches the one from the route
- //get the url params and run the callback passing the params
- if($struct){
- $params = self::getParams($route, $_SERVER['REQUEST_URI']);
- $function->__invoke($params);
- //prevent checking all other routes
- die();
- }
- }
- public static function urlToArray($url1, $url2){
- //convert route and requested url to an array
- //remove empty values caused by slashes
- //and refresh the indexes of the array
- $a = array_values(array_filter(explode('/', $url1), function($val){ return $val !== ''; }));
- $b = array_values(array_filter(explode('/', $url2), function($val){ return $val !== ''; }));
- //debug mode for development
- if(true) array_shift($b);
- return array($a, $b);
- }
- public static function checkStructure($url1, $url2){
- list($a, $b) = self::urlToArray($url1, $url2);
- //if the sizes of the arrays don't match, their structures don't match either
- if(sizeof($a) !== sizeof($b)){
- return false;
- }
- //for each value from the route
- foreach ($a as $key => $value){
- //if the static values from the url don't match
- // or the dynamic values start with a '?' character
- //their structures don't match
- if($value[0] !== ':' && $value !== $b[$key] || $value[0] === ':' && $b[$key][0] === '?'){
- return false;
- }
- }
- //else, their structures match
- return true;
- }
- public static function getParams($url1, $url2){
- list($a, $b) = self::urlToArray($url1, $url2);
- $params = array('params' => array(), 'query' => array());
- //foreach value from the route
- foreach($a as $key => $value){
- //if it's a dynamic value
- if($value[0] == ':'){
- //get the value from the requested url and throw away the query string (if any)
- $param = explode('?', $b[$key])[0];
- $params['params'][substr($value, 1)] = $param;
- }
- }
- //get the last item from the request url and parse the query string from it (if any)
- $queryString = explode('?', end($b))[1];
- parse_str($queryString, $params['query']);
- return $params;
- }
- }
- ?>
- <?php
- class AboutUs extends Controller{
- }
- ?>
- <?php
- class Controller extends Database{
- public static function CreateView($viewName, $urlParams = null){
- require_once('./views/'.$viewName.'.php');
- }
- }
- ?>
- <h1>About Us</h1>
- RewriteEngine On
- RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]
- <?php
- function loadClasses($class_name){
- $classesPath = './classes/'.$class_name.'.php';
- $controllersPath = './controllers/'.$class_name.'.php';
- if(file_exists($classesPath)){
- require_once($classesPath);
- }else if(file_exists($controllersPath)){
- require_once($controllersPath);
- }
- }
- spl_autoload_register(loadClasses);
- require_once('routes.php');
- ?>
- <?php
- Route::get('/about-us', function(){
- AboutUs::CreateView('about-us');
- });
- ?>
- //check the structure of the url
- $struct = self::checkStructure($route, $_SERVER['REQUEST_URI']);
- //if the requested url matches the one from the route
- //get the url params and run the callback passing the params
- if($struct){
- $params = self::getParams($route, $_SERVER['REQUEST_URI']);
- $function->__invoke($params);
- //prevent checking all other routes
- die();
- }
- /users/:username
- /users/:username/photos
- /users/:username/photos/:photoId
- /users/:username/photos/:photoId?showComments=false
- /users/jake1990
- /users/jake1990/photos
- /users/jake1990/photos/931280134
- /users/jake1990/photos/931280134?showComments=false
- Array -> [params => [username => jake1990], query: []];
- Array -> [params => [username => jake1990], query: []];
- Array -> [params => [username => jake1990, photoId => 931280134], query: []];
- Array -> [params => [username => jake1990, photoId => 931280134], query: [showComments => false]];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement