swilen / framework
受 Laravel 和 Expressjs 启发的快速路由
dev-main / 1.x-dev
2022-11-28 03:47 UTC
Requires
- php: >=7.3
- ext-json: *
- ext-mbstring: *
- psr/container: ^2.0
- psr/http-message: ^1.0
- psr/log: ^1.1
Requires (Dev)
- mockery/mockery: ^1.5
- pestphp/pest: ^1.21
This package is auto-updated.
Last update: 2024-09-28 08:00:29 UTC
README
v0.7.2-alpha
一个受 Laravel 和 Expressjs 启发的快速、极简且灵活的 PHP 框架
创建项目
建议使用 Composer 创建 Swilen 项目。这将创建一个包含所有必需依赖项的 Swilen 项目。Swilen 需要 PHP 7.3 或更高版本。
$ composer create-project swilen/swilen:dev-main swilen
项目结构
Composer 生成当前项目结构。
📦swilen
┣ 📂app
┃ ┣ 📂Modules
┃ ┃ ┣ 📂Payments
┃ ┃ ┃ ┣ 📜PaymentController.php
┃ ┃ ┃ ┗ 📜PaymentService.php
┃ ┃ ┗ 📂Users
┃ ┃ ┃ ┣ 📜UserController.php
┃ ┃ ┃ ┗ 📜UserService.php
┃ ┣ 📂Shared
┃ ┃ ┣ 📂Http
┃ ┃ ┃ ┣ 📂Controller
┃ ┃ ┃ ┃ ┗ 📜Controller.php
┃ ┃ ┃ ┗ 📂Middleware
┃ ┃ ┃ ┃ ┗ 📜Authenticate.php
┃ ┃ ┗ 📂Providers
┃ ┃ ┃ ┣ 📜RouteServiceProvider.php
┃ ┃ ┃ ┗ 📜SecurityServiceProvider.php
┃ ┣ 📂storage
┃ ┃ ┣ 📂logs
┃ ┃ ┃ ┣ 📜.gitkeep
┃ ┃ ┃ ┗ 📜swilen-2022-10-06.log
┃ ┃ ┗ 📂public
┃ ┃ ┃ ┣ 📜.gitkeep
┃ ┃ ┃ ┗ 📜swilen.png
┃ ┣ 📜app.config.php
┃ ┣ 📜app.php
┃ ┗ 📜app.routes.php
┣ 📂public
┃ ┣ 📜.htaccess
┃ ┗ 📜index.php
┣ 📂vendor
┣ 📜.editorconfig
┣ 📜.env.example
┣ 📜.gitignore
┣ 📜composer.json
┣ 📜composer.lock
┣ 📜[README.me](#swilen-framework)
引导文件
📜swilen/public/index.php
. 这是应用的入口点。
<?php define('SWILEN_START', microtime(true)); /* |-------------------------------------------------------------------------- | Register The Auto Loader |-------------------------------------------------------------------------- */ require __DIR__.'/../vendor/autoload.php'; /* |-------------------------------------------------------------------------- | Require Swilen Application instance |-------------------------------------------------------------------------- */ $swilen = require_once __DIR__.'/../app/app.php'; /* |-------------------------------------------------------------------------- | Create new request instance from PHP superglobals |-------------------------------------------------------------------------- */ $request = Swilen\Http\Request::create(); /* |-------------------------------------------------------------------------- | Handle the incoming request and retrieve the response |-------------------------------------------------------------------------- */ $response = $swilen->handle($request); /* |-------------------------------------------------------------------------- | Terminate application response |-------------------------------------------------------------------------- */ $response->terminate();
📜swilen/app/app.php
. 应用定义。
<?php /* |-------------------------------------------------------------------------- | Create Swilen application instance |-------------------------------------------------------------------------- */ $app = new Swilen\Arthropod\Application( $_ENV['SWILEN_BASE_URL'] ?? dirname(__DIR__) ); /* |-------------------------------------------------------------------------- | Return application instance for use outher file |-------------------------------------------------------------------------- */ return $app;
📜swilen/app/app.routes.php
. 应用的 API 路由。
<?php use App\Shared\Http\Middleware\Authenticate; use Swilen\Petiole\Facades\Route; /* |-------------------------------------------------------------------------- | Swilen application routes |-------------------------------------------------------------------------- */ Route::prefix('users')->group(function () { Route::post('/sign-in', [App\Modules\Users\UserController::class, 'userSignIn']); Route::post('/sign-up', [App\Modules\Users\UserController::class, 'userSignUp']); }); Route::prefix('payments')->use(Authenticate::class)->group(function () { Route::get('/', [App\Modules\Payments\PaymentController::class, 'index']); Route::get('/{int:id}', [App\Modules\Payments\PaymentController::class, 'find']); Route::post('/', [App\Modules\Payments\PaymentController::class, 'store']); });
📜swilen/app/app.config.php
. 应用的配置。
<?php /* |-------------------------------------------------------------------------- | Swilen application config |-------------------------------------------------------------------------- */ return [ // ------------------------------------------------------------------- // | BASE APPLICATION CONFIG // ------------------------------------------------------------------- 'app' => [ // APPLICATION SECRET KEY 'secret' => env('APP_SECRET', ''), // APPLICATION ENVIRONMENT 'env' => env('APP_ENV', 'production'), // APPLICATION DEBUG 'debug' => env('APP_DEBUG', false), ], // ------------------------------------------------------------------ // | DATABASE CONNECTION CONFIG // ------------------------------------------------------------------ 'database' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'schema' => env('DB_SCHEMA', 'swilen'), 'username' => env('DB_USERNAME', ''), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'options' => extension_loaded('pdo_mysql') ? array_filter([ PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4 COLLATE utf8_unicode_ci;', ]) : [], ], // ------------------------------------------------------------------ // | CORS CONFIG 'coming soon' // ------------------------------------------------------------------ 'cors' => [ 'Allow-Origin' => '*', 'Allow-Headers' => 'X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method', 'Allow-Methods' => 'GET, POST, OPTIONS, PUT, DELETE', 'Allow-Credentials' => true, 'Max-Age' => 600 ], // ------------------------------------------------------------------ // | BASE APPLICATION PROVIDERS // ------------------------------------------------------------------ 'providers' => [ \Swilen\Security\SecurityServiceProvider::class, \Swilen\Database\DatabaseServiceProvider::class, \App\Shared\Providers\RouteServiceProvider::class ], ];
您可以使用内置的 PHP 服务器快速测试此项目
$ php -S localhost:8000 -t public