poliander/janeiro

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.
This package is abandoned and no longer maintained. No replacement package was suggested.

Lightweight PHP MVC framework

0.3.0 2018-12-08 09:21 UTC

This package is auto-updated.

Last update: 2019-12-03 15:59:37 UTC


README

Build Status Scrutinizer_Code_Coverage Scrutinizer Code Quality License Latest Stable Version

A lightweight multi-module MVC framework providing...

  • PSR-11 dependency injection container with auto-wiring capability,
  • PSR-15 request handler with middleware support,
  • PSR-15 routing middleware

Bootstrapping

Examples use Zend\Diactoros as PSR-7/PSR-17 implementation.

Using nginx/php-fpm stack

// public/index.php

use Janeiro\Application;
use Zend\Diactoros\ServerRequestFactory;

$application = Application::fromConfiguration('../config/application.php');
$application
    ->process(ServerRequestFactory::fromGlobals())
    ->respond();

Using RoadRunner

// bin/worker.php

use Janeiro\Application;
use Spiral\Goridge;
use Spiral\RoadRunner;
use Zend\Diactoros\Response;

$application = Application::fromConfiguration('../config/application.php');

$worker = new RoadRunner\Worker(new Goridge\StreamRelay(STDIN, STDOUT));
$client = new RoadRunner\PSR7Client($worker);

while (null !== ($request = $client->acceptRequest())) {
    try {
        $response = $application->process($request)->getResponse();
    } catch (\Throwable $e) {
        $response = new Response('Internal error', 500);
        error_log((string)$e, E_USER_ERROR);
    }
    
    $client->respond($response);
}

Configuration

Lines which are commented out are invisible (internal) defaults that can be overwritten.

// config/application.php

use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseFactoryInterface;

return [
    'services' => [
#       RequestHandlerInterface::class  => \Janeiro\RequestHandler::class,
        ResponseFactoryInterface::class => \Zend\Diactoros\ResponseFactory::class,
    ],
    'routing' => [
        'namespaces' => [
            'YourApplication\Module'
        ],
#       'defaults' => [
#           'module'     => 'index',
#           'controller' => 'index',
#           'action'     => 'index'
#       ],
        'table' => [
            [
                'pattern'    => '/',
                'methods'    => [ 'GET' ]
            ], [
                'pattern'    => '/:module/:controller/:action',
                'target'     => [
                    'module'     => 1,
                    'controller' => 2,
                    'action'     => 3
                ],
            ], [
                'pattern'    => '/api/v1/customer/:integer',
                'methods'    => [ 'GET' ],
                'target'     => [
                    'module'     => 'api',
                    'controller' => 'customer',
                    'action'     => 'show',
                    'parameters' => [
                        'id'         => 1
                    ]
                ]
            ]
        ]
    ]
];

Controllers & Dependency Injection

// src/YourApplication/Module/Api/Controller/CustomerController.php

namespace YourApplication\Module\Api\Controller;

use Janeiro\Mvc\Controller;
use Janeiro\Mvc\Request;

use YourApplication\Repository\CustomerRepository;

class CustomerController extends Controller
{
    private $customers;
    
    /**
     * Constructor parameters are auto-wired
     */
    public function __construct(CustomerRepository $customers) {
        $this->customers = $customers;
    }
    
    /**
     * @header Content-Type: application/json
     */
    public function showAction(Request $request)
    {
        $customer = $this->customers->findOne($request->getParameter('id'));
        
        if (empty($customer)) {
            $request->getRoute()->forward(['action' => 'not_found']);
        }
        
        echo json_encode($customer->toArray());
    }
    
    public function notFoundAction(Request $request)
    {
        return $request->createResponse(404);
    }
}