Skip to content

Repository files navigation

Koine Mvc

Another very simple MVC framework.

Code information:

Build StatusCoverage StatusCode ClimateScrutinizer Code Quality

Package information:

Latest Stable VersionTotal DownloadsLatest Unstable VersionLicenseDependency 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:

<?phpnamespaceMyApp;
useKoine\Mvc\Controller;
class HelloWorldController extends Controller
{
publicfunctionbeforeAction()
{
// do something, like check for logged user$this->getRequest()->getSession();
if (!$session['user_id']) {
thrownew \MyApp\AccessDeniedException("User is not logged");
}
}
publicfunctionsayHello()
{
$this->view->setLayout('layouts/application');
$this->render->('hello_world/say_hello', array(
'message' => 'Hello World!'
));
// or$this->getResponse()->setBody('Hello World!');
}
publicfunctionredirectToHome()
{
$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

namespaceMyAppTests;
useKoine\Test\Mvc\ControllerTestCase;
class HelloWordControllerTest extends ControllerTestCase
{
publicfunctionsetUp()
{
$this->setUpController('MyApp\\HelloWordController');
}
publicfunctiontestSayHelloWhenUserIsLoggedIn()
{
$session = array('user_id');
$params = array();
$this->getRequest('sayHello', $params, $session);
$this->assertResponseCode(200);
}
/** * @expectedException MyApp\AccessDenied */publicfunctiontestThrowsExceptionWhenUserIsNotLoggedIn()
{
$this->getRequest('sayHello', $params, $session);
}
protectedfunctiontestRedirectsToHome()
{
$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

About

Yet another mvc framework

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages