Browse Source

reset

master^2
Ernest Debruyne 2 years ago
parent
commit
8a58b88026
7 changed files with 114 additions and 43 deletions
  1. 18
    30
      public/index.php
  2. 17
    0
      src/DB/Blog.php
  3. 16
    0
      src/DB/Menu.php
  4. 39
    4
      src/model/Blog.php
  5. 13
    0
      templates/blog.html.twig
  6. 10
    8
      templates/index.html.twig
  7. 1
    1
      templates/navbar.html.twig

+ 18
- 30
public/index.php View File

@@ -11,10 +11,11 @@ use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
use Blog\DB\User;
use Blog\DB\Blog as BlogDB;
use Blog\Model\Blogs;
use Blog\View\Twig;
use Blog\DB\DB;
use Blog\Model\Blogs;
use Blog\Model\Menu;

use Blog\Model\Blog;


@@ -46,23 +47,15 @@ $container->set(

function addNavbar($twig)
{
$urls = [
["link" => "https://www.google.be", "name" => "Google"],
["link" => "https://www.facebook.com", "name" => "Facebook"],
["link" => "https://www.twiter.com", "name" => "Twitter"]
];
$twig->addBlockVariable('navbar', $urls);
$menu = new Menu('primary');
$twig->addBlockVariable('navbar', $menu->getLinks());
}


function addNavbar2($twig)
{
$urls = [
["link" => "https://www.google.be", "name" => "Bing"],
["link" => "https://www.facebook.com", "name" => "Apple"],
["link" => "https://www.twiter.com", "name" => "TikTok"]
];
$twig->addBlockVariable('navbar', $urls);
$menu = new Menu('secondary');
$twig->addBlockVariable('navbar', $menu->getLinks());
}

function addFooter($response)
@@ -71,17 +64,10 @@ function addFooter($response)
$response->getBody()->write("</body></html>");
return;
}
function init()
{
$art1 = ["slug" => "artikel-1", "title" => "Dit is titel 1", "content" => "Lorem ipsum 1"];
$art2 = ["slug" => "artikel-2", "title" => "Dit is titel 2", "content" => "Lorem ipsum 2"];
$art3 = ["slug" => "artikel-3", "title" => "Dit is titel 3", "content" => "Lorem ipsum 3"];
$art4 = ["slug" => "artikel-4", "title" => "Dit is titel 4", "content" => "Lorem ipsum 4"];
$_SESSION['blogs'] = [$art1, $art2, $art3, $art4];
}


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

$blogs = new Blogs();
$blogs = $blogs->getBlogs();

@@ -141,15 +127,17 @@ $app->map(['GET', 'POST'], '/blog/create', function (Request $request, Response
});

$app->get('/blog/{slug}', function (Request $request, Response $response, array $args) {
$blogs = new Blogs();
$blogs = $blogs->getBlog($args['slug']);
$vars = [
"content" => $blogs
];

addNavbar($response);
foreach ($_SESSION['blogs'] as $art) {
if ($art['slug'] == $args['slug']) {
$response->getBody()->write("<h1>" . $art['title'] . "</h1>");
}
}
$twig = $this->get('twig');
addNavbar($twig);
$a = $twig->render('blog.html.twig', $vars);
$response->getBody()->write($a);

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


+ 17
- 0
src/DB/Blog.php View File

@@ -16,4 +16,21 @@ class Blog extends DB
$res = $stmt->execute();
return $res;
}

public function getBlog($slug)
{
$sql = "SELECT * FROM blogs WHERE slug = :slug ;";
$stmt = $this->prepare($sql);
$stmt->bindValue(':slug', $slug, SQLITE3_TEXT);
$res = $stmt->execute();
return $res;
}

public function getBlogs()
{
$sql = "SELECT * FROM blogs";
$stmt = $this->prepare($sql);
$res = $stmt->execute();
return $res;
}
}

+ 16
- 0
src/DB/Menu.php View File

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

namespace Blog\DB;

class Menu extends DB
{
// Fetch all links for a given menu type
public function getMenu($type)
{
$sql = "SELECT * FROM menu WHERE type = :type ;";
$stmt = $this->prepare($sql);
$stmt->bindValue(":type", $type);
$res = $stmt->execute();
return $res;
}
}

+ 39
- 4
src/model/Blog.php View File

@@ -37,6 +37,45 @@ class Blog
return $this->author;
}

public function setContent($value)
{
$this->content = $value;
}

public function setID($value)
{
$this->id = $value;
}

public function setSlug($value)
{
$this->slug = $value;
}

public function setTitle($value)
{
$this->title = $value;
}

public function getContent()
{
return $this->content;
}

public function getID()
{
return $this->id;
}
public function getSlug()
{
return $this->slug;
}
public function getTitle()
{
return $this->title;
}


public function getComments()
{
$comments = new Comment();
@@ -51,8 +90,4 @@ class Blog
//$db->createBlog($data);
$db->createBlog($this);
}

public function getAssociative()
{
}
}

+ 13
- 0
templates/blog.html.twig View File

@@ -0,0 +1,13 @@
{% extends "base.html.twig" %} {% block main %}

<ul>
{% for blog in content.getBlogsList %}

<h1>{{ blog.title }}</h1>

{{
blog.content
}}
{% endfor %}
</ul>
{% endblock %}

+ 10
- 8
templates/index.html.twig View File

@@ -1,8 +1,10 @@
{% extends "base.html.twig" %}


{% block main %}

{{ content }}

{% endblock %}
{% extends "base.html.twig" %} {% block main %}

<ul>
{% for blog in content.getBlogsList%}
<li>
<a href="/blog/{{ blog.slug }}">{{ blog.title }}</a>
</li>
{% endfor %}
</ul>
{% endblock %}

+ 1
- 1
templates/navbar.html.twig View File

@@ -2,7 +2,7 @@
<ul>
{% for url in navbar %}
<li>
<a href="{{ url.link }}">{{ url.name }} </a>
<a href="{{ url.url }}">{{ url.name }} </a>
</li>
{% endfor %}
</ul>

Loading…
Cancel
Save