PHP code example of koine / mvc

1. Go to this page and download the library: Download koine/mvc 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/ */

    

koine / mvc example snippets



// index.php or something


// set request
$env     = new \Koine\Http\Environment($_SERVER);
$cookies = new \Koine\Http\Cookies($_COOKIE);
$session = new \Koine\Http\Session($_SESSION);
$params  = new \Koine\Http\Params($_REQUEST);

$request = new \Koine\Http\Request(array(
    'environment' => $env,
    'cookies'     => $cookies,
    'session'     => $session,
    'params'      => $params,
));

// set view
$view = new \Koine\Mvc\View();
$view->getConfig()->addPath(__DIR__ . '/views');

// set front controller
$frontController = new \Koine\Mvc\FrontController();
$frontController->setRequest($request)
    ->setController('MyApp\HelloWordController')
    ->setAction('sayHello')
    ->setView($view);

$response = $frontController->execute();
$response->send();

exit();



namespace MyApp;

use Koine\Mvc\Controller;

class HelloWorldController extends Controller
{
    public function beforeAction()
    {
        // do something, like check for logged user
        $this->getRequest()->getSession();

        if (!$session['user_id']) {
            throw new \MyApp\AccessDeniedException("User is not logged");
        }
    }

    public function sayHello()
    {
        $this->view->setLayout('layouts/application');

        $this->render->('hello_world/say_hello', array(
            'message' => 'Hello World!'
        ));

        // or

        $this->getResponse()->setBody('Hello World!');
    }

    public function redirectToHome()
    {
        $this->getResponse()->redirectTo('/');
    }
}


namespace MyAppTests;

use Koine\Test\Mvc\ControllerTestCase;

class HelloWordControllerTest extends ControllerTestCase
{
    public function setUp()
    {
        $this->setUpController('MyApp\\HelloWordController');
    }

    public function testSayHelloWhenUserIsLoggedIn()
    {
        $session = array('user_id');
        $params = array();

        $this->getRequest('sayHello', $params, $session);
        $this->assertResponseCode(200);
    }

    /**
     * @expectedException MyApp\AccessDenied
     */
    public function testThrowsExceptionWhenUserIsNotLoggedIn()
    {
        $this->getRequest('sayHello', $params, $session);
    }

    protected function testRedirectsToHome()
    {
        $this->getRequest('rediresctsToHome');

        $this->assertResponseIsRedirect();
        $this->assertResponseRedirectsTo('/');

        // or

        $this->assertTrue($this->getResponse()->isRedirect());
        $headers = $this->getResponse()->getHeaders();
        $this->assertEquals('Location: /', $headers['Location']);
    }
}

phtml
<!-- layouts/application.phtml -->
<h1>Some Layout!</h1>

<?= $this->render($this->view, $this->localVariables) 
$this->view
$this->localVariables