Create a new file: application/controller/blog.php
application/controller/blog.php
<?php/** * Class Blog * Handles blog-related pages and actions */class Blog extends Controller{ /** * PAGE: index * This is the main blog listing page * URL: http://yourproject/blog or http://yourproject/blog/index */ public function index() { // Load views require APP . 'view/_templates/header.php'; require APP . 'view/blog/index.php'; require APP . 'view/_templates/footer.php'; } /** * PAGE: article * This shows a single blog article * URL: http://yourproject/blog/article/5 */ public function article($id) { // For now, just pass the ID to the view // In a real app, you'd fetch article data from the database $article_id = $id; // Load views require APP . 'view/_templates/header.php'; require APP . 'view/blog/article.php'; require APP . 'view/_templates/footer.php'; }}
Controllers must extend the Controller base class, which provides database access via $this->db and model access via $this->model.
2
Create the View Directory
Create a directory for blog views:
mkdir application/view/blog
3
Create the Index View
Create application/view/blog/index.php:
application/view/blog/index.php
<div class="container"> <h1>Blog</h1> <p>Welcome to the blog!</p> <h2>Recent Articles</h2> <ul> <li><a href="<?php echo URL; ?>blog/article/1">My First Article</a></li> <li><a href="<?php echo URL; ?>blog/article/2">Learning MINI Framework</a></li> <li><a href="<?php echo URL; ?>blog/article/3">PHP Best Practices</a></li> </ul></div>
The URL constant contains your application’s base URL, ensuring links work in any environment or sub-folder.
4
Create the Article View
Create application/view/blog/article.php:
application/view/blog/article.php
<div class="container"> <h1>Article #<?php echo htmlspecialchars($article_id, ENT_QUOTES, 'UTF-8'); ?></h1> <p>This is article number <?php echo htmlspecialchars($article_id, ENT_QUOTES, 'UTF-8'); ?>.</p> <p>In a real application, this would display the actual article content from the database.</p> <a href="<?php echo URL; ?>blog">← Back to blog</a></div>
Always use htmlspecialchars() when outputting user-provided or dynamic content to prevent XSS attacks.
5
Test Your Controller
Open your browser and navigate to:
http://localhost/mini/blog
You should see your blog listing. Click on an article to see it in action!
Now let’s enhance our blog to fetch real data from the database.
1
Create a Database Table
Add a blog posts table to your database:
CREATE TABLE post ( id INT NOT NULL AUTO_INCREMENT, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id));-- Insert some sample dataINSERT INTO post (title, content) VALUES('Getting Started with MINI', 'MINI is an extremely simple PHP framework...'),('Database Operations', 'Working with PDO in MINI is straightforward...'),('Clean URLs', 'MINI provides beautiful clean URLs out of the box...');
2
Add Model Methods
Edit application/model/model.php and add blog methods:
application/model/model.php
/** * Get all blog posts */public function getAllPosts(){ $sql = "SELECT id, title, content, created_at FROM post ORDER BY created_at DESC"; $query = $this->db->prepare($sql); $query->execute(); return $query->fetchAll();}/** * Get a single blog post by ID * @param int $post_id */public function getPost($post_id){ $sql = "SELECT id, title, content, created_at FROM post WHERE id = :post_id LIMIT 1"; $query = $this->db->prepare($sql); $parameters = array(':post_id' => $post_id); // For debugging, you can use: // echo Helper::debugPDO($sql, $parameters); $query->execute($parameters); return $query->fetch();}
PDO automatically escapes all parameters, protecting against SQL injection. No need to manually sanitize input.
3
Update the Controller
Modify application/controller/blog.php to use the model:
application/controller/blog.php
public function index(){ // Get all posts from the database $posts = $this->model->getAllPosts(); // Load views require APP . 'view/_templates/header.php'; require APP . 'view/blog/index.php'; require APP . 'view/_templates/footer.php';}public function article($id){ // Get the specific post from the database $post = $this->model->getPost($id); // Load views require APP . 'view/_templates/header.php'; require APP . 'view/blog/article.php'; require APP . 'view/_templates/footer.php';}
4
Update the Views
Update application/view/blog/index.php to display real data:
MINI includes a helpful PDO debugger. Use it to see the actual SQL being executed:
$sql = "SELECT id, title FROM post WHERE id = :post_id LIMIT 1";$query = $this->db->prepare($sql);$parameters = array(':post_id' => $post_id);// Debug the query (shows the SQL with parameters replaced)echo Helper::debugPDO($sql, $parameters);$query->execute($parameters);