Download the PHP package kajna/k-framework without Composer
On this page you can find all versions of the php package kajna/k-framework. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kajna/k-framework
More information about kajna/k-framework
Files in kajna/k-framework
Package k-framework
Short Description Simple and Lightweight yet powerful PHP framework
License MIT
Homepage https://github.com/ACTIV8-Developers/K
Informations about the package k-framework
K
Introduction
K 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
Getting started
Install
K requires PHP >=7.0 and Composer dependency manager to run.
So, before using K, you will need to make sure you have Composer installed on your machine
To install K using composer run following command:
Setup web server
Apache
K 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):
Additionally, make sure virtual host is configured with the AllowOverride option so that the .htaccess rewrite rules can be used:
AllowOverride All
Nginx
The nginx configuration file should contain at least this code in your location block:
try_files $uri $uri/ /index.php?$args;
This assumes that index.php is in the root folder of your project (www root).
Architecture Foundations
Application structure
Application 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:
- Config
- Controllers
- Hooks
- Middleware
- Models
- Views
Application lifecycle
The 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.
The 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.
K has middleware based architecture that means that each added middleware is executed before calling routing process which is itself middleware also.
After all middleware are executed and targeted route is found (or not), application generated response is displayed back to user.
The Basics
Routing
Basic routes
All 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:
Routes with parameters
Sometimes 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:
Captured ID will be appended to Request object get array
Available Router Methods
The router allows you to register routes that respond to any HTTP verb:
Middleware
What is middleware?
Middleware is a anything that is callable and accepts another callable as parameter (next middleware on stack):
Middleware 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.
How does middleware work?
Different 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.
When 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.
How to write middleware?
Closure middleware example
Invokable class middleware example
This example middleware is an invokable class that implements the magic __invoke() method.
How do I add middleware?
You 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.
Application middleware
Application (global) middleware will be executed on any request.
Route middleware
You can also attach middleware to any route and it will be invoked only when route is matched.
Route group middleware
Middleware can be attached to group of routes as well:
Hooks
What is a hook?
A "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.
Although middleware can be used in most cases, some specific events will require hooks, currently K supports hooks for following events:
- Route not found (not.found)
- Exception cached (internal.error)
- After application response is sent (after.execute)
Example for setting hook which will be invoked when exception is thrown.
Controllers
K'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.
Container access
K injects container object to every class route that extends ContainerAware class (which Controller class does). Content of container can be accesed in different ways:
Render/Buffer template:
Though 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.
Note that render method will return Response object
Controller example
Request
Request object is created in K boot process and can be obtained from application container as described in controllers section.
Examples of commonly used request methods:
Response
Every 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.
Examples of commonly used response methods:
Licence
The K Framework is released under the MIT public license.