소스 검색

branch customers + fix typo's + customer MVC v1

customers
Ramdan Katakpawou 2 년 전
부모
커밋
f5f1d1d2a0
10개의 변경된 파일198개의 추가작업 그리고 34개의 파일을 삭제
  1. 23
    17
      public/index.php
  2. 0
    0
      src/Controller/.gitkeep
  3. 36
    0
      src/Controller/Shop.php
  4. 0
    11
      src/DB/Customers.php
  5. 51
    0
      src/DB/CustomersDB.php
  6. 0
    0
      src/Model/.gitkeep
  7. 43
    0
      src/View/twig.php
  8. 0
    6
      src/templates/base.html.twig
  9. 12
    0
      templates/base.html.twig
  10. 33
    0
      templates/customers.html.twig

+ 23
- 17
public/index.php 파일 보기

@@ -1,23 +1,33 @@
<?php
namespace Shop;
session_start();

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
Use Shop\DB\DB;

use DI\Container;
use Shop\View\Twig;
require __DIR__ . '/../vendor/autoload.php';
// maak namespace in een aangemaakte file en verplaats mydb
/*class myDB extends \SQLite3
{
public function __construct()
{
$this->open('../private/webshop.db');
}

}*/
$container = new Container();
AppFactory::setContainer($container);

$app = AppFactory::create();

$container->set('twig', function () {
$twig = new Twig('../templates');
return $twig;
});

$container->set(
'db',
function () {
$db = new DB();
return $db;
}
);

