{"id":26806337,"url":"https://github.com/activ8-developers/k","last_synced_at":"2025-04-23T12:06:11.938Z","repository":{"id":16653717,"uuid":"19409166","full_name":"ACTIV8-Developers/K","owner":"ACTIV8-Developers","description":"Simple and lightweight yet powerfull PHP framework","archived":false,"fork":false,"pushed_at":"2020-07-01T17:30:37.000Z","size":1692,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-23T12:05:27.585Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"keon/awesome-nlp","license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ACTIV8-Developers.png","metadata":{"files":{"readme":"README.md","changelog":null,"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-05-03T18:19:29.000Z","updated_at":"2022-01-23T13:40:53.000Z","dependencies_parsed_at":"2022-07-26T06:16:17.537Z","dependency_job_id":null,"html_url":"https://github.com/ACTIV8-Developers/K","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ACTIV8-Developers%2FK","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ACTIV8-Developers%2FK/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ACTIV8-Developers%2FK/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ACTIV8-Developers%2FK/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ACTIV8-Developers","download_url":"https://codeload.github.com/ACTIV8-Developers/K/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250430584,"owners_count":21429324,"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":[],"created_at":"2025-03-29T23:31:08.069Z","updated_at":"2025-04-23T12:06:11.905Z","avatar_url":"https://github.com/ACTIV8-Developers.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"K\n=\n[![Version](https://img.shields.io/badge/version-3.5.0-blue.svg)](https://packagist.org/packages/kajna/k-framework)\n[![DUB](https://img.shields.io/dub/l/vibe-d.svg)](http://opensource.org/licenses/MIT)\n\n## Introduction\n\nK is simple mini framework, made with simplicity and performance in mind. This is template app repository, if you want to explore how internals work or contribute core files can be found [here](https://github.com/ACTIV8-Developers/K-Core)\n\n# Getting started\n\n### Install\n\nK requires PHP **\u003e=7.0** and [Composer](https://getcomposer.org/) dependency manager to run.\n\nSo, before using K, you will need to make sure you have Composer installed on your machine\n\nTo install K using composer run following command:\n```\ncomposer create-project kajna/k-framework projectname --prefer-dist\n```\n### Setup web server\n\n#### Apache\n\nK uses front end controller pattern so ensure the .htaccess and index.php files are in the same public-accessible directory. The .htaccess file should contain at least this code (K ships with example .htaccess file that can be used):\n\n```\nRewriteEngine On\nRewriteCond %{REQUEST_FILENAME} !-f\nRewriteRule ^(.*)$ /index.php [QSA,L]\n```\n\nAdditionally, make sure virtual host is configured with the AllowOverride option so that the .htaccess rewrite rules can be used:\n\nAllowOverride All\n\n#### Nginx\n\nThe nginx configuration file should contain at least this code in your location block:\n\n    try_files $uri $uri/ /index.php?$args;\n\nThis assumes that index.php is in the root folder of your project (www root).\n\n# Architecture Foundations\n\n## Application structure\n\nApplication consists of bootstrap index.php file and App folder. App folder will usually hold all user files as configuration, controllers, models, views, middleware, hooks etc. Althought not required App folder will come with predefined folder structure:\n\n*   Config\n*   Controllers\n*   Hooks\n*   Middleware\n*   Models\n*   Views\n\n## Application lifecycle\n\nThe entry point for all requests to a K application is the index.php file. All requests are directed to this file by your web server (Apache / Nginx) configuration. The index.php file doesn't contain much code. Rather, it is simply a starting point for loading the rest of the framework.\n\nThe index.php file loads the Composer generated auto loader definition, and then retrieves an instance of the K application **Core** class. When **Core** class is instantiated optionally midlewares and hooks are added and then execute method is called to start generating response.\n\nK has middleware based architecture that means that each added middleware is executed before calling routing process which is itself middleware also.\n\nAfter all middleware are executed and targeted route is found (or not), application generated response is displayed back to user.\n\n# The Basics\n\n## Routing\n\n#### Basic routes\n\nAll routes are declared in file App/routes.php file. The most basic K route simply accepts a URI and two strings representing name of class and name of class method to execute:\n```php\n$route-\u003eget('contact', \"ExampleController\", \"contactAction\"); \n$route-\u003epost('contact', \"ExampleController\", \"contactPostAction\"); \n```\n\n#### Routes with parameters\n\nSometimes you will need to capture segments of the URI within your route. For example, when article ID is passed in the URL you may capture it by defining route parameters:\n```php\n$route-\u003eget('article/:id', ArticleController::class, \"getArticleAction\"); \n```\n\nCaptured ID will be appended to **Request** object get array\n\n#### Available Router Methods\n\nThe router allows you to register routes that respond to any HTTP verb:\n```php\n$route-\u003eget($uri, $controller, $method);\n$route-\u003epost($uri, $controller, $method);\n$route-\u003eput($uri, $controller, $method);\n$route-\u003epatch($uri, $controller, $method);\n$route-\u003edelete($uri, $controller, $method);\n$route-\u003eoptions($uri, $controller, $method);\n```\n\n## Middleware\n\n#### What is middleware?\n\nMiddleware is a anything that is callable and accepts another callable as parameter (next middleware on stack):\n\nMiddleware can do any task like start session, filter request, connect to database, etc. The only hard requirement is that a middleware MUST return an instance of **Response**. Each middleware SHOULD invoke the next middleware.\n\n#### How does middleware work?\n\nDifferent frameworks use middleware differently. K implements middleware as stack. Each new middleware will be put on top of existing middleware. The structure expands as additional middleware layers are added. The last middleware layer added is the first to be executed. By default application core routing process will be automatically added as middleware.\n\nWhen K application is executed middleware stack is popped and first element is invoked which will either return **Response** or invoke next middleware and so on. When middleware stack is executed application will render returned response and will display it back to user.\n\n#### How to write middleware?\n\n##### Closure middleware example\n```php\nfunction ($next) {\n    // Do something\n\n    return $next();\n};\n```\n\n##### Invokable class middleware example\n\nThis example middleware is an invokable class that implements the magic __invoke() method.\n```php\nclass ExampleMiddleware\n{\n    public function __invoke($next)\n    {\n        // Do something\n\n        return $next();\n    }\n}\n```\n\n#### How do I add middleware?\n\nYou may add middleware to a K application or to an individual K application route. All scenarios accept the same middleware and implement the same middleware interface.\n\n#### Application middleware\n\nApplication (global) middleware will be executed on any request.\n```php\n$app\n-\u003eaddMiddleware(new App\\Middleware\\SessionMiddleware())\n-\u003eaddMiddleware(new App\\Middleware\\RegistryMiddleware());\n```\n\n#### Route middleware\n\nYou can also attach middleware to any route and it will be invoked only when route is matched.\n```php\n$route-\u003eget('foo', 'Foo', 'Bar')-\u003eaddMiddleware(function($next) {});\n```\n\n#### Route group middleware\nMiddleware can be attached to group of routes as well:\n```php\n$route-\u003egroup(API_PREFIX, function($route) {\n    $route-\u003eget('data/list', DataController::class, 'getList');\n}, [\n    new \\App\\Middleware\\JSONParserMiddleware($container),\n    new \\App\\Middleware\\AuthMiddleware($container)\n]);\n```\n\n## Hooks\n\n#### What is a hook?\n\nA \"hook\" is a moment in the K application lifecycle at which a callable assigned to the hook will be invoked if present. A hook is identified by a string name.\n\nAlthough middleware can be used in most cases, some specific events will require hooks, currently K supports hooks for following events:\n\n*   Route not found (not.found)\n*   Exception cached (internal.error)\n*   After application response is sent (after.execute)\n\nExample for setting hook which will be invoked when exception is thrown.\n```php\n$app-\u003esetHook('internal.error', (new App\\Hooks\\InternalErrorHook())-\u003esetContainer($container));\n```\n\n## Controllers\n\nK's Controller class provides many useful methods for accessing container objects and extending route target classes by Controller class is preferred way of doing things.\n\n#### Container access\n\nK injects container object to every class route that extends ContainerAware class (which Controller class does). Content of container can be accesed in different ways:\n```php\n// Get via magic method\n$request = $this-\u003erequest;\n\n// Get through array access operator on injected container object\n$request = $this-\u003econtainer['request']; \n\n// Register class in container \n$this-\u003econtainer['classname'] = function() { \n     return new ClassName(); \n}; \n```\n\n#### Render/Buffer template:\n\nThough not a requirement, most controllers will ult imately render a template that's responsible for generating the HTML (or other format). Templates are usually stored in App/Views folder.\n```php\n$response = $this-\u003erender('ViewName', $dataToBeSent);\n$view = $this-\u003ebuffer('ViewName', $dataToBeSent);\n```\n\nNote that render method will return **Response** object\n\n#### Controller example\n```php\nnamespace App\\Controllers;\n\nuse Core\\Core\\Controller;\nuse App\\Models\\ExampleModel;\n\n/**\n * Example controller class.\n * @property ExampleModel $model\n */\nclass ExampleController extends Controller\n{\n    /**\n     * Example method I.\n     */\n    public function indexAction()\n    {\n        // Get data from model class \n        // (when accesing undeclared object in controller container will be checked for specific key,\n        // in this case we assume model is registered in container somewhere else)\n        $data['content'] = $this-\u003emodel-\u003egetData();\n\n        // Render method will buffer view with passed data and write it to Response class for final output\n        return $this-\u003erender('ExampleView', $data);\n    }\n    \n    /**\n     * Example method II.\n     */\n    public function testAction()\n    {\n        return (new Response())-\u003esetStatusCode(200)-\u003esetBody('\u003cdiv\u003eHello World\u003c/div\u003e');\n    }\n}\n```\n\n## Request\n\n**Request** object is created in K boot process and can be obtained from application container as described in controllers section.\n\nExamples of commonly used request methods:\n```php\n// Gets foo GET var. \n$foo = $request-\u003eget-\u003eget('foo'); \n// Gets POST var. \n$bar = $request-\u003epost-\u003eget('bar');  \n// Get request method. \n$method = $request-\u003egetMethod(); \n// Retrieve a COOKIE value \n$request-\u003ecookies-\u003eget('PHPSESSID');\n// Gets the URI. \n$uri = $request-\u003egetUri(); \n// Server variables. \n$host = $request-\u003eserver-\u003eget('HTTP_HOST'); \n// File posted in a form. \n$file = $request-\u003efiles-\u003eget('file'); \n// Get one of request headers. \n$type = $request-\u003eheaders-\u003eget('CONTENT_TYPE'); \n// Cookies \n$id = $request-\u003ecookies-\u003eget('PHPSESSID'); \n// Check if it is AJAX request. \n$isAjax = $request-\u003eisAjax(); \n// Check if it is POST request.  \n$isPost = $request-\u003eisPost();\n```\n## Response\n\nEvery middleware or route callback is expected to return **Response** object, by default K will not create any response and it expected that response is created during application runtime and passed back.\n\nExamples of commonly used response methods:\n\n```php\n$response-\u003esetHeader('Content-type', 'application/pdf'); \n$response-\u003esetCookie('lang', 'en'); \n$response-\u003esetProtocolVersion('HTTP/1.1'); \n$response-\u003esetStatusCode(200);\n// Append to output body.\n$response-\u003ewriteBody('');\n// Set output body.\n$response-\u003esetBody(''); \n```\n\n### Licence\nThe K Framework is released under the [MIT](http://opensource.org/licenses/MIT) public license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factiv8-developers%2Fk","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Factiv8-developers%2Fk","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factiv8-developers%2Fk/lists"}