pollus/dispatcher

1.0.0 2019-08-26 13:03 UTC

This package is auto-updated.

Last update: 2020-01-26 14:19:10 UTC


README

A basic dispatcher for pollus/controller that allows the usage of generic routes with any framework that implements PSR-7 and PSR-11

Install:

composer require pollus/dispatcher

Example usage with Slim Framework 4

This example is inside "example" folder.

/**
 * /index.php 
 * 
 * Run php -S localhost:8080 inside this folder to run this file
 * 
 * This example uses the Dispatcher with Slim Framework 4
 */

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Factory\AppFactory;
use Pollus\Dispatcher\Dispatcher;
use Psr\Http\Server\RequestHandlerInterface;
use Pollus\Dispatcher\Exceptions\NotFoundException;
use Pollus\Dispatcher\Exceptions\MethodNotAllowedException;
use Slim\Exception\HttpNotFoundException;
use Slim\Exception\HttpMethodNotAllowedException;
use Slim\Psr7\Response;

require __DIR__ . '/../vendor/autoload.php';

$app = AppFactory::create();

$app->addRoutingMiddleware();   

/**
 * Catch exceptions, use your own implementation here
 */
$app->add(function (ServerRequestInterface $request, RequestHandlerInterface $handler) 
{
    try
    {
        return $handler->handle($request);
    }
    catch(NotFoundException | HttpNotFoundException $ex)
    {
        $response = new Response();
        $response->withStatus(404)
                 ->getBody()
                 ->write($ex->getMessage());
        return $response;
        
    }
    catch(MethodNotAllowedException | HttpMethodNotAllowedException $ex)
    {
        $response = new Response();
        $response->withStatus(405)
                 ->getBody()
                 ->write($ex->getMessage());
        return $response;
    }
});

// You can also pass a ContainerInterface to second argument.
$dispatcher = new Dispatcher("\\Tests\\Controllers");

// Custom controller
$app->any('/', function (ServerRequestInterface $request, ResponseInterface $response, $args) use ($dispatcher)
{
    return $dispatcher
        ->setController("home")
        ->setMethod("index")
        ->prepare($request, $response, $args)
        ->run();
});

// Generic Route
$app->any('/{controller}/{method}[/{params:.*}]', function (ServerRequestInterface $request, ResponseInterface $response, $args) use ($dispatcher)
{
    return $dispatcher
        ->prepare($request, $response, $args)
        ->run();
});

$app->run();
/**
 *  /Controllers/HomeController.php
 */
namespace Tests\Controllers;

use Pollus\Controller\Controller;

class HomeController extends Controller
{
    /**
     * @method GET
     */
    public function index()
    {
        $this->response
                ->getBody()
                ->write("Hello World. Clique here to test <a href='/home/test-method/hello'>another method</a>");

        return $this->response;
    }

    /**
     * @method GET
     */
    public function testMethod(?string $testString)
    {   
        $this->response
                ->getBody()
                ->write("I am testMethod(), " . htmlentities($testString));
                
        return $this->response;
    }

    /**
     * @method POST
     */
    public function postMethod(?string $testString)
    {   
        $this->response
                ->getBody()
                ->write("I am postMethod(), " . htmlentities($testString));
                
        return $this->response;
    }
}

MIT License

Copyright (c) 2019 Renan Cavalieri

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.