入门篇 - MVC 博客应用

知识库
知识库文档
/tech-stacks/laminas/tutorial/入门篇 - MVC 博客应用.md

文档

Laminas MVC 入门教程:博客应用

1. 背景

Laminas MVC 是企业级 PHP 开发的经典选择。本教程通过构建博客应用,掌握 MVC 骨架中的 Module、Controller、Service 和 ORM。

2. 前置概念

概念 说明
Module Laminas 的模块单元,每个功能一个模块
ModuleManager 加载和配置模块
ServiceManager DI 容器,管理服务实例
TableGateway 数据库表抽象模式

3. 分步操作

步骤一:创建项目

composer create-project laminas/laminas-mvc-skeleton blog-app
cd blog-app
composer development-enable

步骤二:创建 Blog 模块

# 模块目录结构
mkdir -p module/Blog/src/{Controller,Model,Form}
mkdir -p module/Blog/view/blog/blog

module/Blog/Config/module.config.php

<?php
namespace Blog;

use Laminas\Router\Http\Segment;

return [
    'router' => [
        'routes' => [
            'blog' => [
                'type' => Segment::class,
                'options' => [
                    'route' => '/blog[/:action[/:id]]',
                    'defaults' => [
                        'controller' => Controller\BlogController::class,
                        'action' => 'index',
                    ],
                ],
            ],
        ],
    ],
    'controllers' => [
        'factories' => [
            Controller\BlogController::class => function ($container) {
                return new Controller\BlogController($container->get('BlogTable'));
            },
        ],
    ],
    'view_manager' => [
        'template_path_stack' => [
            'blog' => __DIR__ . '/../view',
        ],
    ],
];

module/Blog/Module.php

<?php
namespace Blog;

class Module
{
    public function getConfig(): array
    {
        return include __DIR__ . '/Config/module.config.php';
    }
}

步骤三:创建数据库和 TableGateway

module/Blog/src/Model/Post.php

<?php
namespace Blog\Model;

class Post
{
    public int $id;
    public string $title;
    public string $content;
    public string $created_at;

    public function exchangeArray(array $data): void
    {
        $this->id = (int) ($data['id'] ?? 0);
        $this->title = $data['title'] ?? '';
        $this->content = $data['content'] ?? '';
        $this->created_at = $data['created_at'] ?? date('Y-m-d H:i:s');
    }
}

module/Blog/src/Model/BlogTable.php

<?php
namespace Blog\Model;

use Laminas\Db\TableGateway\TableGateway;
use Laminas\Db\Sql\Select;

class BlogTable
{
    public function __construct(private TableGateway $tableGateway) {}

    public function fetchAll(): iterable
    {
        return $this->tableGateway->select(function (Select $select) {
            $select->order('created_at DESC');
        });
    }

    public function getPost(int $id): ?Post
    {
        return $this->tableGateway->select(['id' => $id])->current() ?: null;
    }

    public function savePost(Post $post): void
    {
        $data = [
            'title' => $post->title,
            'content' => $post->content,
        ];

        if ($post->id === 0) {
            $this->tableGateway->insert($data);
        } else {
            $this->tableGateway->update($data, ['id' => $post->id]);
        }
    }
}

步骤四:创建控制器

module/Blog/src/Controller/BlogController.php

<?php
namespace Blog\Controller;

use Blog\Model\BlogTable;
use Blog\Model\Post;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;

class BlogController extends AbstractActionController
{
    public function __construct(private BlogTable $table) {}

    public function indexAction(): ViewModel
    {
        return new ViewModel(['posts' => $this->table->fetchAll()]);
    }

    public function addAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            $post = new Post();
            $post->exchangeArray($request->getPost()->toArray());
            $this->table->savePost($post);
            return $this->redirect()->toRoute('blog');
        }
        return new ViewModel();
    }
}

步骤五:注册模块

config/modules.config.php

return [
    'Laminas\ZendFrameworkBridge',
    'Laminas\Router',
    'Application',
    'Blog',  // ← 新增
];

4. 验证

php -S localhost:8080 -t public/
# 访问 http://localhost:8080/blog

5. 思考题

  1. 如何用 Laminas Form 代替手动表单?
  2. 如何使用 AbstractRestfulController 构建 REST API?
  3. 如何集成 Doctrine ORM 替代 TableGateway?

信息

路径
/tech-stacks/laminas/tutorial/入门篇 - MVC 博客应用.md
更新时间
2026/5/31