Files
php-nette-utils/lib/NetteUtils/Routing/RestRoute.php
T
2024-01-14 07:40:38 +01:00

47 lines
1.1 KiB
PHP
Executable File

<?php
namespace NetteUtils\Routing;
use Nette\Application\Routers\Route;
use Nette\Http\IRequest;
/**
* Extended Nette Route with capability to validate request method
*
* @author Jan Pavlíček <jan@pavlicek.dev>
*/
class RestRoute extends Route
{
/**
* List of HTTP methods that will match with this route
* @var array
*/
protected $allowedMethods = ['GET', 'POST', 'OPTIONS'];
public function __construct($methods, $mask, $metadata = [], $flags = 0)
{
if ($methods) {
$this->allowedMethods = array_merge(explode('|', $methods), ['OPTIONS']);
}
parent::__construct($mask, $metadata, $flags);
}
/**
* Maps HTTP request to a Request object. Does not match, if methods are defined and request
* does not match one of them.
*
* @return Nette\Application\Request|NULL
*/
public function match(IRequest $httpRequest) : ?array
{
if (!in_array($httpRequest->getMethod(), $this->allowedMethods)) {
return null;
}
return parent::match($httpRequest);
}
}