orx0r/slim3-controller

This package is abandoned and no longer maintained. No replacement package was suggested.

Implementation of controller for Slim Framework v3

0.1.2 2016-05-08 15:30 UTC

This package is not auto-updated.

Last update: 2020-02-21 17:05:45 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Implementation of controller for Slim Framework v3

Install

Via Composer

$ composer require orx0r/slim3-controller

Usage

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');

Testing

$ composer test

Contributing

Please see CONTRIBUTING

License

The MIT License (MIT). Please see License File for more information.