Skip to content

Application Controllers

sunrise-php edited this page Jan 7, 2019 · 1 revision

Simple controller

declare(strict_types=1);
namespace App\Http\Controller;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

/**
 * @Route(
 *   id="test",
 *   path="/test",
 *   methods={"GET"}
 * )
 */
class TestController implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler) : ResponseInterface
    {
    	$response = $handler->handle($request);

    	$response->getBody()->write(\sprintf("route: %s\n",
    		$request->getAttribute('@route')
    	));

    	return $response;
    }
}
curl -X GET 0.0.0.0:8080/test

Route with attributes

/**
 * @Route(
 *   id="test",
 *   path="/test/{foo}",
 *   methods={"GET"}
 * )
 */
class TestController implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler) : ResponseInterface
    {
        $response = $handler->handle($request);

        $response->getBody()->write(\sprintf("route: %s; foo: %s\n",
            $request->getAttribute('@route'),
            $request->getAttribute('foo')
        ));

        return $response;
    }
}
curl -X GET 0.0.0.0:8080/test/bar

Route with regular expression

/**
 * @Route(
 *   id="test",
 *   path="/test/{id<\d+>}",
 *   methods={"GET"}
 * )
 */
class TestController implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler) : ResponseInterface
    {
        $response = $handler->handle($request);

        $response->getBody()->write(\sprintf("route: %s; id: %d\n",
            $request->getAttribute('@route'),
            $request->getAttribute('id')
        ));

        return $response;
    }
}
curl -X GET 0.0.0.0:8080/test/100
curl -X GET 0.0.0.0:8080/test/foo

Route with optional attributes

/**
 * @Route(
 *   id="test",
 *   path="/test(/{year<\d{4}>}(/{month<\d{2}>}(/{day<\d{2}>})))",
 *   methods={"GET"}
 * )
 */
class TestController implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler) : ResponseInterface
    {
        $response = $handler->handle($request);

        $response->getBody()->write(\sprintf("route: %s; year: %s; month: %s; day: %s\n",
            $request->getAttribute('@route'),
            $request->getAttribute('year', 'undefined'),
            $request->getAttribute('month', 'undefined'),
            $request->getAttribute('day', 'undefined')
        ));

        return $response;
    }
}
curl -X GET 0.0.0.0:8080/test
curl -X GET 0.0.0.0:8080/test/1990
curl -X GET 0.0.0.0:8080/test/1990/03
curl -X GET 0.0.0.0:8080/test/1990/03/23

Route with several methods

/**
 * @Route(
 *   id="test",
 *   path="/test",
 *   methods={"GET", "POST", "PATCH", "DELETE"}
 * )
 */
class TestController implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler) : ResponseInterface
    {
        $response = $handler->handle($request);

        $response->getBody()->write(\sprintf("route: %s; method: %s\n",
            $request->getAttribute('@route'),
            $request->getMethod()
        ));

        return $response;
    }
}
curl -X GET 0.0.0.0:8080/test
curl -X POST 0.0.0.0:8080/test
curl -X PATCH 0.0.0.0:8080/test
curl -X DELETE 0.0.0.0:8080/test

Route with middlewares

/**
 * @Route(
 *   id="test",
 *   path="/test",
 *   methods={"GET"},
 *   before={
 *     "App\Http\Middleware\FooMiddleware",
 *     "App\Http\Middleware\BarMiddleware"
 *   },
 *   after={
 *     "App\Http\Middleware\BazMiddleware",
 *     "App\Http\Middleware\QuxMiddleware"
 *   }
 * )
 */
class TestController implements MiddlewareInterface
{
    // some code
}

@Route.before this is annotation option must be contain middleware queue that will be runs before the controller runs.

@Route.after this is annotation option must be contain middleware queue that will be runs after the controller runs.

Dependency Injection in controllers

Logger

use Psr\Log\LoggerInterface;

/**
 * @Route(
 *   id="test",
 *   path="/test",
 *   methods={"GET"}
 * )
 */
class TestController implements MiddlewareInterface
{
    /**
     * @Inject
     * 
     * @var LoggerInterface
     */
    protected $logger;

    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler) : ResponseInterface
    {
        $this->logger->debug('foo bar');

        return $handler->handle($request);
    }
}
curl -X GET http://0.0.0.0:8080/test
tail -1 app.log

Entity Manager

use Doctrine\ORM\EntityManager;

/**
 * @Inject
 *
 * @var EntityManager
 */
protected $entityManager;

public function process(
    ServerRequestInterface $request,
    RequestHandlerInterface $handler) : ResponseInterface
{
    // some code

    $this->entityManager->persist($entity);
    $this->entityManager->flush();

    // some code
}

Your dependencies

Just register your dependencies in config/definitions.php and use the new dependencies also as shown in the examples above.

Learn the structure of the application