symlex / symlex-core
Minimalistic Kernel and Routers based on Symfony Components
Installs: 3 334
Dependents: 5
Suggesters: 0
Security: 0
Stars: 11
Watchers: 3
Forks: 3
Open Issues: 0
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-10-05 12:14:59 UTC
README
Note: This repository contains the kernel and routers as reusable components. For more information and a complete framework based on symlex-core please see https://github.com/symlex/symlex
As published by phpbenchmarks.com, Symlex adds significantly less overhead to REST requests than other common PHP frameworks:
Our complete framework documentation can be found on docs.symlex.org. Tuzi Liu maintains a Chinese translation for us.
Kernel
The light-weight Symlex kernel can bootstrap almost any application. It is based on our
di-microkernel library. The kernel itself is just a few lines
to set environment parameters, initialize the Symfony service container and then start the app by calling run()
.
YAML files located in config/
configure the application and all of it's dependencies as a service. The filename matches
the application's environment name (e.g. config/console.yml
). The configuration can additionally be modified
for sub environments such as local or production by providing a matching config file like config/console.local.yml
(see app.sub_environment
parameter). These files are in the same well documented
format you might know from 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" ] ]
This provides a uniform approach for bootstrapping Web applications such as Symlex\Application\Web
or command-line
applications like Symfony\Component\Console\Application
(wrapped in Symlex\Application\Console
) using the same kernel.
The result is much cleaner and leaner than the usual bootstrap and configuration madness you know from many frameworks.
Disable Caching
If debug mode is turned off, the service container configuration is cached by the kernel in the directory set as cache path.
You have to delete all cache files after updating the configuration. To disable caching completely, add
container.cache: false
to your config parameters:
parameters: container.cache: false
Routers
There are three router classes included in this library. They configure the Symfony router component to perform the actual routing, so you can expect the same high performance. After routing a request to the appropriate controller action, the router subsequently renders the response to ease controller testing (actions never directly return JSON or HTML):
Symlex\Router\Web\RestRouter
handles REST requests (JSON)Symlex\Router\Web\ErrorRouter
renders exceptions as error messages (HTML or JSON)Symlex\Router\Web\TwigRouter
renders regular Web pages via Twig (HTML)Symlex\Router\Web\TwigDefaultRouter
is like TwigRouter but sends all requests to a default controller action (required for client-side routing e.g. with Vue.js)
It's easy to create your own custom routing/rendering based on the existing examples.
The application's HTTP kernel class initializes the routers that were configured in the service container:
<?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.'); } }
The REST and Twig routers accept optional URL (e.g. /api
) and service name prefixes (e.g. controller.rest.
).
Routing examples for the default HTTP kernel (Symlex\Kernel\WebApp
):
GET /
will be routed tocontroller.web.index
service'sindexAction(Request $request)
POST /session/login
will be routed tocontroller.web.session
service'spostLoginAction(Request $request)
GET /api/users
will be routed tocontroller.rest.users
service'scgetAction(Request $request)
POST /api/users
will be routed tocontroller.rest.users
service'spostAction(Request $request)
OPTIONS /api/users
will be routed tocontroller.rest.users
service'scoptionsAction(Request $request)
GET /api/users/123
will be routed tocontroller.rest.users
service'sgetAction($id, Request $request)
OPTIONS /api/users/123
will be routed tocontroller.rest.users
service'soptionsAction($id, Request $request)
GET /api/users/123/comments
will be routed tocontroller.rest.users
service'scgetCommentsAction($id, Request $request)
GET /api/users/123/comments/5
will be routed tocontroller.rest.users
service'sgetCommentsAction($id, $commendId, Request $request)
PUT /api/users/123/comments/5
will be routed tocontroller.rest.users
service'sputCommentsAction($id, $commendId, Request $request)
The routers pass on the request instance to each matched controller action as last argument. It contains request parameters and headers: http://symfony.com/doc/current/book/http_fundamentals.html#requests-and-responses-in-symfony
Controller actions invoked by TwigRouter can either return nothing (the matching Twig template will be rendered), an array (the Twig template can access the values as variables) or a string (redirect URL).
REST controller actions (invoked by RestRouter) always return arrays, which are automatically converted to valid JSON. Delete actions can return null ("204 No Content").
Interceptors
HTTP interceptors can be used to perform HTTP authentication or other actions (e.g. blocking certain IP ranges) before routing a request:
<?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.'); } }
Run multiple kernels via Symlex\Kernel\WebApps
Note: This is an experimental proof-of-concept. Feedback welcome.
As an alternative to Symfony bundles, Symlex\Kernel\WebApps
is capable of running multiple apps based on Symlex\Kernel\App
on the same Symlex installation:
$app = new WebApps('web', __DIR__ . '/../app', false); $app->run();
It's bootstrapped like a regular WebApp and subsequently bootstaps other Symlex apps according to the configuration in app/config/web.guests.yml
(path, debug, prefix and domain are optional; bootstrap and config are required):
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
Note: Assets in web/ like images, CSS or JavaScript in are not automatically shared in a way Assetic does this with Symfony bundles. If your apps not only provide Web services, you might have to create symbolic links or modify your HTML templates.
About
Symlex is maintained by Michael Mayer and aims to simplify agile Web development by providing a working system that promotes best practices by example. Michael released his first PHP framework in 2001 and has previously worked with major framework vendors. Building this would not have been possible without a lot of prior work by other developers. Thank you to those and everyone who contributed!
Feel free to send an e-mail to hello@symlex.org if you have any questions, need commercial support or just want to say hello. Contributions are welcome, even if it's just a tiny pull-request or bug report.
Donations
Please leave a star if you like this project, it provides enough motivation to keep going. Thank you very much! <3