andyvanee / miniroute
PHP 的最小化路由器
0.0.1
2019-01-03 21:00 UTC
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-08-29 05:22:27 UTC
README
PHP 的最小化路由器/应用程序框架。此代码处于 预-alpha 阶段,因此可能发生非常重大的变化!
安装
composer require andyvanee/miniroute
使用
你可以以千万种方式设置你的应用程序,但推荐的结构类似于以下内容
- src/MyApp/Controller
- HomeController.php
- public
- index.php
- configuration.php
- routes.php
src/MyApp/Controller/HomeController.php
namespace MyApp\Controller; class HomeController { public function __construct($container) { // Setup anything that is common to all routes in this controller } public function index($request, $response) { // Handle this route } }
public/index.php
require '../vendor/autoload.php'; $app = new MiniRoute\MiniRoute; require 'configuration.php'; require 'routes.php'; $app->run();
public/configuration.php
$app->register('helloservice', function() { return 'Hello Service'; });
public/routes.php
use MyApp\Controller\HomeController; $app->route('GET', '/', [HomeController::class, 'index']);
如果你想对应用程序的全局行为和功能有更多的控制,你可以子类化 MiniRoute
,或者创建一个基类,如 AppController
,所有的控制器都将继承自它。
运行测试
cd andyvanee/miniroute && composer run test