koine/mvc

Another MVC framework

1.0 2015-08-15 23:43 UTC

This package is not auto-updated.

Last update: 2024-11-09 16:58:38 UTC


README

Another very simple MVC framework.

Code information:

Build Status Coverage Status Code Climate Scrutinizer Code Quality

Package information:

Latest Stable Version Total Downloads Latest Unstable Version License Dependency Status

Skeleton app

For a ready to use app clone the skeleton app or follow its composer instalation guide.

Usage

<?php
// 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();

The controller:

<?php

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('/');
    }
}

The layout:

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

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

Note that in order to render the view in the layout you MUST pass the $this->view and the $this->localVariables variable in order to make them available in the view

The view;

<!-- hello_word.phtml -->
<p><?= $message ?></p>

Testing

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']);
    }
}

Installing

Via Composer

Append the lib to your requirements key in your composer.json.

{
    // composer.json
    // [..]
    require: {
        // append this line to your requirements
        "koine/mvc": "~0.9.7"
    }
}

Alternative install

Issues/Features proposals

Here is the issue tracker.

Contributing

Only TDD code will be accepted. Please follow the PSR-2 code standard.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

How to run the tests:

phpunit --configuration tests/phpunit.xml

To check the code standard run:

phpcs --standard=PSR2 lib
phpcs --standard=PSR2 tests

Lincense

MIT

Authors