koine / mvc
Another MVC framework
1.0
2015-08-15 23:43 UTC
Requires
- php: >=5.4
- koine/http: ~0.9.3
- koine/view: ~0.9.5
- nurse/di: 0.9.*
Requires (Dev)
- phpunit/phpunit: *
- satooshi/php-coveralls: dev-master
README
Another very simple MVC framework.
Code information:
Package information:
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
- Learn composer. You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow this set of instructions
Issues/Features proposals
Here is the issue tracker.
Contributing
Only TDD code will be accepted. Please follow the PSR-2 code standard.
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - 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