Controller extensions for the Slim Framework, used by help.fortrabbit.com and blog.fortrabbit.com
Controller extensions for the Slim Framework, used by help.fortrabbit.com and blog.fortrabbit.com
SlimController is an extension for the Slim Framework providing the C of MVC.
With Slim alone, you can create great applications very, very quickly. Sometimes things get out of hand an you just need a bit more structure - or at least I do. That's what SlimController is for.
Create a composer.json file
{
"require": {
"slimcontroller/slimcontroller": "0.4.3"
},
"autoload": {
"psr-0": {
"MyApp": "src/"
}
}
}Run installation
composer.phar install --devIf you know how Slim works, using SlimController shouldn't be a big deal.
Setup a structure for your controller and templates (just a suggestion, do as you like):
mkdir -p src/MyApp/Controller templates/homeCreate your first controller in src/MyApp/Controller/Home.php
render('home/index', array(
'someVar' => date('c')
));
}
public function helloAction($name)
{
$this->render('home/hello', array(
'name' => $name
));
}
}Here are the two corresponding demo templates:
templates/home/index.php
This is the SlimController extension @ templates/home/hello.php
Hello Minimal bootstrap file for this example
APP_PATH . '/templates',
'controller.class_prefix' => '\\MyApp\\Controller',
'controller.method_suffix' => 'Action',
'controller.template_suffix' => 'php',
));
$app->addRoutes(array(
'/' => 'Home:index',
'/hello/:name' => 'Home:hello',
));
$app->run();php -S localhost:8080Optional class prefix for controller classes. Will be prepended to routes.
Using \\MyApp\\Controller as prefix with given routes:
$app->addRoutes(array(
'/' => 'Home:index',
'/hello/:name' => 'Home:hello',
));Translates to
$app->addRoutes(array(
'/' => '\\MyApp\\Controller\\Home:index',
'/hello/:name' => '\\MyApp\\Controller\\Home:hello',
));Optional class suffix for controller classes. Will be appended to routes.
Using Controller as suffix with given routes:
$app->addRoutes(array(
'/' => 'Home:index',
'/hello/:name' => 'Home:hello',
));Translates to
$app->addRoutes(array(
'/' => 'HomeController:index',
'/hello/:name' => 'HomeController:hello',
));Optional method suffix. Appended to routes.
Using Action as suffix with given routes:
$app->addRoutes(array(
'/' => 'Home:index',
'/hello/:name' => 'Home:hello',
));Translates to
$app->addRoutes(array(
'/' => 'Home:indexAction',
'/hello/:name' => 'Home:helloAction',
));Defaults to twig. Will be appended to template name given in render() method.
// how to integrate the Slim middleware
$app->addRoutes(array(
'/' => array('Home:index', function() {
error_log("MIDDLEWARE FOR SINGLE ROUTE");
},
function() {
error_log("ADDITIONAL MIDDLEWARE FOR SINGLE ROUTE");
}
),
'/hello/:name' => array('post' => array('Home:hello', function() {
error_log("THIS ROUTE IS ONLY POST");
}
))
), function() {
error_log("APPENDED MIDDLEWARE FOR ALL ROUTES");
});app->response()->status(404);
/**
* Params
*/
// reads "?data[foo]=some+value"
$foo = $this->param('foo');
// reads "data[bar][name]=some+value" only if POST!
$bar = $this->param('bar.name', 'post');
// all params of bar ("object attributes")
// "?data[bar][name]=me&data[bar][mood]=happy" only if POST!
$bar = $this->param('bar');
//error_log($bar['name']. ' is '. $bar['mood']);
// reads multiple params in array
$params = $this->params(array('foo', 'bar.name1', 'bar.name1'));
//error_log($params['bar.name1']);
// reads multiple params only if they are POST
$params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post');
// reads multiple params only if they are POST and all are given!
$params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post', true);
if (!$params) {
error_log("Not all params given.. maybe some. Don't care");
}
// reads multiple params only if they are POST and replaces non given with defaults!
$params = $this->params(array('foo', 'bar.name1', 'bar.name1'), 'post', array(
'foo' => 'Some Default'
));
/**
* Redirect shortcut
*/
if (false) {
$this->redirect('/somewhere');
}
/**
* Rendering
*/
$this->render('folder/file', array(
'foo' => 'bar'
));
}
}No open issues yet, or sync has not completed.