{"id":14983969,"url":"https://github.com/symlex/symlex-core","last_synced_at":"2025-04-10T19:43:07.845Z","repository":{"id":20020657,"uuid":"23288478","full_name":"symlex/symlex-core","owner":"symlex","description":"Minimalistic Kernel and Router based on Symfony Components","archived":false,"fork":false,"pushed_at":"2022-07-05T07:24:18.000Z","size":132,"stargazers_count":10,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-24T17:21:23.382Z","etag":null,"topics":["dependency-injection","kernel","micro-framework","php","router","symfony"],"latest_commit_sha":null,"homepage":"https://docs.symlex.org","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/symlex.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-24T18:09:07.000Z","updated_at":"2025-01-27T14:08:12.000Z","dependencies_parsed_at":"2022-08-21T15:10:45.191Z","dependency_job_id":null,"html_url":"https://github.com/symlex/symlex-core","commit_stats":null,"previous_names":[],"tags_count":39,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/symlex%2Fsymlex-core","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/symlex%2Fsymlex-core/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/symlex%2Fsymlex-core/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/symlex%2Fsymlex-core/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/symlex","download_url":"https://codeload.github.com/symlex/symlex-core/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248281424,"owners_count":21077423,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dependency-injection","kernel","micro-framework","php","router","symfony"],"created_at":"2024-09-24T14:08:15.368Z","updated_at":"2025-04-10T19:43:07.828Z","avatar_url":"https://github.com/symlex.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Minimalistic Kernel and Routers based on Symfony Components\n===========================================================\n\n[![Latest Stable Version](https://poser.pugx.org/symlex/symlex-core/v/stable.svg)](https://packagist.org/packages/symlex/symlex-core)\n[![License](https://poser.pugx.org/symlex/symlex-core/license.svg)](https://packagist.org/packages/symlex/symlex-core)\n[![Test Coverage](https://codecov.io/gh/symlex/symlex-core/branch/master/graph/badge.svg)](https://codecov.io/gh/symlex/symlex-core)\n[![Build Status](https://travis-ci.org/symlex/symlex-core.png?branch=master)](https://travis-ci.org/symlex/symlex-core)\n[![Documentation](https://readthedocs.org/projects/symlex-docs/badge/?version=latest\u0026style=flat)](https://docs.symlex.org/en/latest/)\n[![Community Chat](https://badges.gitter.im/symlex/community.png)](https://gitter.im/symlex/community)\n\n*Note: This repository contains the kernel and routers as reusable components. For more information and a \ncomplete framework based on symlex-core please see https://github.com/symlex/symlex*\n\nAs published by [phpbenchmarks.com](http://www.phpbenchmarks.com/en/benchmark/apache-bench/php-7.3/symlex-4.1.html), \nSymlex adds [significantly less overhead](https://github.com/symlex/symlex/blob/master/README.md#performance) to REST \nrequests than other common PHP frameworks:\n\n\u003cimg src=\"https://symlex.org/images/performance-large.svg\" width=\"100%\"\u003e\n\nOur complete framework documentation can be found on [docs.symlex.org](https://docs.symlex.org/en/latest/).\n[Tuzi Liu](https://github.com/tuzimoe) maintains a [Chinese translation](https://docs.symlex.org/zh/latest/) for us.\n\n## Kernel ##\n\nThe light-weight Symlex kernel can bootstrap almost any application. It is based on our \n[di-microkernel](https://github.com/symlex/di-microkernel) library. The kernel itself is just a few lines \nto set environment parameters, initialize the Symfony service container and then start the app by calling `run()`.\n\nYAML files located in `config/` configure the application and all of it's dependencies as a service. The filename matches \nthe application's environment name (e.g. `config/console.yml`). The configuration can additionally be modified \nfor sub environments such as local or production by providing a matching config file like `config/console.local.yml`\n(see `app.sub_environment` parameter). These files are in the same [well documented](https://symfony.com/doc/current/components/dependency_injection.html) \nformat you might know from Symfony:\n\n```yaml\nparameters:\n    app.name: 'My App'\n    app.version: '1.0'\n\nservices:\n    doctrine.migrations.migrate:\n        class: Doctrine\\DBAL\\Migrations\\Tools\\Console\\Command\\MigrateCommand\n        \n    app:\n        class: Symfony\\Component\\Console\\Application\n        arguments: [%app.name%, %app.version%]\n        public: true\n        calls:\n            - [ add, [ \"@doctrine.migrations.migrate\" ] ]\n```\n\nThis provides a uniform approach for bootstrapping Web applications such as `Symlex\\Application\\Web` or command-line \napplications like `Symfony\\Component\\Console\\Application` (wrapped in `Symlex\\Application\\Console`) using the same kernel.\nThe result is much cleaner and leaner than the usual bootstrap and configuration madness you know from many frameworks.\n\n### Disable Caching ###\n\nIf debug mode is turned off, the service container configuration is cached by the kernel in the directory set as cache path. \nYou have to delete all cache files after updating the configuration. To disable caching completely, add \n`container.cache: false` to your config parameters:\n\n```yaml\nparameters:\n    container.cache: false\n```\n\n## Routers ##\n\nThere 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.\nAfter 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):\n\n- `Symlex\\Router\\Web\\RestRouter` handles REST requests (JSON)\n- `Symlex\\Router\\Web\\ErrorRouter` renders exceptions as error messages (HTML or JSON)\n- `Symlex\\Router\\Web\\TwigRouter` renders regular Web pages via Twig (HTML)\n- `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)\n\nIt's easy to create your own custom routing/rendering based on the existing examples.\n\nThe application's HTTP kernel class initializes the routers that were configured in the service container:\n```php\n\u003c?php\n\nnamespace Symlex\\Kernel;\n\nclass WebApp extends App\n{\n    protected $urlPrefix = '';\n\n    public function __construct($appPath, $debug = false)\n    {\n        parent::__construct('web', $appPath, $debug);\n    }\n\n    public function init()\n    {\n        if ($this-\u003edebug) {\n            ini_set('display_errors', 1);\n        }\n    }\n\n    public function getUrlPrefix($urlPrefixPostfix = ''): string\n    {\n        return $this-\u003eurlPrefix . $urlPrefixPostfix;\n    }\n\n    public function setUrlPrefix(string $urlPrefix)\n    {\n        $this-\u003eurlPrefix = $urlPrefix;\n    }\n\n    protected function setUp()\n    {\n        $container = $this-\u003egetContainer();\n\n        // The error router catches errors and displays them as error pages\n        $container-\u003eget('router.error')-\u003eroute();\n\n        // Routing for REST API calls\n        $container-\u003eget('router.rest')-\u003eroute($this-\u003egetUrlPrefix('/api'), 'controller.rest.');\n\n        // All other requests are routed to matching controller actions\n        $container-\u003eget('router.twig')-\u003eroute($this-\u003egetUrlPrefix(), 'controller.web.');\n    }\n}\n```\n\nThe REST and Twig routers accept optional URL (e.g. `/api`) and service name prefixes (e.g. `controller.rest.`).\n\nRouting examples for the default HTTP kernel (`Symlex\\Kernel\\WebApp`):\n- `GET /` will be routed to `controller.web.index` service's `indexAction(Request $request)`\n- `POST /session/login` will be routed to `controller.web.session` service's `postLoginAction(Request $request)`\n- `GET /api/users` will be routed to `controller.rest.users` service's `cgetAction(Request $request)`\n- `POST /api/users` will be routed to `controller.rest.users` service's `postAction(Request $request)`\n- `OPTIONS /api/users` will be routed to `controller.rest.users` service's `coptionsAction(Request $request)`\n- `GET /api/users/123` will be routed to `controller.rest.users` service's `getAction($id, Request $request)`\n- `OPTIONS /api/users/123` will be routed to `controller.rest.users` service's `optionsAction($id, Request $request)`\n- `GET /api/users/123/comments` will be routed to `controller.rest.users` service's `cgetCommentsAction($id, Request $request)`\n- `GET /api/users/123/comments/5` will be routed to `controller.rest.users` service's `getCommentsAction($id, $commendId, Request $request)`\n- `PUT /api/users/123/comments/5` will be routed to `controller.rest.users` service's `putCommentsAction($id, $commendId, Request $request)`\n\nThe 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\n\nController 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). \n\nREST controller actions (invoked by **RestRouter**) always return arrays, which are automatically converted to valid JSON. Delete actions can return null (\"204 No Content\").\n\n## Interceptors ##\n\nHTTP interceptors can be used to perform HTTP authentication or other actions (e.g. blocking certain IP ranges) **before** routing a request:\n\n```php\n\u003c?php\n\nuse Symlex\\Kernel\\App;\n\nclass WebApp extends App\n{\n    public function __construct($appPath, $debug = false)\n    {\n        parent::__construct('web', $appPath, $debug);\n    }\n\n    public function boot () {\n        parent::boot();\n\n        $container = $this-\u003egetContainer();\n\n        /*\n         * In app/config/web.yml:\n         *\n         * services:\n         *     http.interceptor:\n         *         class: Symlex\\Router\\HttpInterceptor\n         */\n        $interceptor = $container-\u003eget('http.interceptor');\n        $interceptor-\u003edigestAuth('Realm', array('foouser' =\u003e 'somepassword'));\n\n        $container-\u003eget('router.error')-\u003eroute();\n        $container-\u003eget('router.rest')-\u003eroute('/api', 'controller.rest.');\n        $container-\u003eget('router.twig')-\u003eroute('', 'controller.web.');\n    }\n}\n```\n\n## Run multiple kernels via `Symlex\\Kernel\\WebApps` ##\n\n*Note: This is an experimental proof-of-concept. Feedback welcome.*\n\nAs an alternative to Symfony bundles, `Symlex\\Kernel\\WebApps` is capable of running multiple apps based on `Symlex\\Kernel\\App` on the same Symlex installation:\n\n```php\n$app = new WebApps('web', __DIR__ . '/../app', false);\n$app-\u003erun();\n```\n\nIt'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):\n\n```yaml\nexample:\n    prefix: /example\n    domain: www.example.com\n    bootstrap: \\Symlex\\Kernel\\WebApp\n    config: web.yml\n    debug: true\n    path: vendors/foo/bar/app\n\ndefault:\n    bootstrap: \\Symlex\\Kernel\\WebApp\n    config: web.default.yml\n```\n\n*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.*\n\n## About ##\n\nSymlex is maintained by [Michael Mayer](https://blog.liquidbytes.net/about) and\naims to simplify agile Web development by providing a working system that promotes best practices by example.\nMichael released his [first PHP framework](http://freshmeat.sourceforge.net/projects/awf) in 2001 and \nhas previously worked with major framework vendors.\nBuilding this would not have been possible without a lot of prior work by other developers.\nThank you to those and everyone who contributed!\n\nFeel free to send an e-mail to [hello@symlex.org](mailto:hello@symlex.org) if you have any questions, \nneed [commercial support](https://blog.liquidbytes.net/contact/) or just want to say hello. \nContributions are welcome, even if it's just a tiny pull-request or bug report.\n\n## Donations ##\n\nPlease leave a star if you like this project, it provides enough motivation to keep going.\nThank you very much! \u003c3\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsymlex%2Fsymlex-core","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsymlex%2Fsymlex-core","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsymlex%2Fsymlex-core/lists"}