phastasf/framework
最新稳定版本:1.0.0
Composer 安装命令:
composer require phastasf/framework
包简介
A lightweight PHP framework for building CLI, web and API applications
关键字:
README 文档
README
A lightweight, modern PHP framework for building CLI, web, and API applications. Built on PSR standards with a clean, intuitive API.
Features
- PSR Standards: PSR-7, PSR-11, PSR-15, PSR-3, PSR-6, PSR-16, PSR-20
- Dependency Injection: Automatic dependency resolution
- Routing & Middleware: Fast routing with PSR-15 middleware pipeline
- Database & ORM: Multi-database support (MySQL, PostgreSQL, SQLite, SQL Server) with Datum ORM
- Migrations: Schema versioning with Kram
- Views: Template engine with layout inheritance
- Authentication: JWT-based authentication
- Queue System: Job queues with Redis and ElasticMQ/SQS
- Caching: Multiple backends (File, Memory, Redis, Predis, Memcache)
- Logging: Flexible logging with multiple backends
- Email: SMTP, Mailgun, and Resend transports
- Validation: Powerful input validation
- Console Commands: Built-in CLI commands and generators
Requirements
- PHP 8.2+
- Composer
Installation
composer require phastasf/framework
Quick Start
Web Application
Create public/index.php:
<?php require __DIR__.'/../vendor/autoload.php'; define('BASE_PATH', __DIR__.'/..'); $framework = new Phast\Framework; $framework->getWebEntrypoint()->handle();
Console Application
Create console:
#!/usr/bin/env php <?php require __DIR__.'/vendor/autoload.php'; define('BASE_PATH', __DIR__); $framework = new Phast\Framework; exit($framework->getConsoleEntrypoint()->run());
chmod +x console
Configuration
Configuration files are in config/. The framework loads defaults from the package and merges your project's config/ overrides.
Application
// config/app.php return [ 'debug' => env('APP_DEBUG', false), 'controllers' => ['namespace' => 'App\\Controllers'], 'models' => ['namespace' => 'App\\Models'], 'jobs' => ['namespace' => 'App\\Jobs'], ];
Database
// config/database.php return [ 'driver' => env('DB_DRIVER', 'mysql'), 'migrations' => BASE_PATH.'/database/migrations', 'mysql' => [ 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', ''), 'username' => env('DB_USERNAME', 'root'), 'password' => env('DB_PASSWORD', ''), ], ];
Routing
// routes/web.php return function (Router $router) { $router->get('/', 'HomeController@index'); $router->get('/users/{id}', 'UserController@show'); $router->post('/users', 'UserController@store'); };
Middleware
Middleware is configured in config/middleware.php:
// config/middleware.php return [ // Core framework middleware (required) \Phast\Middleware\ErrorHandlerMiddleware::class, \Phast\Middleware\SessionMiddleware::class, // Add AuthMiddleware here if you want authentication // \Phast\Middleware\AuthMiddleware::class, // Add your custom middleware here (before routing) \App\Middleware\CustomMiddleware::class, \Phast\Middleware\RoutingMiddleware::class, \Phast\Middleware\DispatcherMiddleware::class, ];
Generate a new middleware:
php console g:middleware CustomMiddleware
Service Providers
Service providers are configured in config/providers.php:
// config/providers.php return [ // ConfigProvider must be first (other providers depend on it) \Phast\Providers\ConfigProvider::class, \Phast\Providers\CacheProvider::class, \Phast\Providers\DatabaseProvider::class, // ... other framework providers // Add your custom providers here \App\Providers\CustomProvider::class, ];
Generate a new service provider:
php console g:provider CustomProvider
Service providers implement Phast\Providers\ProviderInterface with two methods:
provide(Container $container): Register services in the containerinit(Container $container): Initialize services after all providers are registered
namespace App\Providers; use Katora\Container; use Phast\Providers\ProviderInterface; class CustomProvider implements ProviderInterface { public function provide(Container $container): void { // Register services here $container->set('custom.service', fn() => new CustomService); } public function init(Container $container): void { // Initialize services here (called after all providers are registered) $service = $container->get('custom.service'); $service->initialize(); } }
Controllers
namespace App\Controllers; use Phast\Controller; class HomeController extends Controller { public function index(ServerRequestInterface $request): ResponseInterface { return $this->render('welcome', ['message' => 'Hello, Phast!']); } }
Models
namespace App\Models; use Datum\Model; class User extends Model { protected static ?string $table = 'users'; } // Usage $user = User::find(1); $user = new User; $user->name = 'John'; $user->save();
Migrations
// Generated migration class CreateUsersTable implements MigrationInterface { public function up(ConnectionInterface $connection): void { $connection->execute("CREATE TABLE users (...)"); } public function down(ConnectionInterface $connection): void { $connection->execute("DROP TABLE users"); } }
Queue Jobs
namespace App\Jobs; use Qatar\Job; class SendEmailJob extends Job { public function handle(array $payload): void { // Job logic } public function retries(): int { return 3; } }
Console Commands
Generators
g:command- Generate console commandg:controller- Generate controllerg:event- Generate event classg:job- Generate jobg:middleware- Generate middleware classg:migration- Generate migrationg:model- Generate modelg:provider- Generate service provider
Database
m:up- Run pending migrationsm:down [count]- Rollback migrations (default: 1)
Development
serve- Start development serverworker- Run queue workershell- Start interactive PHP shell (REPL) with container accessuncache- Clear cached config, routes, and application cache
Usage Examples
Caching
$cache = $container->get(CacheInterface::class); $cache->set('key', 'value', 3600); $value = $cache->get('key');
Logging
$logger = $container->get(LoggerInterface::class); $logger->info('User logged in', ['user_id' => 123]);
Validation
use Filtr\Validator; $validator = new Validator; $validator->required('email')->email(); $validator->required('password')->min(8); $result = $validator->validate($data);
JWT Authentication
// config/auth.php return [ 'jwt' => [ 'secret' => env('JWT_SECRET'), 'algorithm' => 'HS256', ], 'middleware' => [ 'include' => ['/api/*'], 'exclude' => ['/api/auth/*'], 'required' => true, ], ];
Error Handling
The framework includes centralized error handling that:
- Catches all exceptions
- Returns JSON for API requests (
Accept: application/json) - Renders HTML error pages for web requests
- Logs 5xx errors automatically
- Shows debug info when
app.debugis enabled
License
MIT License - see LICENSE file.
Credits
Built on excellent PSR-compliant libraries:
- Katora - DI Container
- Kunfig - Configuration Management
- Databoss - Database Connections
- Datum - ORM
- Kram - Migrations
- Tez - Routing
- Vidyut - Middleware Pipeline
- Sandesh - HTTP Messages
- Phew - Templates
- Filtr - Validation
- Drishti - Logging
- Godam - Caching
- Qatar - Queues
- Jweety - JWT
- Envelope - Email
- Phlash - Flash Messages
- Ank - Captcha
- Soochak - Events
- Samay - Clock
- Dakiya - Session
- Clip - CLI Commands
- Prayog - REPL/Shell (dev)
统计信息
- 总下载量: 25
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 2
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-31