PHP code example of kajna / k-framework

1. Go to this page and download the library: Download kajna/k-framework library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kajna / k-framework example snippets


$route->get('contact', "ExampleController", "contactAction"); 
$route->post('contact', "ExampleController", "contactPostAction"); 

$route->get('article/:id', ArticleController::class, "getArticleAction"); 

$route->get($uri, $controller, $method);
$route->post($uri, $controller, $method);
$route->put($uri, $controller, $method);
$route->patch($uri, $controller, $method);
$route->delete($uri, $controller, $method);
$route->options($uri, $controller, $method);

function ($next) {
    // Do something

    return $next();
};

class ExampleMiddleware
{
    public function __invoke($next)
    {
        // Do something

        return $next();
    }
}

$app
->addMiddleware(new App\Middleware\SessionMiddleware())
->addMiddleware(new App\Middleware\RegistryMiddleware());

$route->get('foo', 'Foo', 'Bar')->addMiddleware(function($next) {});

$route->group(API_PREFIX, function($route) {
    $route->get('data/list', DataController::class, 'getList');
}, [
    new \App\Middleware\JSONParserMiddleware($container),
    new \App\Middleware\AuthMiddleware($container)
]);

$app->setHook('internal.error', (new App\Hooks\InternalErrorHook())->setContainer($container));

// Get via magic method
$request = $this->request;

// Get through array access operator on injected container object
$request = $this->container['request']; 

// Register class in container 
$this->container['classname'] = function() { 
     return new ClassName(); 
}; 

$response = $this->render('ViewName', $dataToBeSent);
$view = $this->buffer('ViewName', $dataToBeSent);

namespace App\Controllers;

use Core\Core\Controller;
use App\Models\ExampleModel;

/**
 * Example controller class.
 * @property ExampleModel $model
 */
class ExampleController extends Controller
{
    /**
     * Example method I.
     */
    public function indexAction()
    {
        // Get data from model class 
        // (when accesing undeclared object in controller container will be checked for specific key,
        // in this case we assume model is registered in container somewhere else)
        $data['content'] = $this->model->getData();

        // Render method will buffer view with passed data and write it to Response class for final output
        return $this->render('ExampleView', $data);
    }
    
    /**
     * Example method II.
     */
    public function testAction()
    {
        return (new Response())->setStatusCode(200)->setBody('<div>Hello World</div>');
    }
}

// Gets foo GET var. 
$foo = $request->get->get('foo'); 
// Gets POST var. 
$bar = $request->post->get('bar');  
// Get request method. 
$method = $request->getMethod(); 
// Retrieve a COOKIE value 
$request->cookies->get('PHPSESSID');
// Gets the URI. 
$uri = $request->getUri(); 
// Server variables. 
$host = $request->server->get('HTTP_HOST'); 
// File posted in a form. 
$file = $request->files->get('file'); 
// Get one of request headers. 
$type = $request->headers->get('CONTENT_TYPE'); 
// Cookies 
$id = $request->cookies->get('PHPSESSID'); 
// Check if it is AJAX request. 
$isAjax = $request->isAjax(); 
// Check if it is POST request.  
$isPost = $request->isPost();

$response->setHeader('Content-type', 'application/pdf'); 
$response->setCookie('lang', 'en'); 
$response->setProtocolVersion('HTTP/1.1'); 
$response->setStatusCode(200);
// Append to output body.
$response->writeBody('');
// Set output body.
$response->setBody('');