lighthouse/framework
最新稳定版本:v0.1.0
Composer 安装命令:
composer require lighthouse/framework
包简介
The Lighthouse PHP Framework - An educational MVC framework
README 文档
README
An educational PHP MVC framework designed to teach how modern frameworks work internally.
Installation
composer require lighthouse/framework
Requirements
- PHP 8.2 or higher
Quick Start
Create Your Application
public/index.php:
<?php require __DIR__ . '/../vendor/autoload.php'; use Lighthouse\Application; $app = new Application( basePath: dirname(__DIR__), debug: true ); // Configure views $app->useViews(__DIR__ . '/../views'); // Define routes $app->get('/', function () { return 'Hello, Lighthouse!'; }); $app->get('/users/{id}', function ($id) { return "User: {$id}"; }); // Run the application $app->run();
Routing
// Basic routes $app->get('/users', 'UserController@index'); $app->post('/users', 'UserController@store'); $app->put('/users/{id}', 'UserController@update'); $app->delete('/users/{id}', 'UserController@destroy'); // Route groups $app->group('/api', function ($router) { $router->get('/users', 'Api\UserController@index'); $router->get('/posts', 'Api\PostController@index'); }); // Named routes $app->get('/users/{id}', 'UserController@show')->name('users.show'); // Generate URLs $url = $app->url('users.show', ['id' => 123]); // /users/123
Controllers
<?php namespace App\Controllers; use Lighthouse\Controller; use Psr\Http\Message\ServerRequestInterface; class UserController extends Controller { public function index() { $users = [ ['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Jane'], ]; return $this->view('users.index', ['users' => $users]); } public function show(ServerRequestInterface $request, string $id) { return $this->json(['id' => $id, 'name' => 'John']); } public function store(ServerRequestInterface $request) { $data = $request->getParsedBody(); // ... create user return $this->redirect('/users'); } }
Views
views/users/index.php:
<?php $view->extends('layouts.main'); ?> <?php $view->section('content'); ?> <h1>Users</h1> <ul> <?php foreach ($users as $user): ?> <li><?= $view->e($user['name']) ?></li> <?php endforeach; ?> </ul> <?php $view->endSection(); ?>
Middleware
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\MiddlewareInterface; class AuthMiddleware implements MiddlewareInterface { public function process( ServerRequestInterface $request, RequestHandlerInterface $handler ): ResponseInterface { // Check authentication if (!$this->isAuthenticated($request)) { return new Response(401); } return $handler->handle($request); } } // Add middleware $app->pipe(new AuthMiddleware()); $app->pipe(function ($request, $handler) { // Callable middleware $response = $handler->handle($request); return $response->withHeader('X-Powered-By', 'Lighthouse'); });
Dependency Injection
// Bind services $container = $app->getContainer(); $container->bind(UserRepository::class, DatabaseUserRepository::class); $container->singleton(Logger::class, function ($container) { return new FileLogger('/var/log/app.log'); }); // Auto-injection in controllers class UserController extends Controller { public function __construct( Application $app, private UserRepository $users, private Logger $logger ) { parent::__construct($app); } }
Error Handling
use Lighthouse\ErrorHandler\Exception\NotFoundException; use Lighthouse\ErrorHandler\Exception\ForbiddenException; $app->get('/admin', function () { if (!isAdmin()) { throw new ForbiddenException('Admin access required'); } return 'Admin Panel'; }); $app->get('/users/{id}', function ($id) { $user = findUser($id); if (!$user) { throw new NotFoundException("User {$id} not found"); } return $user; });
JSON Responses
// Return array - automatically converted to JSON $app->get('/api/users', function () { return [ ['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Jane'], ]; }); // Or use the json helper $app->get('/api/users/{id}', function ($id) use ($app) { return $app->json(['id' => $id], 200); });
Redirects
$app->post('/login', function () use ($app) { // ... authenticate return $app->redirect('/dashboard'); }); // Redirect to named route $app->post('/users', function () use ($app) { // ... create user return $app->redirect($app->url('users.index')); });
Application Structure
my-app/
├── public/
│ └── index.php # Entry point
├── src/
│ └── Controllers/ # Your controllers
├── views/
│ ├── layouts/ # Layout templates
│ └── users/ # View templates
├── config/
│ └── app.php # Configuration
├── vendor/
└── composer.json
Architecture
Lighthouse is built from these independent packages:
| Package | Description |
|---|---|
lighthouse/http |
PSR-7 HTTP messages |
lighthouse/container |
PSR-11 DI container |
lighthouse/middleware |
PSR-15 middleware pipeline |
lighthouse/router |
HTTP router |
lighthouse/view |
PHP template engine |
lighthouse/error-handler |
Error/exception handling |
Each package can be used independently or as part of the full framework.
Educational Purpose
This framework is designed to be read and understood. It accompanies an educational book that teaches:
- How HTTP request/response works
- What dependency injection really does
- How routing matches URLs to code
- Why middleware is powerful
- How view engines work
- Best practices for error handling
The goal is not to replace Laravel or Symfony, but to make them understandable.
API Reference
Application
| Method | Description |
|---|---|
get/post/put/patch/delete(path, handler) |
Register routes |
group(prefix, callback) |
Create route group |
pipe(middleware) |
Add middleware |
useViews(path) |
Configure view engine |
run() |
Start the application |
view(name, data, status) |
Render view response |
json(data, status) |
Create JSON response |
redirect(url, status) |
Create redirect response |
url(name, params) |
Generate URL for named route |
getContainer() |
Get DI container |
getRouter() |
Get router |
getView() |
Get view engine |
Controller
| Method | Description |
|---|---|
view(name, data, status) |
Render view |
json(data, status) |
JSON response |
redirect(url, status) |
Redirect response |
redirectToRoute(name, params) |
Redirect to named route |
text(content, status) |
Plain text response |
html(content, status) |
HTML response |
empty(status) |
Empty response |
Testing
composer test
License
MIT License. See LICENSE for details.
Part of the Lighthouse Project
This is the main framework package of the Lighthouse Project, an educational PHP framework designed to teach how modern frameworks work internally.
Happy learning! 🚀
lighthouse/framework 适用场景与选型建议
lighthouse/framework 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「mvc」 「educational」 「lighthouse」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lighthouse/framework 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lighthouse/framework 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lighthouse/framework 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Kinikit - PHP Application development framework MVC component
Alfabank REST API integration
Small extensible framework in PHP
Oauth 2 client for Ubiquity framework
Light, secure and intuitive PHP Framework.
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-17