Quellcode durchsuchen

oefening opgelost

blogs_session
DESKTOP-Q2SHMM6\ernes vor 2 Jahren
Ursprung
Commit
af5ae4fa08
1 geänderte Dateien mit 29 neuen und 25 gelöschten Zeilen
  1. 29
    25
      public/index.php

+ 29
- 25
public/index.php Datei anzeigen

@@ -14,7 +14,7 @@ function addNavbar($response)

$response->getBody()->write("<html><head></head><body>");
if (isset($_SESSION['username'])) {
$response->getBody()->write('<div><a href="/">Index</a> | <a href="/create">Create Blog</a> | <a href="/logout">Logout</a></div>');
$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>');
}
@@ -39,7 +39,9 @@ function init()


$app->get('/', function (Request $request, Response $response, array $args) {
init();
if (!isset($_SESSION['blogs'])) {
init();
}
addNavbar($response);
$response->getBody()->write('<hr/><h1>Onze blog</h1>');
$response->getBody()->write('<ul>');
@@ -54,14 +56,36 @@ $app->get('/', function (Request $request, Response $response, array $args) {
return $response;
});

$app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response $response, array $args) {
if (isset($_SESSION['username'])) {
if ($request->getMethod() == 'GET') {
addNavbar($response);
$response->getBody()->write('<form action="/blog/create" method="POST">');
$response->getBody()->write('<label for="slug">slug:</label>');
$response->getBody()->write('<input type="text" name="slug"/><br/>');
$response->getBody()->write('<label for="title">Titel:</label>');
$response->getBody()->write('<input type="text" name="title"/><br/>');
$response->getBody()->write('<input type="submit"/>');
addFooter($response);
} else {
$data = $request->getParsedBody();
$_SESSION['blogs'][] = ["slug" => $data['slug'], "title" => $data['title'], "content" => "Lorem ipsum 4"];
return $response->withHeader('Location', '/')->withStatus(302);
}
} else {
addNavbar($response);
$response->getBody()->write(('Please login'));
addFooter($response);
}
return $response;
});

$app->get('/blog/{slug}', function (Request $request, Response $response, array $args) {

addNavbar($response);
foreach ($_SESSION['blogs'] as $art) {
if ($art['slug'] == $args['slug']) {
$response->getBody()->write("<h1>" . $art['title'] . "</h1></br>");
$response->getBody()->write("<p>" . $art['content'] . "</p>");
$response->getBody()->write("<h1>" . $art['title'] . "</h1>");
}
}

@@ -108,26 +132,6 @@ $app->post('/postcomment', function (Request $request, Response $response, array
return $response;
});

$app->get('/create', function (Request $request, Response $response, array $args) {
if (isset($_SESSION['username'])) {
addNavbar($response);
$response->getBody()->write("<h1>Blog create</h1>");
$response->getBody()->write('<form action="" method="POST" name="newArticle">');
$response->getBody()->write('<label for="title">Title</label>');
$response->getBody()->write('<input type="text" name="title"/></br>');
$response->getBody()->write('<label for="article">Content</label>');
$response->getBody()->write('<textarea name="article" rows="10" cols="50"></textarea></br>');
$response->getBody()->write('<input type="submit" name="submit"/>');
$response->getBody()->write('</form>');
addFooter($response);
} else {
addNavbar($response);
$response->getBody()->write('Please Log In');
addFooter($response);
return $response->withHeader('Location', '/')->withStatus(302);
}
return $response;
});


$app->run();

Laden…
Abbrechen
Speichern