Initial commit
This commit is contained in:
Executable
+46
@@ -0,0 +1,46 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user