11use InvalidArgumentException;
30 protected $methods = [];
53 private $finalized =
false;
62 protected $outputBuffering =
'append';
69 protected $arguments = [];
76 protected $savedArguments = [];
94 public function __construct($methods, $pattern, $callable, $groups = [], $identifier = 0)
96 $this->methods = is_string($methods) ? [$methods] : $methods;
97 $this->pattern = $pattern;
98 $this->callable = $callable;
99 $this->groups = $groups;
100 $this->identifier =
'route' . $identifier;
108 if ($this->finalized) {
112 $groupMiddleware = [];
113 foreach ($this->getGroups() as $group) {
114 $groupMiddleware = array_merge($group->getMiddleware(), $groupMiddleware);
117 $this->middleware = array_merge($this->middleware, $groupMiddleware);
119 foreach ($this->getMiddleware() as $middleware) {
123 $this->finalized =
true;
133 return $this->callable;
143 $this->callable = $callable;
153 return $this->methods;
163 return $this->groups;
183 return $this->identifier;
193 return $this->outputBuffering;
209 if (!in_array($mode, [
false,
'prepend',
'append'],
true)) {
210 throw new InvalidArgumentException(
'Unknown output buffering mode');
212 $this->outputBuffering = $mode;
227 if (!is_string(
$name)) {
228 throw new InvalidArgumentException(
'Route name must be a string');
245 if ($includeInSavedArguments) {
246 $this->savedArguments[
$name] = $value;
248 $this->arguments[
$name] = $value;
260 public function setArguments(array $arguments, $includeInSavedArguments =
true)
262 if ($includeInSavedArguments) {
263 $this->savedArguments = $arguments;
265 $this->arguments = $arguments;
276 return $this->arguments;
289 if (array_key_exists(
$name, $this->arguments)) {
290 return $this->arguments[
$name];
308 $this->setArguments($this->savedArguments);
311 foreach ($arguments as $k => $v) {
312 $this->setArgument($k, $v,
false);
351 $this->callable = $this->resolveCallable($this->callable);
361 } elseif (is_string($newResponse)) {
363 if (
$response->getBody()->isWritable()) {
364 $response->getBody()->write($newResponse);
foreach($paths as $path) $request
An exception for terminatinating execution or to throw for unit testing.
Default route callback strategy with route parameters as an array of arguments.
getCallable()
Get route callable.
run(ServerRequestInterface $request, ResponseInterface $response)
Run route.
setArguments(array $arguments, $includeInSavedArguments=true)
Replace route arguments.
getMethods()
Get route methods.
setArgument($name, $value, $includeInSavedArguments=true)
Set a route argument.
setCallable($callable)
This method enables you to override the Route's callable.
__construct($methods, $pattern, $callable, $groups=[], $identifier=0)
Create new route.
getArgument($name, $default=null)
Retrieve a specific route argument.
getArguments()
Retrieve route arguments.
setOutputBuffering($mode)
Set output buffering mode.
getGroups()
Get parent route groups.
setName($name)
Set route name.
finalize()
Finalize the route in preparation for dispatching.
getIdentifier()
Get route identifier.
prepare(ServerRequestInterface $request, array $arguments)
Prepare the route for use.
getOutputBuffering()
Get output buffering mode.
Representation of an outgoing, server-side response.
Representation of an incoming, server-side HTTP request.
Defines a contract for invoking a route callable.
Slim Framework (https://slimframework.com)
trait MiddlewareAwareTrait
Middleware.
addMiddleware(callable $callable)
Add middleware.