gravatalonga / king
个人项目的基础框架,可以处理任何类型的应用
1.0.13
2022-12-22 11:13 UTC
Requires
- php: ^8.1
- doctrine/migrations: ^3.5
- gravatalonga/king-foundation: >=1.0.15
- middlewares/whoops: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- phpunit/phpunit: ^9.5
README
Web 框架
安装
composer create-project gravatalonga/king project-folder
配置
- 将文件
.env.example
复制到.env
并进行配置。同时,您还需要检查配置文件夹。 - 运行
npm install
工作原理
服务提供者
服务提供者是将依赖或库绑定到应用程序的方式,您也可以对已绑定的进行任何类型的修改。
要创建服务提供者,需要实现 ServiceProvider
并实现两个方法。
工厂方法 用于创建新条目,必须返回一个键值对数组。
扩展方法 用于扩展已绑定的条目,与上面相同。
示例
<?php class Dumb implement ServiceProvider { public function factories(): array { return [ 'random' => function() { return rand(0, 10); }, 'math' => function($random) { return 1 + $random; }, 'other' => [self, 'getOtherFactory'] ]; } public function extensions() { return [ /** * @var $other is a previous entry */ 'other' => function (ContainerInterface $c, $other) { return $other + 1; } ]; } public function getOtherFactory(ContainerInterface $container) { return $container->has('random') ? $container->get('random') : null; } }
配置
每个配置文件夹中的文件都会被加载到容器中,文件名成为键条目,内容成为条目值。
路径
绑定到容器的路径有
path.config
=> 配置文件夹
path.public
=> 公共文件夹
path.resource
=> 资源文件夹
path.storage
=> 存储文件夹
path.domain
=> 域文件夹
path.base
=> 根文件夹
创建路由
$app = new App(); $app->get('/get', function(Request $request, Response $response) { $response->getBody()->write("Hello World"); return $response; }); $app->run();
但您也可以在服务提供者中创建路由
<?php class HelloServiceProvider implement ServiceProvider { public function factories(): array { return []; } public function extensions() { return [ RouteCollectorInterface::class => function (ContainerInterface $c, RouteCollectorInterface $route) { $route->get('/get', function(Request $request, Response $response) { $response->getBody()->write("Hello world"); return $response; }); return $route; } ]; } }
服务提供者
CommandBusServiceProvider
\League\Tactician\CommandBus
\League\Tactician\Container\ContainerLocator
DatabaseServiceProvider
\Doctrine\DBAL\Connection
database.connections
实例为\Gravatalonga\DriverManager\Manager
DotEnvServiceProvider
env
实例为\Dotenv\Dotenv
LogServiceProvider
logger.manager
实例为\Gravatalonga\DriverManager\Manager
\Psr\Log\LoggerInterface
SlimServiceProvider
\Psr\Http\Message\ResponseFactoryInterface
\Slim\Interfaces\CallableResolverInterface
\Slim\Interfaces\RouteCollectorInterface
TwigServiceProvider
twig.loader
实例为\Twig\Loader\FilesystemLoader
twig.options
是数组\Twig\Environment
迁移
- composer migrate
修复样式
- composer fix