You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Michael Boeyens 93ffb44abe added base route and menu controller 3 years ago
.phpdoc/cache php documentor 3 years ago
.vscode added settings.json 3 years ago
cache added slim middleware and error middleware 3 years ago
db change db 3 years ago
docs php documentor 3 years ago
lib added base route and menu controller 3 years ago
notes changes run-xdebug.sh 3 years ago
public added base route and menu controller 3 years ago
templates login also works with json 3 years ago
.DS_Store change db 3 years ago
.gitignore Merge branch 'master' into lesnotities 3 years ago
README.md added links to readme 3 years ago
composer.json installed guzzle 3 years ago
composer.lock composer update 3 years ago
run-composer.sh lesnotities 3 years ago
run-xdebug.sh lesnotities 3 years ago

README.md

Notes e-commerce

Content

Docker

Startup Composer

#windows
docker run --rm -tiv ${pwd}:/app composer

#mac-linux
docker run --rm -tiv $PWD:/app composer

Startup php/Xdebug

#windows
docker run --rm -tiv ${pwd}:/src -p 8000:8000 debaetsr/php_xdebug

#mac-linux
docker run --rm -tiv $PWD:/src -p 8000:8000 debaetsr/php_xdebug

run phpdocumentor

#windows
docker run --rm -v ${pwd}:/data phpdoc/phpdoc:3 -d . -t ./docs

#mac-linux
docker run --rm -v $PWD:/data phpdoc/phpdoc:3 -d . -t ./docs

Use host-ip adress

use hostip adres: niet met localhost verbinden, maar met host.docker.internal


Slim Framework

Add own library to autoloader

Add to composer.json:

"autoload": {
    "psr-4": {
      "MyApp\\": "lib"
    }
  }

Put autoload in namespace:

docker run --rm --interactive --tty --volume $PWD:/app composer dump-autoload -o

composer: --optimize (-o): Convert PSR-0/4 autoloading to classmap to get a faster autoloader. This is recommended especially for production, but can take a bit of time to run, so it is currently not done by default.

Basic App Route

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->run();

Slim Controller Setup

namespace MyApp\Controller;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class ClassName {
private $container;

// constructor receives container instance
public function __construct(ContainerInterface $container){
$this->container = $container;}

public function functionName(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface{
return $response;}
}
$app->map(['GET', 'POST'], '/route', \MyApp\Controller\ControllerRoute::class . ":classname");

Create container in Slim

use DI\Container;
use Slim\Factory\AppFactory;

require __DIR__ . '/../vendor/autoload.php';

$container = new Container();
AppFactory::setContainer(($container));
$app = AppFactory::create();

//vb: link db in container
$container->set('db', function () {
    return new DB();
});

Create groups of routes

use Slim\Routing\RouteCollectorProxy;
// ...

$app->group('/users/{id:[0-9]+}', function (RouteCollectorProxy $group) {
    $group->map(['GET', 'DELETE', 'PATCH', 'PUT'], '', function ($request, $response, array $args) {
        // Find, delete, patch or replace user identified by $args['id']
        // ...

        return $response;
    })->setName('user');

    $group->get('/reset-password', function ($request, $response, array $args) {
        // Route for /users/{id:[0-9]+}/reset-password
        // Reset the password for user identified by $args['id']
        // ...

        return $response;
    })->setName('user-password-reset');
});

Middleware class

use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Psr7\Response;

class ExampleBeforeMiddleware
{
    /**
     * Example middleware invokable class
     *
     * @param  ServerRequest  $request PSR-7 request
     * @param  RequestHandler $handler PSR-15 request handler
     *
     * @return Response
     */
    public function __invoke(Request $request, RequestHandler $handler): Response
    {
        $response = $handler->handle($request);
        $existingContent = (string) $response->getBody();

        $response = new Response();
        $response->getBody()->write('BEFORE' . $existingContent);

        return $response;
    }
}

Returning JSON

$data = array('name' => 'Rob', 'age' => 40);
$payload = json_encode($data);

$response->getBody()->write($payload);
return $response
          ->withHeader('Content-Type', 'application/json')
          ->withStatus(201);

Returning redirect

return $response
  ->withHeader('Location', 'https://www.example.com')
  ->withStatus(302);

Sqlite

Sqlite date format

  • YYYY-MM-DD
  • YYYY-MM-DD HH:MM
  • YYYY-MM-DD HH:MM:SS
  • YYYY-MM-DD HH:MM:SS.SSS
  • YYYY-MM-DDTHH:MM
  • YYYY-MM-DDTHH:MM:SS
  • YYYY-MM-DDTHH:MM:SS.SSS
  • HH:MM
  • HH:MM:SS
  • HH:MM:SS.SSS
  • now
  • DDDDDDDDDD

Sqlite php commands: sqlite3 class

// prepare query, return as statement object
public SQLite3::prepare ( string $query ) : SQLite3Stmt|false
// vb
$stmt = $this->db->prepare('SELECT * FROM products WHERE id = :productId');

// Executes an SQL query for resultless object
public SQLite3::exec ( string $query ) : bool

// return last error code
public SQLite3::lastErrorCode ( ) : int

//return last error message
public SQLite3::lastErrorMsg ( ) : string

//return id for last inserted row
public SQLite3::lastInsertRowID ( ) : int

Sqlite php commands: sqlite3Stmt class

// Bind parameters from sql command
public SQLite3Stmt::bindParam ( string|int $param , mixed &$var , int $type = SQLITE3_TEXT ) : bool
// vb
$stmt->bindValue(":productId", $productId, SQLITE3_NUM);

// execute sqliteStmt and return SQLite3Result object
public SQLite3Stmt::execute ( ) : SQLite3Result|false

Sqlite php commands: sqlite3Result class

// Fetch array -> return row from result query
public SQLite3Result::fetchArray ( int $mode = SQLITE3_BOTH ) : array|false
//vb
$res->fetchArray(SQLITE3_ASSOC)

Twig

For loop for array

{% for user in users %}
      li>{{ user.username|e }}</li>
{% endfor %}

Include other templates

{% for box in boxes %}
    {{ include('render_box.html') }}
{% endfor %}

Template Inheritance

{% extends "base.html" %}

{% block title %}Index{% endblock %}

Cockpit

Filter query: operators

  • $not or $ne: not equal to
  • $lt: less then
  • $lte: less then or equal to
  • $gt: greater then
  • $gte: greater then or equal to

github link