Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuvraj Sablania committed Jan 16, 2020
0 parents commit 7b8c664
Show file tree
Hide file tree
Showing 23 changed files with 343 additions and 0 deletions.
Binary file added ..gitignore.swp
Binary file not shown.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
config.php
23 changes: 23 additions & 0 deletions app/controllers/PagesController.php
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'));
}
}
24 changes: 24 additions & 0 deletions app/controllers/UsersController.php
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');
}
}
7 changes: 7 additions & 0 deletions app/models/project.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace App\Models;

class Project
{
}
9 changes: 9 additions & 0 deletions app/routes.php
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');
5 changes: 5 additions & 0 deletions app/views/about.view.php
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';
6 changes: 6 additions & 0 deletions app/views/contact.view.php
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';
5 changes: 5 additions & 0 deletions app/views/index.view.php
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';
3 changes: 3 additions & 0 deletions app/views/partials/footer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
</body>

</html>
13 changes: 13 additions & 0 deletions app/views/partials/head.php
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';
6 changes: 6 additions & 0 deletions app/views/partials/nav.php
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>
18 changes: 18 additions & 0 deletions app/views/users.view.php
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';
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"autoload": {
"classmap": [
"./"
]
}
}
13 changes: 13 additions & 0 deletions config.example.php
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,
],
],
];
22 changes: 22 additions & 0 deletions core/App.php
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];
}
}
16 changes: 16 additions & 0 deletions core/Request.php
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'];
}
}
54 changes: 54 additions & 0 deletions core/Router.php
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');
}
}
28 changes: 28 additions & 0 deletions core/bootstrap.php
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));
}
23 changes: 23 additions & 0 deletions core/database/Connection.php
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());
}
}
}
46 changes: 46 additions & 0 deletions core/database/QueryBuilder.php
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');
}
}
}
10 changes: 10 additions & 0 deletions index.php
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());
3 changes: 3 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: #999;
}

0 comments on commit 7b8c664

Please sign in to comment.