diff --git a/README.md b/README.md index 3f6007f..cddd486 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ composer require miladrahimi/phprouter "4.*" First of all, you need to configure your webserver to handle all the HTTP requests with a single PHP file like `index.php`. Here you can see sample configurations for Apache HTTP Server and NGINX. * Apache `.htaccess` sample: - ``` + ```apacheconfig Options -MultiViews diff --git a/src/Router.php b/src/Router.php index b810dfd..66ef447 100644 --- a/src/Router.php +++ b/src/Router.php @@ -371,36 +371,33 @@ private function runControllerThroughMiddleware( * @param ServerRequestInterface $request * @return ResponseInterface|mixed|null * @throws InvalidControllerException + * @throws ReflectionException */ private function runController($controller, array $parameters, ServerRequestInterface $request) { - try { - if (is_string($controller) && strpos($controller, '@')) { - list($className, $methodName) = explode('@', $controller); - - if (class_exists($className) == false) { - throw new InvalidControllerException("Controller class `$controller` not found."); - } + if (is_string($controller) && strpos($controller, '@')) { + list($className, $methodName) = explode('@', $controller); - $classObject = new $className(); - - if (method_exists($classObject, $methodName) == false) { - throw new InvalidControllerException("Controller method `$methodName` not found."); - } + if (class_exists($className) == false) { + throw new InvalidControllerException("Controller class `$controller` not found."); + } - $parameters = $this->arrangeMethodParameters($className, $methodName, $parameters, $request); + $classObject = new $className(); - $controller = [$classObject, $methodName]; - } elseif (is_callable($controller)) { - $parameters = $this->arrangeFunctionParameters($controller, $parameters, $request); - } else { - throw new InvalidControllerException('Invalid controller: ' . $controller); + if (method_exists($classObject, $methodName) == false) { + throw new InvalidControllerException("Controller method `$methodName` not found."); } - return call_user_func_array($controller, $parameters); - } catch (ReflectionException $e) { - throw new InvalidControllerException('Reflection error', 0, $e); + $parameters = $this->arrangeMethodParameters($className, $methodName, $parameters, $request); + + $controller = [$classObject, $methodName]; + } elseif (is_callable($controller)) { + $parameters = $this->arrangeFunctionParameters($controller, $parameters, $request); + } else { + throw new InvalidControllerException('Invalid controller: ' . $controller); } + + return call_user_func_array($controller, $parameters); } /** @@ -457,6 +454,7 @@ function (ReflectionParameter $parameter) use ($parameters, $request) { return $parameters[$parameter->getName()]; } + /** @noinspection PhpPossiblePolymorphicInvocationInspection */ if ( ($parameter->getType() && $parameter->getType()->getName() == ServerRequestInterface::class) || ($parameter->getType() && $parameter->getType()->getName() == ServerRequest::class) || @@ -465,6 +463,7 @@ function (ReflectionParameter $parameter) use ($parameters, $request) { return $request; } + /** @noinspection PhpPossiblePolymorphicInvocationInspection */ if ( ($parameter->getType() && $parameter->getType()->getName() == Router::class) || ($parameter->getName() == 'router')