simsoft/slim
Composer 安装命令:
composer require simsoft/slim
包简介
A fluent routing wrapper for Slim Framework 4 with simplified request/response helpers, middleware, error handling, API resources, and DataTable support
关键字:
README 文档
README
A fluent routing wrapper for Slim Framework 4 that simplifies route setup, request/response handling, middleware, and error handling through a chainable builder API.
Requirements
- PHP >= 8.2
- Composer
Install
composer require simsoft/slim
Quick Start
Create an index.php entry file:
<?php declare(strict_types=1); require_once 'vendor/autoload.php'; use Simsoft\Slim\Route; use Slim\App; use function Simsoft\Slim\response; Route::make() ->withErrorHandler(false, true, true) ->withRouting(function(App $app) { // Define a GET route for the homepage $app->get('/', function() { response('Hello World!'); }); // {name} is a URL parameter — passed as a function argument $app->get('/{name}', function(string $name) { response("Hello $name!"); }); }) ->run();
Visit http://localhost/ to see "Hello World!" or http://localhost/john to
see "Hello John!"
Route Builder API
Everything is configured through a fluent (chainable) builder:
Route::make($container) // Optional PSR-11 dependency injection container ->withDomain('https://example.com') // Your app's domain (for URL generation) ->withBasePath('/api/v1') // Prefix all routes with this path ->withErrorHandler(...) // Configure error display and logging ->withMiddleware(function(App $app) { }) // Register global middleware ->withRouting( routes: function(App $app) { }, // Define your routes here cachePath: '/path/to/routes.cache', // Optional: cache routes for production ) ->run(); // Process the request and send a response
Controllers
Instead of closures, point routes to controller classes for cleaner code:
<?php namespace App; use function Simsoft\Slim\response; class UserController { public function index() { response('Users list'); } public function show(string $id) { response("User $id"); } // Returning a string sends it as text public function version(): string { return '1.0.0'; } // Returning an array sends it as JSON automatically public function list(): array { return ['users' => []]; } }
Register controller routes:
Route::make() ->withRouting(function(App $app) { $app->get('/users', [UserController::class, 'index']); $app->get('/users/{id}', [UserController::class, 'show']); }) ->run();
ContainerAwareTrait
Access container services (database, logger, etc.) as magic properties in your controllers:
use Simsoft\Slim\Traits\ContainerAwareTrait; /** * @property \App\Services\Logger $logger * @property \App\Services\Database $db */ class UserController { use ContainerAwareTrait; public function index() { $this->logger->info('accessed'); // Resolves $container->get('logger') $users = $this->db->fetchAll('users'); response($users); } }
Request & Response
Global helper functions for reading requests and sending responses:
use function Simsoft\Slim\request; use function Simsoft\Slim\response; // Reading the request request()->getQueryParams(); // Get URL query parameters (?key=value) request()->getParsedBody(); // Get POST body data request()->isMethod('post'); // Check HTTP method request()->isXHR(); // Detect AJAX requests request()->getBearerToken(); // Extract "Bearer xxx" token request()->urlFor('users.show', ['id' => '1']); // Generate URL from route name request()->notFound(); // Throw exception 404 // Sending responses response('Hello World'); // Plain text response(['status' => 'ok']); // JSON (arrays auto-encode) response('Error', 500); // Text with status code response()->json($data); // Explicit JSON response()->xml($xmlString); // XML with the correct content-type response()->redirect('/path', 301); // Redirect response()->header('X-Custom', 'val'); // Set response header
Full reference: docs/REQUEST_RESPONSE.md
URL Helper
Generate URLs from named routes — no hardcoded paths:
use Simsoft\Slim\URL; URL::for('users.show', ['id' => '42']); // /users/42 URL::fullFor('users.show', ['id' => '42']); // https://example.com/users/42
Middleware
Middleware runs on every request (or specific routes) to handle cross-cutting concerns like authentication, CORS, and rate limiting:
use Simsoft\Slim\Middlewares\Auth; use Simsoft\Slim\Middlewares\CORS; use Simsoft\Slim\Middlewares\CacheOff; use Simsoft\Slim\Middlewares\RateLimit; use Simsoft\Slim\Middlewares\Csrf; use Simsoft\Slim\Middlewares\SecurityHeaders; Route::make() ->withMiddleware(function(App $app) { $app->add(new SecurityHeaders()); // Security headers $app->add(new CORS('https://myapp.com')); // Allow cross-origin requests $app->add(new CacheOff()); // Prevent browser caching $app->add(new RateLimit(maxRequests: 100, windowSeconds: 60)); // 100 req/min $app->add(new Csrf()); // Protect forms from CSRF attacks $app->add(new Auth(fn($request) => $_SESSION['user'] ?? null)); // Require login }) ->withRouting(function(App $app) { /* ... */ }) ->run();
| Middleware | Purpose |
|---|---|
Auth |
Authentication + role/permission authorization |
CORS |
Cross-origin resource sharing headers |
CacheOff |
Disable browser caching |
Benchmark |
Response time and memory usage headers |
Logging |
Request/response logging with custom recorder |
RateLimit |
IP-based rate limiting with configurable window |
Quota |
API usage quota per user/key (daily/monthly) |
Csrf |
CSRF token validation for forms and AJAX |
SecurityHeaders |
XSS, clickjacking, MIME sniffing protection |
MaintenanceMode |
503 response with IP bypass |
IpFilter |
Whitelist or blacklist client IPs |
TrailingSlash |
Normalize trailing slashes in URLs |
ContentNegotiation |
Detect preferred format from Accept header |
ContentLength |
Add Content-Length header to responses |
MethodOverride |
Allow forms to simulate PUT/PATCH/DELETE |
Full reference: docs/builtin-middleware.md
Error Handling
Configure how errors are displayed and logged. Hide details in production, show them in development:
Route::make() ->withErrorHandler( displayError: false, // true in dev, false in production logError: true, // Write errors to log logErrorDetails: true, // Include stack traces errorHandlerClass: CustomErrorHandler::class, // Optional custom handler ) ->withRouting(function(App $app) { /* ... */ }) ->run();
Full reference: docs/ERROR_HANDLING.md
Plugins
- DataTable Plugin — Server-side response builder for jQuery DataTables
- API Resources — Data transformation layer for structured API responses
Documentation
📖 https://sim-soft.github.io/slim/
| Topic | Link |
|---|---|
| Route Builder | docs/route-builder.md |
| Defining Routes | docs/defining-routes.md |
| Controllers | docs/controllers.md |
| Middleware | docs/builtin-middleware.md |
| Request & Response | docs/REQUEST_RESPONSE.md |
| API Resources | docs/RESOURCE.md |
| Error Handling | docs/ERROR_HANDLING.md |
| DataTable Plugin | docs/PLUGIN_DATATABLE.md |
License
MIT License. See LICENSE for details.
simsoft/slim 适用场景与选型建议
simsoft/slim 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 175 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「routing」 「php」 「rest」 「api」 「Authentication」 「cors」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 simsoft/slim 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 simsoft/slim 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 simsoft/slim 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Write down your routing mapping at one place
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
Flight routing is a simple, fast PHP router that is easy to get integrated with other routers.
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
Alfabank REST API integration
统计信息
- 总下载量: 175
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-04