bittyphp/bitty-controller

This package is abandoned and no longer maintained. The author suggests using the bittyphp/controller package instead.

Abstract controller.

v2.0.2 2019-03-03 23:32 UTC

README

Build Status Codacy Badge PHPStan Enabled Mutation Score Total Downloads License

Abstract controller for Bitty.

This package is not required. It merely provides an abstract controller that can be extended to provide commonly useful functionality.

Installation

It's best to install using Composer.

$ composer require bittyphp/controller

Container Access

A simple wrapper exists around the container get() method purely to access services using less code.

<?php

namespace Acme\Controller;

use Bitty\Controller\AbstractController;
use Psr\Http\Message\ResponseInterface;

class ExampleController extends AbstractController
{
    public function test(): ResponseInterface
    {
        // Short method
        $myService = $this->get('some.service');

        // Normal, longer method
        $myOtherService = $this->container->get('some.other.serivce');

        // ...
    }
}

Route Redirects

It is fairly common to need to redirect a user to a different route for various reasons. For that, the redirectToRoute() method exists to generate the route URI and return a redirect response.

<?php

namespace Acme\Controller;

use Bitty\Controller\AbstractController;
use Psr\Http\Message\ResponseInterface;

class ExampleController extends AbstractController
{
    public function test(): ResponseInterface
    {
        // Redirect to another route with optional parameters.
        return $this->redirectToRoute('some.route', ['foo' => 'bar']);
    }
}

Rendering Templates

You can render templates as a response using the render() method. Note: This method requires a view service to be defined that implements Bitty\View\ViewInterface. See the View Layer for what template engines are available or how to build your own.

<?php

namespace Acme\Controller;

use Bitty\Controller\AbstractController;
use Psr\Http\Message\ResponseInterface;

class ExampleController extends AbstractController
{
    public function test(): ResponseInterface
    {
        // Render a template with the given data
        return $this->render('some.template.html', ['foo' => 'bar']);
    }
}