Michael Boeyens 93ffb44abe added base route and menu controller | 3 years ago | |
---|---|---|
.phpdoc/cache | 3 years ago | |
.vscode | 3 years ago | |
cache | 3 years ago | |
db | 3 years ago | |
docs | 3 years ago | |
lib | 3 years ago | |
notes | 3 years ago | |
public | 3 years ago | |
templates | 3 years ago | |
.DS_Store | 3 years ago | |
.gitignore | 3 years ago | |
README.md | 3 years ago | |
composer.json | 3 years ago | |
composer.lock | 3 years ago | |
run-composer.sh | 3 years ago | |
run-xdebug.sh | 3 years ago |
#windows
docker run --rm -tiv ${pwd}:/app composer
#mac-linux
docker run --rm -tiv $PWD:/app composer
#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
#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 hostip adres: niet met localhost verbinden, maar met host.docker.internal
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.
<?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();
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");
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();
});
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');
});
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;
}
}
$data = array('name' => 'Rob', 'age' => 40);
$payload = json_encode($data);
$response->getBody()->write($payload);
return $response
->withHeader('Content-Type', 'application/json')
->withStatus(201);
return $response
->withHeader('Location', 'https://www.example.com')
->withStatus(302);
// 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
// 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
// Fetch array -> return row from result query
public SQLite3Result::fetchArray ( int $mode = SQLITE3_BOTH ) : array|false
//vb
$res->fetchArray(SQLITE3_ASSOC)
{% for user in users %}
li>{{ user.username|e }}</li>
{% endfor %}
{% for box in boxes %}
{{ include('render_box.html') }}
{% endfor %}
{% extends "base.html" %}
{% block title %}Index{% endblock %}