dev-master
9999999-devHouse PHP — A minimal MVC Application Toolset
MIT
The Requires
- php >=5.4.0
- mthaml/mthaml ~1.7
by Wes Roberts
House PHP — A minimal MVC Application Toolset
A super-minimal pure PHP MVC toolkit., (*1)
The controller layer is the interface for your application.
Use it to define a Route
, accept a Request
, and return a Response
., (*2)
We'll start with a Hello world route in app.php
., (*3)
$app = new House\Router; $app->get('/', function($req, $resp){ return 'Hello World'; });
The router is powerful in its simplicity., (*4)
Try this simplified expression route that says hello to an arbitrary name., (*5)
$app->get('/hello/:name', function($req){ return 'Hello ' . $req->param('name'); });
You can even nest routes based on criteria:, (*6)
$app->group('/user', function($app){ $app->put(function(){ // Create user }) ->get('/:id', function(){ // Retrieve user }) ->post('/:id', function(){ // Update user }) ->delete('/:id', function(){ // Delete user }); });
You can attach middleware to routes with before()
and after()
., (*7)
$app->before('*', function($req, $resp){ House\Log::info('Request: ' . $req); $resp->code(200); });
You can optionally catch errors per route as well., (*8)
$app->error('*', function($req, $resp){ House\Log::error($req->exception->getMessage()); return 500; });
Notice that controller return values are passed to $response->write()
., (*9)
See example.php
for more examples of exactly how badass Router
is., (*10)
The model layer is very basic. There is no ORM. If you need a more robust model layer, please see Symfony or php-activerecord. Otherwise, check this:, (*11)
class User extends House\Model {} // throws House\NotFound User::find(['id' => 1]); // returns array() $users = User::where(['status' => ['active', 'inactive']])->limit(5)->all();
The view layer is almost vanishingly small. It's so simple, it doesn't even need to exist.
Returning a string from a controller method automatically calls $response->write($string)
., (*12)
So, view helpers are any method that return a string. We've bundled a few to get you started, but House could potentially support any template engine, flat file, etc. Since House is not a framework, there is no special View registry or folder, so you can easily install alternative template engines with composer and use them as-is., (*13)
Quickly configure your app to use Haml for example:, (*14)
House\Haml::config([ 'cache' => sys_get_temp_dir() . '/haml', 'views' => __DIR__ . '/views', ]); function haml($view, $vars = array(), $config = array()) { return new House\Haml($view, $vars, $config); }
Then implement it in your application with ease:, (*15)
$app->get('/', function($req){ return haml('index', $req->params()); });
House PHP — A minimal MVC Application Toolset
MIT