-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Yuvraj Sablania
committed
Jan 16, 2020
0 parents
commit 7b8c664
Showing
23 changed files
with
343 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
vendor | ||
config.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
class PagesController | ||
{ | ||
public function home() | ||
{ | ||
return view('index'); | ||
} | ||
|
||
public function about() | ||
{ | ||
return view('about'); | ||
} | ||
|
||
public function contact() | ||
{ | ||
$name = 'Laracast'; | ||
|
||
return view('contact', compact('name')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Core\App; | ||
|
||
class UsersController | ||
{ | ||
public function index() | ||
{ | ||
$users = App::get('database')->selectAll('users'); | ||
|
||
return view('users', compact('users')); | ||
} | ||
|
||
public function store() | ||
{ | ||
App::get('database')->insert('users', [ | ||
'name' => $_POST['name'], | ||
]); | ||
|
||
return redirect('users'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
class Project | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
$router->get('', 'PagesController@home'); | ||
$router->get('about', 'PagesController@about'); | ||
$router->get('contact', 'PagesController@contact'); | ||
|
||
$router->get('users', 'UsersController@index'); | ||
|
||
$router->post('users', 'UsersController@store'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php require 'partials/head.php' ?> | ||
|
||
<h1>About us</h1> | ||
|
||
<?php require 'partials/footer.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?php require 'partials/head.php' ?> | ||
|
||
<h1>Contact <?= $name; ?> | ||
</h1> | ||
|
||
<?php require 'partials/footer.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?php require 'partials/head.php' ?> | ||
|
||
<h1>home page</h1> | ||
|
||
<?php require 'partials/footer.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!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</title> | ||
</head> | ||
|
||
<body> | ||
|
||
<?php require 'nav.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<nav> | ||
<li><a href="/">home</a></li> | ||
<li><a href="/about">about</a></li> | ||
<li><a href="/users">users</a></li> | ||
<li><a href="/contact">contact</a></li> | ||
</nav> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php require 'partials/head.php' ?> | ||
|
||
<h1>all users</h1> | ||
|
||
<ul> | ||
<?php foreach ($users as $user): ?> | ||
<li><?= $user->name ?> | ||
</li> | ||
<?php endforeach; ?> | ||
</ul> | ||
|
||
<h1> Enter your name</h1> | ||
<form action="/users" method="POST"> | ||
<input type="text" name="name"> | ||
<button type="submit">Submit</button> | ||
</form> | ||
|
||
<?php require 'partials/footer.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"autoload": { | ||
"classmap": [ | ||
"./" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
return [ | ||
'database' => [ | ||
'name' => 'your_database_name', | ||
'username' => 'your_database_username', | ||
'password' => 'your_database_password', | ||
'connection' => 'mysql:host=127.0.0.1', | ||
'options' => [ | ||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
class App | ||
{ | ||
protected static $registry = []; | ||
|
||
public static function bind($key, $value) | ||
{ | ||
static::$registry[$key] = $value; | ||
} | ||
|
||
public static function get($key) | ||
{ | ||
if (!array_key_exists($key, static::$registry)) { | ||
throw new Exception("No {$key} found in the container"); | ||
} | ||
|
||
return static::$registry[$key]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
class Request | ||
{ | ||
public static function uri() | ||
{ | ||
return trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); | ||
} | ||
|
||
public static function method() | ||
{ | ||
return $_SERVER['REQUEST_METHOD']; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
use Exception; | ||
|
||
class Router | ||
{ | ||
protected $routes = [ | ||
'GET' => [], | ||
'POST' => [], | ||
]; | ||
|
||
public static function load($file) | ||
{ | ||
$router = new static(); | ||
|
||
require $file; | ||
|
||
return $router; | ||
} | ||
|
||
public function get($url, $controller) | ||
{ | ||
$this->routes['GET'][$url] = $controller; | ||
} | ||
|
||
public function post($url, $controller) | ||
{ | ||
$this->routes['POST'][$url] = $controller; | ||
} | ||
|
||
public function direct($uri, $requestType) | ||
{ | ||
if (array_key_exists($uri, $this->routes[$requestType])) { | ||
return $this->callAction( | ||
...explode('@', $this->routes[$requestType][$uri]) | ||
); | ||
} | ||
|
||
throw new Exception('Error 404 page not found'); | ||
} | ||
|
||
protected function callAction($controller, $action) | ||
{ | ||
$controller = "App\\Controllers\\{$controller}"; | ||
$controller = new $controller(); | ||
if (method_exists($controller, $action)) { | ||
return $controller->$action(); | ||
} | ||
|
||
throw new Exception('Error method not found'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use App\Core\App; | ||
use App\Core\Database\Connection; | ||
use App\Core\Database\QueryBuilder; | ||
|
||
App::bind('config', require 'config.php'); | ||
App::bind('database', new QueryBuilder( | ||
Connection::make(App::get('config')['database']) | ||
)); | ||
|
||
function view($viewName, $data = []) | ||
{ | ||
extract($data); | ||
|
||
return require "app/views/{$viewName}.view.php"; | ||
} | ||
|
||
function redirect($path) | ||
{ | ||
header("location: /{$path}"); | ||
} | ||
|
||
function dd($var) | ||
{ | ||
echo '<pre>'; | ||
die(var_dump($var)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Core\Database; | ||
|
||
use PDO; | ||
use PDOException; | ||
|
||
class Connection | ||
{ | ||
public static function make($config) | ||
{ | ||
try { | ||
return new PDO( | ||
$config['connection'] . ';dbname=' . $config['name'], | ||
$config['username'], | ||
$config['password'], | ||
$config['options'] | ||
); | ||
} catch (PDOException $e) { | ||
die($e->getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Core\Database; | ||
|
||
use PDO; | ||
use PDOException; | ||
|
||
class QueryBuilder | ||
{ | ||
protected $pdo; | ||
|
||
public function __construct(PDO $pdo) | ||
{ | ||
$this->pdo = $pdo; | ||
} | ||
|
||
public function selectAll($table) | ||
{ | ||
$statement = $this->pdo->prepare("select * from {$table}"); | ||
$statement->execute(); | ||
|
||
return $statement->fetchAll(PDO::FETCH_CLASS); | ||
} | ||
|
||
protected function getInsertQueryString($table, $params) | ||
{ | ||
return sprintf( | ||
"insert into {$table} (%s) values (%s)", | ||
implode(', ', array_keys($params)), | ||
':' . implode( | ||
', :', | ||
array_keys($params) | ||
) | ||
); | ||
} | ||
|
||
public function insert($table, $params) | ||
{ | ||
try { | ||
$statement = $this->pdo->prepare($this->getInsertQueryString($table, $params)); | ||
$statement->execute($params); | ||
} catch (PDOException $e) { | ||
die('Whoops, Something went wrong'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
require 'vendor/autoload.php'; | ||
require 'core/bootstrap.php'; | ||
|
||
use App\Core\Router; | ||
use App\Core\Request; | ||
|
||
Router::load('app/routes.php') | ||
->direct(Request::uri(), Request::method()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
body { | ||
background: #999; | ||
} |