-
Notifications
You must be signed in to change notification settings - Fork 3
Application Controllers
sunrise-php edited this page Jan 7, 2019
·
1 revision
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(
* 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(
* 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(
* 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(
* 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(
* 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.
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
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
}
Just register your dependencies in config/definitions.php
and use the new dependencies also as shown in the examples above.
Have questions?
Ask your questions in our chat:
Get more features for your application using Awesome PSR-15 Middleware