$app->map(['GET', 'POST'], "/orders/{client}/create", function (Request $request, Response $response, array $args) {
if ($request->getMethod() == "GET") {
$db2 = new DB();
@@ -168,8 +178,8 @@ $app->get('/order/{id}', function (Request $request, Response $response, array $
});

// put this in src db
$app->get('/customers', function (Request $request, Response $response, array $args) {
$db = new DB();
$app->get('/customers',\Shop\Controller\Shop::class . ':customers');/*function (Request $request, Response $response, array $args*/
/*$db = new DB();
$sql = "SELECT * FROM clients";
$stmt = $db->prepare($sql);
$res = $stmt->execute();
@@ -179,14 +189,10 @@ $app->get('/customers', function (Request $request, Response $response, array $a
$response->getBody()->write('<hr/>');
$response->getBody()->write("<table>");
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
// maak hier een deftige tabel van
// maak hier een deftige tabel van; maak twig template.
$response->getBody()->write("<tr><td>" . " " . " <a href=\"/orders/" . $row['id'] . "\">" . $row["id"] . "</a> " . " " . $row['firstname'] . " " . $row['lastname'] . " " . $row['street'] . " " . $row['street2'] . " " . $row['zipcode'] . " " . $row['city'] . " " . $row['country'] . "</td></tr>");
}
$response->getBody()->write("</table>");

return $response;
});



return $response;*/
$app->run();

+ 0
- 0
src/Controller/.gitkeep 파일 보기


+ 36
- 0
src/Controller/Shop.php 파일 보기

@@ -0,0 +1,36 @@
<?php

namespace Shop\Controller;


use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Container\ContainerInterface;
use Shop\DB\CustomersDB;

class Shop {
protected $container;
protected $twig;

public function __construct(ContainerInterface $container){
$this->container = $container;

}
public function customers(ServerRequestInterface $request, ResponseInterface $response, array $args){

$container = $this->container;
$twig = $container->get('twig');
$db = new CustomersDB($this->container);
$customers = $db->loadCust();

$var = ["customerList" => $customers];

$container = $this->container;


$render = $twig->render('customers.html.twig', $var);
$response->getBody()->write($render);
return $response;
}
}

+ 0
- 11
src/DB/Customers.php 파일 보기

@@ -1,11 +0,0 @@
<?php

use Shop\DB\DB;

class Customers extends DB {
public function customers(){
$stmt= $this->prepare("SELECT * FROM clients");
$res = $stmt->execute();
return $res;
}
}

+ 51
- 0
src/DB/CustomersDB.php 파일 보기

@@ -0,0 +1,51 @@
<?php

namespace Shop\DB;

use Shop\DB\DB;
use Shop\Model\Customers;
use Psr\Container\ContainerInterface;

class CustomersDB extends DB {
protected $container;
protected $customers = [];

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function loadCust(){
$stmt= $this->container->get('webshop.db')->prepare("SELECT * FROM clients");
$res = $stmt->execute();

$stmt ->bindValue(':id',SQLITE3_TEXT);
$stmt ->bindValue(':firstname', SQLITE3_TEXT);
$stmt ->bindValue(':lastname', SQLITE3_TEXT);
$stmt ->bindValue(':company', SQLITE3_TEXT );
$stmt ->bindValue(':vatnumber', SQLITE3_TEXT);
$stmt ->bindValue(':street', SQLITE3_TEXT);
$stmt ->bindValue(':street2nd', SQLITE3_TEXT);
$stmt ->bindValue(':zipcode', SQLITE3_TEXT);
$stmt ->bindValue(':city', SQLITE3_TEXT);
$stmt ->bindValue(':state', SQLITE3_TEXT);
$stmt ->bindValue(':country', SQLITE3_TEXT);

while($result = $res->fetchArray(SQLITE3_ASSOC)){
$customer = new Customers($this->container);
$customer->setId($result['id']);
$customer->setFirstname($result['firstname']);
$customer->setLastname($result['lastname']);
$customer->setCompany($result['company']);
$customer->setVatNumber($result['vatnumber']);
$customer->setStreet($result['street']);
$customer->setStreet2nd($result['street2']);
$customer->setZipcode($result['zipcode']);
$customer->setCity($result['city']);
$customer->setState($result['state']);
$customer->setCountry($result['country']);
$this->customers[] = $customer;
}
return $res;
}
}

+ 0
- 0
src/Model/.gitkeep 파일 보기


+ 43
- 0
src/View/twig.php 파일 보기

@@ -0,0 +1,43 @@
<?php

namespace Shop\View;

use Twig\Loader\FilesystemLoader;
use Twig\Environment;

class Twig
{

protected $twig;
protected $variables;

public function __construct($tmpl_folder)
{
$loader = new FilesystemLoader($tmpl_folder);
$this->twig = new Environment($loader, []);
$this->variables = [];
}

protected function load($tmpl)
{
return $this->twig->load($tmpl);
}

public function addBlockVariable($block, $data)
{
$current = $this->variables[$block];
if ($current) {
$new = array_merge($current, $data);
} else {
$new = $data;
}
$this->variables[$block] = $new;
}

public function render($tmpl, $vars)
{
$template = $this->load($tmpl);
$variables = array_merge($this->variables, $vars);
return $template->render($variables);
}
}

+ 0
- 6
src/templates/base.html.twig 파일 보기

@@ -1,6 +0,0 @@
<html>
<body>
<div id="main">{% block main%}{% {% endblock %}%}</div>
</body>
<html></html>
</html>

+ 12
- 0
templates/base.html.twig 파일 보기

@@ -0,0 +1,12 @@
<html>
<link
rel="stylesheet"
href="https://unpkg.com/purecss@2.0.6/build/pure-min.css"
integrity="sha384-Uu6IeWbM+gzNVXJcM9XV3SohHtmWE+3VGi496jvgX1jyvDTXfdK+rfZc8C1Aehk5"
crossorigin="anonymous"
/>
<body>
<div id="main">{% block main%} {% endblock %}</div>
</body>
<html></html>
</html>

+ 33
- 0
templates/customers.html.twig 파일 보기

@@ -0,0 +1,33 @@
{% extends "base.html.twig"%} {% block main%}
<h1>CUSTOMERS LIST</h1>
<hr/>
<table>
<tr>
<th>Id</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Company</th>
<th>VAT Number</th>
<th>Street</th>
<th>2nd Street</th>
<th>Zipcode</th>
<th>City</th>
<th>State</th>
<th>Country</th>
</tr>
{% for customer in customerList%}
<tr>
<td><a href="/orders/{{customer.id}}">{{customer.id}}</td>
<td>{{customer.firstname}}</td>
<td>{{customer.lastname}}</td>
<td>{{customer.company}}</td>
<td>>{{customer.vatnumber}}</td>
<td>{{customer.street}}</td>
<td>{{customer.street2nd}}</td>
<td>{{customer.zipcode}}</td>
<td>{{customer.city}}</td>
<td>{{customer.state}}</td>
<td>{{customer.country}}</td>
{% endfor%}
</table>
{% endblock%}

Loading…
취소
저장