swiftphp.ma/installer
最新稳定版本:v1.0.0
Composer 安装命令:
composer require swiftphp.ma/installer
包简介
Modern PHP framework with AI-powered CLI, built-in auth, multi-tenant support, and 70% less code
关键字:
README 文档
README
Modern PHP framework with an expressive router, a simple ORM, built-in authentication, multi-tenant support, middleware, validation, and a lightweight view system. Ships with an AI-friendly CLI to generate controllers, models, and migrations fast — aiming for 70% less boilerplate.
SwiftPHP targets PHP 8+ and PSR-12 coding style. It’s ideal for small to medium apps, dashboards, APIs, and admin tools.
Repository: https://github.com/moaminemahnoudi/swiftphp
Features
- Routing: Simple, declarative routes with controller binding
- Controllers: Base controller with dependency injection via attributes
- ORM & Query Builder: Lightweight
Model+ModelQuerywithDatabaseandQueryBuilder - Migrations: Built-in migrator and generator via CLI
- Authentication: Session-based auth helpers and middleware
- Multi-Tenant: Tenant resolution + role-based access via traits
- Middleware: Auth, CORS, Rate Limiting, Roles, Tenant
- Validation: Declarative validation with
Validatorand exceptions - Views: PHP templates (
.swift.php) with layout and components - CLI:
swiftphpto generate, migrate, serve, and scaffold resources - Error Handling: Friendly error template and handler
- Security: Helpers to harden common web risks
Requirements
- PHP 8.1+
- Composer
- PDO extension for your database (e.g., MySQL, SQLite)
Installation
You can install globally (installer) or scaffold directly from source.
Packagist (recommended)
Once published:
composer global require swiftphp.ma/installer composer create-project swiftphp.ma/installer my-app
From Source (this repository)
# Install dependencies composer install # Ensure storage directories exist mkdir storage\views # Start PHP built-in server php -S localhost:8000 -t public
Open http://localhost:8000 in your browser.
Quick Start
- Configure app and database:
config/app.phpconfig/database.php
- Run migrations:
# Windows PowerShell
php bin\swiftphp migrate
- Start the server:
php -S localhost:8000 -t public
- Visit
http://localhost:8000— defaulthome.swift.phpview renders.
Documentation
Project Structure
app/
Controllers/ # Application controllers
Models/ # Domain models (extends src/Core/Model)
config/ # App + DB config
database/migrations/ # Migration files
public/ # Front controller (index.php)
resources/views/ # Views (layouts, components, pages)
src/ # Framework source (core, router, db, console, etc.)
storage/views/ # Compiled/cached views
Key framework modules:
src/Core: Application, Container, Controller, Model, Routersrc/Http: Request, Responsesrc/Database: Database, QueryBuildersrc/Auth: Auth helpers, Tenantsrc/Middleware: Middleware base and built-inssrc/Validation: Validator, ValidationExceptionsrc/View: View enginesrc/Console: CLI application + commands
Routing
Define routes in public/index.php using the Router.
Example:
use Src\Core\Application; use Src\Core\Router; use App\Controllers\UserController; $app = new Application(); $router = new Router($app); $router->get('/', [UserController::class, 'home']); $router->get('/users', [UserController::class, 'index']); $router->get('/users/{id}', [UserController::class, 'show']); $router->post('/users', [UserController::class, 'store']); $router->dispatch();
Route parameters like {id} are available via Request.
Controllers
Controllers live in app/Controllers and extend Src\Core\Controller.
namespace App\Controllers; use Src\Core\Controller; use Src\Http\Request; use App\Models\User; class UserController extends Controller { public function index(Request $request) { $users = User::query()->get(); return $this->view('users/index', compact('users')); } }
Dependency injection is supported via the container and Attributes\Inject.
Models & Querying
Models extend Src\Core\Model. A simple User model:
namespace App\Models; use Src\Core\Model; class User extends Model { protected string $table = 'users'; protected array $fillable = ['name', 'email']; }
Query examples:
$all = User::query()->get(); $one = User::query()->where('id', 1)->first(); User::query()->insert(['name' => 'Ada', 'email' => 'ada@example.com']); User::query()->update(1, ['name' => 'Ada Lovelace']); User::query()->delete(1);
Migrations
Migration files live in database/migrations. Use the CLI to create and run them.
# Generate migration php bin\swiftphp make:migration create_posts_table # Run migrations php bin\swiftphp migrate
Example migration file name: 2024_01_01_000000_create_users_table.php.
Views
SwiftPHP uses plain PHP templates with a .swift.php suffix.
- Layouts:
resources/views/layouts/app.swift.php - Components:
resources/views/components/*.swift.php - Pages:
resources/views/*.swift.php
Render from a controller:
return $this->view('users/index', ['users' => $users]);
The engine compiles views to storage/views for performance.
Authentication & Authorization
src/Auth/Auth.php: login, logout, current user helperssrc/Middleware/AuthMiddleware.php: gate routes to authenticated userssrc/Traits/HasRoles.php: role management on modelssrc/Auth/Tenant.php+src/Traits/HasTenant.php: multi-tenant helpers
Apply middleware on routes or in controller dispatch.
Middleware
Built-ins:
AuthMiddlewareCorsMiddlewareRateLimitMiddlewareRoleMiddlewareTenantMiddleware
Register and apply to routes via the router or a pipeline.
Validation
Use Src\Validation\Validator to validate request data.
use Src\Validation\Validator; Validator::make($request->all(), [ 'email' => ['required', 'email'], 'name' => ['required', 'min:2'] ])->validate();
On failure, throws ValidationException which the error handler renders.
CLI Commands
Run via php bin\\swiftphp (or bin\\swiftphp.bat on Windows):
new: Scaffold a new projectserve: Start a dev servermake:controller: Generate a controllermake:model: Generate a modelmake:migration: Generate a migrationmigrate: Run migrationshelp: Show available commands
Examples:
php bin\swiftphp serve php bin\swiftphp make:controller UserController php bin\swiftphp make:model User php bin\swiftphp make:migration add_auth_fields_to_users_table php bin\swiftphp migrate
Configuration
config/app.php: app name, env, debug, timezone, etc.config/database.php: driver, host, database, user, password
Use environment variables via src/Support/Env.php.
Error Handling & Security
- Error pages via
src/Error/error_template.php - Central
ErrorHandlerto catch and render src/Security/Security.phpprovides helpers for sanitization and headers
Publishing
See PUBLISHING.md for full Packagist steps:
- Tag releases (e.g.,
v1.0.0) - Submit GitHub repo to Packagist
- Optionally configure auto-update hooks
Contributing
Contributions are welcome! Please read CONTRIBUTING.md for guidelines.
- Follow PSR-12
- Add tests when possible
- Update
CHANGELOG.mdfor user-facing changes
Security issues? See SECURITY.md for our policy.
License
MIT. See LICENSE.
FAQ
- Where do I define routes?
- In
public/index.phpusingRouter.
- In
- How do I enable auth-protected pages?
- Register
AuthMiddlewareon routes or controllers.
- Register
- Does it support JSON APIs?
- Yes. Use
Responseto return JSON and skip views.
- Yes. Use
- Can I use SQLite?
- Yes. Configure
config/database.phpfor SQLite with PDO.
- Yes. Configure
Credits
Built with care by the SwiftPHP community.
统计信息
- 总下载量: 7
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-02