Bram Dieudonne 2 роки тому
джерело
коміт
a0113cadc5
2 змінених файлів з 135 додано та 17 видалено
  1. 49
    0
      .vscode/launch.json
  2. 86
    17
      public/index.php

+ 49
- 0
.vscode/launch.json Переглянути файл

@@ -0,0 +1,49 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": { "/src": "${workspaceRoot}" }
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 0,
"runtimeArgs": [
"-dxdebug.start_with_request=yes"
],
"env": {
"XDEBUG_MODE": "debug,develop",
"XDEBUG_CONFIG": "client_port=${port}"
}
},
{
"name": "Launch Built-in web server",
"type": "php",
"request": "launch",
"runtimeArgs": [
"-dxdebug.mode=debug",
"-dxdebug.start_with_request=yes",
"-S",
"localhost:0"
],
"program": "",
"cwd": "${workspaceRoot}",
"port": 9003,
"serverReadyAction": {
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started",
"uriFormat": "http://localhost:%s",
"action": "openExternally"
}
}
]
}

+ 86
- 17
public/index.php Переглянути файл

@@ -1,4 +1,5 @@
<?php
session_start();

use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
@@ -8,35 +9,103 @@ 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;
});
function addNavbar($response)
{

$response->getBody()->write("<html><head></head><body>");
if (isset($_SESSION['username'])) {
$response->getBody()->write('<div><a href="/">Index</a> | <a href="/blog/create">Create Blog</a> | <a href="/logout">Logout</a></div>');
} else {
$response->getBody()->write('<div><a href="/">Index</a> | <a href="/login">Login</a></div>');
}
return;
}

function addFooter($response)
{
$response->getBody()->write('<div>Privacy statement | Cookie Policy | Contact</div>');
$response->getBody()->write("</body></html>");
return;
}

$app->get('/', function (Request $request, Response $response, array $args) {
$response->getBody()->write("Index");
return $response;

addNavbar($response);

$response->getBody()->write('<hr/><h1>Onze blog</h1>');
$response->getBody()->write('<ul>');
$response->getBody()->write('<li><a href="/blog/artikel-1">Blogartikel 1</a></li>');
$response->getBody()->write('<li><a href="/blog/artikel-2">Blogartikel 2</a></li>');
$response->getBody()->write('<li><a href="/blog/artikel-3">Blogartikel 3</a></li>');
$response->getBody()->write('<li><a href="/blog/artikel-4">Blogartikel 4</a></li>');
$response->getBody()->write('</ul>');
$response->getBody()->write('<hr/>');
addFooter($response);


return $response;
});


$app->get('/blog/{slug}', function (Request $request, Response $response, array $args) {
$response->getBody()->write("Blog met slug");
return $response;

addNavbar($response);
$title = $args['slug'];
$response->getBody()->write("<h1>$title</h1>");

addFooter($response);
return $response;
});

$app->post('/login', function (Request $request, Response $response, array $args) {
$response->getBody()->write("login");
return $response;
$app->get('/logout', function (Request $request, Response $response, array $args) {
unset($_SESSION['username']);
addNavbar($response);
$response->getBody()->write('Logged out');
addFooter($response);
return $response->withHeader('Location', '/')->withStatus(302);
});

$app->map(['GET', 'POST'], '/login', function (Request $request, Response $response, array $args) {

if ($request->getMethod() == 'GET') {
addNavbar($response);
$response->getBody()->write('<form action="/login" method="POST">');
$response->getBody()->write('<label for="username">Username:</label>');
$response->getBody()->write('<input type="text" name="username"/><br/>');
$response->getBody()->write('<label for="password">Password:</label>');
$response->getBody()->write('<input type="password" name="password"/><br/>');
$response->getBody()->write('<input type="submit"/>');
addFooter($response);
} else {
$postdata = $request->getParsedBody();
if ($postdata['username'] == 'gebruiker' && $postdata['password'] == "abcd") {
$_SESSION["username"] = $postdata['username'];
addNavbar($response);
$response->getBody()->write('Logged in');
addFooter($response);
return $response->withHeader('Location', '/')->withStatus(302);
} else {
$response->getBody()->write('Username and/or password incorrect');
}
}
return $response;
});

$app->post('/postcomment', function (Request $request, Response $response, array $args) {
$response->getBody()->write("Postcomment");
return $response;
$response->getBody()->write("Postcomment");
return $response;
});

$app->post('/blog/create', function (Request $request, Response $response, array $args) {
$response->getBody()->write("Blog create");
return $response;
$response->getBody()->write("Blog create");
$response->getBody()->write('<form action="/blog/{slug}" method="POST">');
$response->getBody()->write('<label for="title">Title</label>');
$response->getBody()->write('<input type="text" name="title"/></br>');
$response->getBody()->write('<label for="article">Blog post content</label>');
$response->getBody()->write('<input type="text" name="article"/></br>');
$response->getBody()->write('<input type="submit"/>');
$response->getBody()->write('</form>');
return $response;
});

$app->run();
$app->run();

Завантаження…
Відмінити
Зберегти