-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlogs.php
More file actions
99 lines (77 loc) · 2.26 KB
/
Blogs.php
File metadata and controls
99 lines (77 loc) · 2.26 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) 2021 CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace App\Controllers;
use App\Models\Blogs as ModelsBlogs;
class Blogs extends BaseController
{
protected $blogModel;
public function __construct()
{
$this->blogModel = new ModelsBlogs();
}
// List all blogs
public function index()
{
$page_title = 'Blogs';
$blogs = $this->blogModel->findAll();
return view('blogs/index', compact('page_title', 'blogs'));
}
// Show create blog form
public function create()
{
$page_title = 'Create Blog';
return view('blogs/create', compact('page_title'));
}
// Store a new blog
public function store()
{
$data = [
'title' => $this->request->getPost('title'),
'content' => $this->request->getPost('content'),
];
if ($this->blogModel->save($data)) {
return redirect()->to('/blogs');
}
return redirect()->back()->withInput()->with('errors', $this->blogModel->errors());
}
// Show a single blog
public function show($id)
{
$page_title = 'Blogs';
$blog = $this->blogModel->find($id);
return view('blogs/show', compact('page_title', 'blog'));
}
// Show edit blog form
public function edit($id)
{
$page_title = 'Edit Blog';
$blog = $this->blogModel->find($id);
return view('blogs/edit', compact('page_title', 'blog'));
}
// Update a blog
public function update($id)
{
$data = [
'title' => $this->request->getPost('title'),
'content' => $this->request->getPost('content'),
];
if ($this->blogModel->update($id, $data)) {
return redirect()->to('/blogs');
}
return redirect()->to("/blogs/edit/{$id}")->withInput()->with('errors', $this->blogModel->errors());
}
// Delete a blog
public function delete($id)
{
$this->blogModel->delete($id);
return redirect()->to('/blogs');
}
}