Skip to content

Instantly share code, notes, and snippets.

@ta-riq
Created January 1, 2018 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ta-riq/81c955152cb9f65fdafd107ff69f976e to your computer and use it in GitHub Desktop.
Save ta-riq/81c955152cb9f65fdafd107ff69f976e to your computer and use it in GitHub Desktop.
MY ROUTER
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1> About Tariq</h1>
</body>
</html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>About US</h1>
</body>
</html>
<?php
return [
'DB' => [
'Username' => 'root',
'Password' => '',
'Host' => 'mysql:hostname=localhost',
'Schema' => 'mytodo',
'options' => []
]
];
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Contact US</h1>
</body>
</html>
<?php
$query = require 'core/bootstrap.php';
$router = new Router;
require 'routes.php';
$uri = trim($_SERVER['REQUEST_URI'],'/');
require $router->direct($uri);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</titl<?php
$configuration = require 'Config.php';
require 'core/Router.php';
require 'core/database/Connection.php';
require 'core/database/QueryBuilder.php';
return new QueryBuilder(Connection::make($configuration));
<?php
/**
* Class Router
*/
class Router
{
/**
* @var array
*/
protected $routes = array();
/**
* @param $routes
*/
public function define($routes)
{
$this->routes = $routes;
}
/**<?php
/**
* Class Connection
*/
class Connection
{
/**
* @param $configuration
* @return PDO
*/
public static function make($configuration)
{
try
{
return new PDO
(
$configuration['DB']['Host'] . ';dbname='.
$configuration['DB']['Schema'],
$configuration['DB']['Username'],
$configuration['DB']['Password'],
$configuration['DB']['options']
);
}
catch(PDOexception $e)
{
die($e->getMessage());
}
}
}
<?php
class QueryBuilder
{
protected $pdo;
public function __<?php
require 'views/about.view.php';
<?php
require 'views/about-tariq.view.php';
<?php
require 'views/contact.view.php';
<?php
$tasks = $query->selectAll('todos');
require 'views/index.view.php';
construct($pdo)
{
$this->pdo = $pdo;
}
public function selectAll($table)
{
$statement = $this->pdo->prepare("SELECT * FROM {$table}");
$statement->execute();
return $statement->fetchAll(PDO::FETCH_CLASS);
}
}
* @param $uri
* @return mixed
* @throws Exception
*/
public function direct($uri)
{
if (array_key_exists($uri, $this->routes))
{
return $this->routes[$uri];
}
throw new Exception('No route defined for this URI.');
}
}e>
</head>
<body>
<nav>
<ul>
<li><a href="about.view.php">About Page</a></li>
<li><a href="contact.view.php">Contact Page</a></li>
</ul>
</nav>
<h1>My Tasks</h1>
<ul>
<?php foreach($tasks as $task): ?>
<li>
<?php if($task->completed == true): ?>
<strike><?= $task->description; ?></strike>
<?php else: ?>
<?= $task->description; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
<?php
$router->define(
[
'' => 'controllers/index.php',
'about' => 'controllers/about.php',
'about/tariq' => 'controllers/about-tariq.php',
'contact' => 'controllers/contact.php'
]
);
s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment