You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.1 KiB
46 lines
1.1 KiB
<?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);
|
|
}
|
|
}
|