symlex / symlex-core
基于Symfony组件的最简内核和路由器
Requires
- php: >=7.3
- symfony/symfony: ^4.4
- symlex/di-microkernel: ^2.0
- twig/twig: ^2.5.0
Requires (Dev)
- codacy/coverage: dev-master
- lastzero/test-tools: ^5.0
This package is auto-updated.
Last update: 2024-09-05 12:00:42 UTC
README
注意:此存储库包含内核和路由器作为可重用组件。有关更多信息以及基于symlex-core的完整框架,请参阅https://github.com/symlex/symlex
如phpbenchmarks.com发布所示,Symlex比其他常见PHP框架为REST请求添加了显著较少的额外开销
我们的完整框架文档可以在docs.symlex.org找到。 Tuzi Liu为我们维护了中文翻译。
内核
轻量级的Symlex内核可以启动几乎任何应用程序。它基于我们的di-microkernel库。内核本身只需几行代码来设置环境参数,初始化Symfony服务容器,然后通过调用run()来启动应用程序。
位于config/目录下的YAML文件配置应用程序及其所有依赖项作为服务。文件名与应用程序的环境名称匹配(例如,config/console.yml)。配置还可以通过提供匹配的配置文件(例如,config/console.local.yml)来修改子环境,例如本地或生产环境(参见app.sub_environment参数)。这些文件采用您可能从Symfony中熟悉的相同良好文档的格式。
parameters: app.name: 'My App' app.version: '1.0' services: doctrine.migrations.migrate: class: Doctrine\DBAL\Migrations\Tools\Console\Command\MigrateCommand app: class: Symfony\Component\Console\Application arguments: [%app.name%, %app.version%] public: true calls: - [ add, [ "@doctrine.migrations.migrate" ] ]
这提供了一种统一的方法来启动Web应用程序,例如使用相同的内核的Symlex\Application\Web或命令行应用程序(如包装在Symlex\Application\Console中的Symfony\Component\Console\Application)。结果是比您从许多框架中熟悉的常规引导和配置混乱要干净得多。
禁用缓存
如果关闭调试模式,内核将服务容器配置缓存到设置的缓存路径目录中。在更新配置后,您必须删除所有缓存文件。要完全禁用缓存,请将container.cache: false添加到您的配置参数中。
parameters: container.cache: false
路由器
此库包含三个路由器类。它们配置Symfony路由组件以执行实际的路由,因此您可以期待相同的高性能。在将请求路由到适当的控制器操作后,路由器随后将响应渲染到控制器测试中(操作从不直接返回JSON或HTML)
Symlex\Router\Web\RestRouter处理REST请求(JSON)Symlex\Router\Web\ErrorRouter将异常渲染为错误消息(HTML或JSON)Symlex\Router\Web\TwigRouter通过Twig渲染常规Web页面(HTML)Symlex\Router\Web\TwigDefaultRouter类似于TwigRouter,但将所有请求发送到默认控制器操作(对于客户端路由(例如,与Vue.js一起)是必需的)
根据现有的示例,很容易创建自己的自定义路由/渲染。
应用程序的HTTP内核类初始化在服务容器中配置的路由器。
<?php namespace Symlex\Kernel; class WebApp extends App { protected $urlPrefix = ''; public function __construct($appPath, $debug = false) { parent::__construct('web', $appPath, $debug); } public function init() { if ($this->debug) { ini_set('display_errors', 1); } } public function getUrlPrefix($urlPrefixPostfix = ''): string { return $this->urlPrefix . $urlPrefixPostfix; } public function setUrlPrefix(string $urlPrefix) { $this->urlPrefix = $urlPrefix; } protected function setUp() { $container = $this->getContainer(); // The error router catches errors and displays them as error pages $container->get('router.error')->route(); // Routing for REST API calls $container->get('router.rest')->route($this->getUrlPrefix('/api'), 'controller.rest.'); // All other requests are routed to matching controller actions $container->get('router.twig')->route($this->getUrlPrefix(), 'controller.web.'); } }
REST和Twig路由器接受可选的URL前缀(例如/api)和服务名前缀(例如controller.rest.)。
默认HTTP内核(Symlex\Kernel\WebApp)的路由示例
GET /将路由到controller.web.index服务的indexAction(Request $request)POST /session/login将路由到controller.web.session服务的postLoginAction(Request $request)GET /api/users将路由到controller.rest.users服务的cgetAction(Request $request)POST /api/users将路由到controller.rest.users服务的postAction(Request $request)OPTIONS /api/users将路由到controller.rest.users服务的coptionsAction(Request $request)GET /api/users/123将路由到controller.rest.users服务的getAction($id, Request $request)OPTIONS /api/users/123将路由到controller.rest.users服务的optionsAction($id, Request $request)GET /api/users/123/comments将路由到controller.rest.users服务的cgetCommentsAction($id, Request $request)GET /api/users/123/comments/5将路由到controller.rest.users服务的getCommentsAction($id, $commendId, Request $request)PUT /api/users/123/comments/5将路由到controller.rest.users服务的putCommentsAction($id, $commendId, Request $request)
路由器将请求实例传递给每个匹配的控制器操作作为最后一个参数。它包含请求参数和头信息:[https://symfony.com.cn/doc/current/book/http_fundamentals.html#requests-and-responses-in-symfony](https://symfony.com.cn/doc/current/book/http_fundamentals.html#requests-and-responses-in-symfony)
由TwigRouter调用的控制器动作可以返回空(将渲染匹配的Twig模板),数组(Twig模板可以访问作为变量的值)或字符串(重定向URL)。
REST控制器动作(由RestRouter调用)始终返回数组,这些数组将被自动转换为有效的JSON。删除动作可以返回null(“204 No Content”)。
拦截器
HTTP拦截器可以在路由请求之前执行HTTP身份验证或其他操作(例如,阻止某些IP范围)。
<?php use Symlex\Kernel\App; class WebApp extends App { public function __construct($appPath, $debug = false) { parent::__construct('web', $appPath, $debug); } public function boot () { parent::boot(); $container = $this->getContainer(); /* * In app/config/web.yml: * * services: * http.interceptor: * class: Symlex\Router\HttpInterceptor */ $interceptor = $container->get('http.interceptor'); $interceptor->digestAuth('Realm', array('foouser' => 'somepassword')); $container->get('router.error')->route(); $container->get('router.rest')->route('/api', 'controller.rest.'); $container->get('router.twig')->route('', 'controller.web.'); } }
通过Symlex\Kernel\WebApps运行多个内核
注意:这是一个实验性的证明概念。欢迎反馈。
作为Symfony包的替代方案,Symlex\Kernel\WebApps能够在同一个Symlex安装上运行基于Symlex\Kernel\App的多个应用。
$app = new WebApps('web', __DIR__ . '/../app', false); $app->run();
它像常规WebApp一样启动,然后根据app/config/web.guests.yml中的配置(路径、调试、前缀和域名是可选的;启动和配置是必需的)启动其他Symlex应用。
example: prefix: /example domain: www.example.com bootstrap: \Symlex\Kernel\WebApp config: web.yml debug: true path: vendors/foo/bar/app default: bootstrap: \Symlex\Kernel\WebApp config: web.default.yml
注意:web/中的资源,如图片、CSS或JavaScript,不会像Assetic与Symfony包那样自动共享。如果您的应用不仅提供Web服务,您可能需要创建符号链接或修改您的HTML模板。
关于
Symlex由Michael Mayer维护,旨在通过提供一个促进最佳实践的示例系统来简化敏捷Web开发。Michael于2001年发布了他的第一个PHP框架,此前曾与主要框架供应商合作。如果没有其他开发者的很多前期工作,这是不可能实现的。感谢他们以及所有做出贡献的人!
如果您有任何问题,需要商业支持或只是想打个招呼,请随时发送电子邮件至hello@symlex.org。欢迎贡献,即使只是微小的拉取请求或错误报告。
捐赠
如果您喜欢这个项目,请留下星标,这足以保持动力。非常感谢!<3