-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathBlog.php
More file actions
70 lines (59 loc) · 1.67 KB
/
Blog.php
File metadata and controls
70 lines (59 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace App\Controllers;
use App\Libraries\Blog as BlogLibrary;
use CodeIgniter\Exceptions\PageNotFoundException;
use Config\Blog as BlogConfig;
class Blog extends BaseController
{
/**
* @var BlogConfig
*/
protected $config;
/**
* @var BlogLibrary
*/
protected $blog;
public function __construct()
{
$this->config = config(BlogConfig::class);
$this->blog = new BlogLibrary();
}
/**
* Displays posts based on date (reverse chronological order)
*/
public function index()
{
echo $this->render('blog/list', [
'posts' => $this->blog->getRecentPosts($this->config->perPage),
]);
}
/**
* Displays posts based on category.
*/
public function category(string $category)
{
echo $this->render('blog/list', [
'posts' => $this->blog->getRecentPosts($this->config->perPage, 0, $category),
'pageTitle' => "Category: {$category}",
'category' => $category,
]);
}
/**
* Displays a single post
*/
public function post(string $slug)
{
$post = $this->blog->getPost($slug);
if (empty($post)) {
throw PageNotFoundException::forPageNotFound();
}
// Save a hit to this page. Will go simple for now and
// just record every time someone refreshes the page
// but at some point we might need to make it a little smarter.
$this->blog->recordVisit($slug);
echo $this->render('blog/single', [
'post' => $this->blog->getPost($slug),
'title' => $post->title ?? 'Some Post',
]);
}
}