Implementation of controller for Slim Framework v3
Via Composer
$ composer require orx0r/slim3-controller
Please see example how it works in action
// in index.php
$app->get('/hello/{name}', 'app\controllers\HelloController:index');
// in app/controllers/HelloController.php
namespace app\controllers;
use Orx0r\Slim\Controller\Controller;
class HelloController extends Controller
{
public function actionIndex($name)
{
return $this->response->getBody()->write("Hello, $name");
}
}
If you use one of template engine, you can use it in controller:
// in index.php
$c = new \Slim\Container;
$c['view'] = function ($container) {
$view = new \Slim\Views\Twig( __DIR__ . '/../templates');
$view->addExtension(new \Slim\Views\TwigExtension(
$container['router'],
$container['request']->getUri()
));
return $view;
};
$app->get('/hello/{name}', 'app\controllers\HelloController:index');
// in app/controllers/HelloController.php
public function actionIndex($name)
{
return $this->render('hello/index.html', ['name' => $name]);
}
In small Slim app you can use controllerNamespace for resolving all your controllers in same namespace by specifying only controller name. It works without breaking your old code
// register CallableResolver. pass second parameter as controllerNamespace
$c['callableResolver'] = function ($container) {
return new CallableResolver($container, 'app\controllers');
};
$app->get('/hello/{name}', 'HelloController:index');
$ composer test
Please see CONTRIBUTING
The MIT License (MIT). Please see License File for more information.