Skip to content

Commit b8123dc

Browse files
committed
Added array usage for route callbacks. Also some updates.
1 parent 4ef5f0b commit b8123dc

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

src/Router.php

+1
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,7 @@ protected function addRoute(string $uri, string $method, $callback, $options = [
699699
$beforeMiddlewares = array_merge($beforeMiddlewares, $this->calculateMiddleware($options['before'] ?? []));
700700
$afterMiddlewares = array_merge($afterMiddlewares, $this->calculateMiddleware($options['after'] ?? []));
701701

702+
$callback = is_array($callback) ? implode('@', $callback) : $callback;
702703
$routeName = is_string($callback)
703704
? strtolower(preg_replace(
704705
'/[^\w]/i', '.', str_replace($this->namespaces['controllers'], '', $callback)

src/Router/RouterCommand.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ public function beforeAfter($command)
159159
$response = false;
160160
if (is_array($resolvedMiddleware)) {
161161
foreach ($resolvedMiddleware as $middleware) {
162-
$response = $this->runMiddleware($command, $this->resolveMiddleware($middleware), $params, $info);
162+
$response = $this->runMiddleware(
163+
$command,
164+
$this->resolveMiddleware($middleware),
165+
$params,
166+
$info
167+
);
163168
}
164169
return $response;
165170
}
@@ -341,15 +346,14 @@ protected function sendResponse($response)
341346
$this->response->headers->set('Content-Type', 'application/json');
342347
return $this->response
343348
->setContent(json_encode($response))
344-
->prepare($this->request)
345349
->send();
346350
}
347351

348352
if (!is_string($response)) {
349353
return $response instanceof Response ? $response->send() : print($response);
350354
}
351355

352-
return $this->response->setContent($response)->prepare($this->request)->send();
356+
return $this->response->setContent($response)->send();
353357
}
354358

355359
/**

src/Router/RouterException.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ class RouterException
1414
/**
1515
* Create Exception Class.
1616
*
17-
* @param $message
17+
* @param string $message
18+
*
19+
* @param int $statusCode
1820
*
19-
* @return string
2021
* @throws Exception
2122
*/
22-
public function __construct($message)
23+
public function __construct(string $message, int $statusCode = 500)
2324
{
25+
http_response_code($statusCode);
2426
if (self::$debug) {
25-
throw new Exception($message, 1);
27+
throw new Exception($message, $statusCode);
2628
}
27-
2829
die("<h2>Opps! An error occurred.</h2> {$message}");
2930
}
3031
}

src/Router/RouterRequest.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,12 @@ public function validMethod(string $data, string $method): bool
8686
*
8787
* @return string
8888
*/
89-
public function getMethod()
89+
public function getMethod(): string
9090
{
9191
$method = $this->request->getMethod();
92-
if (!empty($_POST['_method'])) {
93-
$method = strtoupper($_POST['_method']);
92+
$formMethod = $this->request->request->get('_method');
93+
if (!empty($formMethod)) {
94+
$method = strtoupper($formMethod);
9495
}
9596

9697
return $method;

0 commit comments

Comments
 (0)