Skip to content

Commit

Permalink
add template
Browse files Browse the repository at this point in the history
  • Loading branch information
trungpv1601 committed Jul 12, 2017
1 parent 888e986 commit 34ac055
Show file tree
Hide file tree
Showing 7,548 changed files with 789,347 additions and 268 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
11 changes: 10 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
APP_DEBUG=true
APP_NAME='Pig Framework'
APP_DEBUG=true
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pig
DB_USERNAME=pig
DB_PASSWORD=secret
47 changes: 39 additions & 8 deletions app/Controllers/ErrorController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@

use Exception;
use Symfony\Component\HttpFoundation\Response;
use Twig_Environment;
use Symfony\Component\HttpFoundation\Request;
use League\Plates\Engine;

/**
* Handles all requests error /.
*
*/
class ErrorController
{
/** @var Twig_Environment */
private $view;

private $request;

/**
* ErrorController, constructed by the container
*
* @param Twig_Environment $view
* @param Engine $view
*/
public function __construct(Twig_Environment $view)
public function __construct(Engine $view)
{
$this->view = $view;
$this->request = Request::createFromGlobals();
}

/**
Expand All @@ -31,16 +34,44 @@ public function __construct(Twig_Environment $view)
*/
public function page404()
{
return new Response($this->view->render('errors/404.html.twig'));
$response = new Response($this->view->render('/errors/404'), Response::HTTP_NOT_FOUND);
if ($response instanceof Response) {
// Send the generated response back to the user
$response
->prepare($this->request)
->send();
}
}

/**
* Throw an exception (for testing the error handler)
* 405 page
*
* @throws Exception
* @return Response
*/
public function page405()
{
return new Response($this->view->render('errors/405.html.twig'));
$response = new Response($this->view->render('/errors/405'), Response::HTTP_METHOD_NOT_ALLOWED);
if ($response instanceof Response) {
// Send the generated response back to the user
$response
->prepare($this->request)
->send();
}
}

/**
* 500 page
*
* @return Response
*/
public function page500($message)
{
$response = new Response($this->view->render('/errors/500', ['message' => $message]));
if ($response instanceof Response) {
// Send the generated response back to the user
$response
->prepare($this->request)
->send();
}
}
}
42 changes: 0 additions & 42 deletions app/Controllers/GreetController.php

This file was deleted.

30 changes: 25 additions & 5 deletions app/Controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use Exception;
use Symfony\Component\HttpFoundation\Response;
use Twig_Environment;
use League\Plates\Engine;

/**
* Handles all requests to /.
Expand All @@ -13,16 +13,16 @@
class IndexController
{
/** @var Twig_Environment */
private $twig;
private $view;

/**
* IndexController, constructed by the container
*
* @param Twig_Environment $twig
*/
public function __construct(Twig_Environment $twig)
public function __construct(Engine $view)
{
$this->twig = $twig;
$this->view = $view;
}

/**
Expand All @@ -32,7 +32,27 @@ public function __construct(Twig_Environment $twig)
*/
public function index()
{
return new Response($this->twig->render('pages/index.html.twig'));
return new Response($this->view->render('index', ['name' => 'Jonathan']));
}

/**
* Login page
*
* @return Response
*/
public function login()
{
return new Response($this->view->render('login'));
}

/**
* Register page
*
* @return Response
*/
public function register()
{
return new Response($this->view->render('register'));
}

/**
Expand Down
73 changes: 37 additions & 36 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,30 @@
$request = Request::createFromGlobals();


/*
* Container setup
*/
$container = new Container();
$container
->add('League\Plates\Engine', new \League\Plates\Engine(__DIR__ . "/../views"));
$container
->delegate(
// Auto-wiring based on constructor typehints.
// http://container.thephpleague.com/auto-wiring
new ReflectionContainer()
);
// Get Container App\Controllers\ErrorController
$errorController = $container->get('App\Controllers\ErrorController');


/*
* Dotenv initialization
*/
if (file_exists(__DIR__ . '/../.env') !== true) {
Response::create('Missing .env file (please copy .env.example).', Response::HTTP_INTERNAL_SERVER_ERROR)
->prepare($request)
->send();
// Response::create('Missing .env file (please copy .env.example).', Response::HTTP_INTERNAL_SERVER_ERROR)
// ->prepare($request)
// ->send();
$errorController->page500('Missing .env file (please copy .env.example).');
return;
}
$dotenv = new Dotenv\Dotenv(__DIR__ . '/../');
Expand All @@ -42,34 +59,16 @@
$whoops->pushHandler(
// Using the pretty error handler in production is likely a bad idea.
// Instead respond with a generic error message.
function () use ($request) {
Response::create('An internal server error has occurred.', Response::HTTP_INTERNAL_SERVER_ERROR)
->prepare($request)
->send();
function () use ($errorController) {
// Response::create('An internal server error has occurred.', Response::HTTP_INTERNAL_SERVER_ERROR)
// ->prepare($request)
// ->send();
$errorController->page500('An internal server error has occurred.');
}
);
}
$whoops->register();


/*
* Container setup
*/
$container = new Container();
$container
->add('Twig_Environment')
->withArgument(
// Our twig templates are stored inside of the views directory.
new Twig_Loader_Filesystem(__DIR__ . '/../views/')
);
$container
->delegate(
// Auto-wiring based on constructor typehints.
// http://container.thephpleague.com/auto-wiring
new ReflectionContainer()
);


/*
* Routes
*/
Expand All @@ -88,15 +87,19 @@ function () use ($request) {
switch ($routeInfo[0]) {
case Dispatcher::NOT_FOUND:
// No matching route was found.
Response::create("404 Not Found", Response::HTTP_NOT_FOUND)
->prepare($request)
->send();

$errorController->page404();
// Response::create("404 Not Found", Response::HTTP_NOT_FOUND)
// ->prepare($request)
// ->send();
break;
case Dispatcher::METHOD_NOT_ALLOWED:
// A matching route was found, but the wrong HTTP method was used.
Response::create("405 Method Not Allowed", Response::HTTP_METHOD_NOT_ALLOWED)
->prepare($request)
->send();

$errorController->page405();
// Response::create("405 Method Not Allowed", Response::HTTP_METHOD_NOT_ALLOWED)
// ->prepare($request)
// ->send();
break;
case Dispatcher::FOUND:
// Fully qualified class name of the controller
Expand All @@ -122,8 +125,6 @@ function () use ($request) {
default:
// According to the dispatch(..) method's documentation this shouldn't happen.
// But it's here anyways just to cover all of our bases.
Response::create('Received unexpected response from dispatcher.', Response::HTTP_INTERNAL_SERVER_ERROR)
->prepare($request)
->send();
$errorController->page500('Received unexpected response from dispatcher');
return;
}
}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
"symfony/var-dumper": "^3.2",
"filp/whoops": "^2.1",
"vlucas/phpdotenv": "^2.4",
"twig/twig": "^1.34",
"catfan/Medoo": "^1.4"
"catfan/Medoo": "^1.4",
"league/plates": "^3.3"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
"files" : ["helpers.php"]
}
}
Loading

0 comments on commit 34ac055

Please sign in to comment.