bittyphp / controller
Abstract controller.
Requires
- php: >=7.1.0
- bittyphp/http: ^2.0
- bittyphp/router: ^3.0
- bittyphp/view: ^2.0
- psr/container: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- bizurkur/coding-standard: ^1.0
- codacy/coverage: ^1.4
- infection/infection: ^0.12.0
- jakub-onderka/php-parallel-lint: ^1.0
- localheinz/composer-normalize: ^1.1
- phing/phing: ^2.16
- phpstan/phpstan: ^0.10.7
- phpstan/phpstan-deprecation-rules: ^0.10.2
- phpstan/phpstan-phpunit: ^0.10.0
- phpstan/phpstan-strict-rules: ^0.10.1
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^3.0
This package is auto-updated.
Last update: 2023-05-29 01:39:36 UTC
README
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']); } }