Executes the router based on the url.

@throws Exception Throws an exception if no location was set and no default route was found.

@return mixed The result of the callable method.
347345
public function execute()
    {
        // Check if we have a url
        if (!$this->url || trim($this->url) == '') {
            $this->url = '/';
        }

        // Make sure the url starts with a slash
        if (substr($this->url, 0, 1) !== '/') {
            $this->url = '/' . $this->url;
        }

        // Check if the absolute route exists in our routes array
        // - and if that route has the same type of request as the current request.
        if (isset($this->routes[$this->method][$this->url])) {
            return call_user_func_array($this->routes[$this->method][$this->url], []);
        }

        // Check if the absolute route exists in our routes array
        // - and if that route has the "any" type.
        if (isset($this->routes['any'][$this->url])) {
            return call_user_func_array($this->routes['any'][$this->url], []);
        }

        // Create an array of methods to check and loop through them
        $methods = [$this->method, 'any'];
        foreach ($methods as $method) {
            // Loop through the routes set for this method
            foreach ($this->routes[$method] as $route => $callable) {
                // Check if the route matches
                $regex = '/^' . str_replace('/', '\/', $route) . '/Uim';
                $matches = [];
                if (preg_match($regex, $this->url, $matches) === 1 && $matches[0] === $this->url) {
                    // First value in the array is the string that matched
                    array_shift($matches);

                    // Execute the callable method
                    return call_user_func_array($callable, $matches);
                }
            }
        }

        // No route found, check if we have an empty route.
        // - We should always have an empty route.
        if (!isset($this->routes['any']['/'])) {
            header('HTTP/1.1 404 Not Found');
            exit;
        }

        // Return the empty route's callable
        return call_user_func_array($this->routes['any']['/'], []);
    }
1201662
public function handle()
    {
        try {
            $query = $this->di->request->getRoute();
            $parts = $this->di->request->getRouteParts();

            // Match predefined routes
            foreach ($this->routes as $route) {
                if ($route->match($query)) {
                    return $route->handle();
                }
            }

            // Default handling route as :controller/:action/:params using the dispatcher
            $dispatcher = $this->di->dispatcher;
            $dispatcher->setControllerName(isset($parts[0]) ? $parts[0] : 'index');

            if ($dispatcher->isValidController()) {
                $dispatcher->setActionName(isset($parts[1]) ? $parts[1] : 'index');

                $params = [];
                if (isset($parts[2])) {
                    $params = $parts;
                    array_shift($params);
                    array_shift($params);
                }
                $dispatcher->setParams($params);

                if ($dispatcher->isCallable()) {
                    return $dispatcher->dispatch();
                }
            }

            // Use the "catch-all" route
            if ($this->defaultRoute) {
                return $this->defaultRoute->handle();
            }

            // No route was matched
            $this->handleInternal('404');
        } catch (\Exception $e) {
            // Exception codes can match a route for a http status code
            $code = $e->getCode();
            $statusCodes = [403, 404, 500];
            if (in_array($code, $statusCodes)) {
                $this->di->flash->setMessage($e->getMessage());
                $this->handleInternal($code);
            } else {
                throw $e;
            }
        }
    }
327278
public function run()
    {
        $this->tryAssignController();
        if (!$this->isControllerAssigned()) {
            throw new Exception("It's unable to assign controller to this route path.");
        }
        $oCtrl = $this->createCtrl();
        $oCtrl->{$this->actionName}($this->actionArgs);
    }
1168740
public function execute()
    {
        $controller = $this->getController();

        if (!$controller) {
            $controller = $this->factoryController();
            $this->setController($controller);
        }

        $this->prepareController($controller);

        $action = $this->getAction();

        if (!$controller->respondTo($action)) {
            throw new Exceptions\ActionNotFoundException(sprintf(
                "Action '%s::%s' was not defined",
                $controller->getClass(),
                $action
            ));
        }

        $controller->beforeAction();
        $controller->send($action);
        $controller->afterAction();

        return $controller->getResponse();
    }
1286806
public function handle(Request $request) : Response
	{
		if ($result = $this->handleContext($request, $this->rootContext)) {
			return $result;
		}
		
		if ($defaultHandler = $this->rootContext->getDefault()) {
			return $this->resultToResponse(
				call_user_func($defaultHandler, $request, null)
			);
		}
		
		throw new Exception('No matching route found. Set a default handler on root context to cover this case.');
	}
912744
protected function _loadUrl()
    {
        if(!key_exists('url', $_GET))
            throw new Exception("You must supply a 'url' parameter in the request");

        $this->_url = ($this->_forwardHost)
            ? $this->_forwardHost . $_GET['url']
            : $_GET['url'];
    }
783666
private function execute()
    {
        $data = curl_exec($this->ch);

        if ($data === false) {
            $this->close();

            throw new \Exception('Impossible to pass the result.');
        }

        $this->error = curl_error($this->ch);

        $this->errno = curl_errno($this->ch);

        $this->header = curl_getinfo($this->ch);

        $this->executed = true;

        $this->close();

        return $data;
    }
632570
public function run(HTTPRequest $request) {
		$options = $request->getRoute()->getOptions();
		if( !empty($options['url_config']) ) {
			$url = AppConfig::instance()->get($options['url_config']);
			if( !$url ) {
				throw new Exception('The RedirectController requires a valid url_config option, please check your configuration.');
			}
		} else
		if( empty($options['redirect']) ) {
			throw new Exception('The RedirectController requires a redirect option, add it to your route configuration.');
		} else {
			$url = u($options['redirect']);
		}
		return new RedirectHTTPResponse($url);
	}
1297910
public function process()
        {
            try {
                $controllerName = $this->getController();
                if(null == $controllerName) {
                    throw new Exception('Method not allowed', 405);
                }
                $controller = new \ReflectionClass($controllerName);
                if(!$controller->isInstantiable()) {
                    throw new Exception('Bad Request', 400);
                }
                try {
                    $method = $controller->getMethod($this->request['method']);
                } catch(ReflectionException $re) {
                    throw new Exception('Unsupported HTTP method ' . $this->request['method'], 405);
                }
                if(!$method->isStatic()) {
                    $controller = $controller->newInstance($this->request);
                    if(!$controller->checkAuth()) {
                        throw new Exception('Unauthorized', 401);
                    }
                    $method->invoke($controller);
                    $this->response = $controller->getResponse();
                    $this->responseStatus = $controller->getResponseStatus();
                } else {
                    throw new Exception('Static methods not supported in Controllers', 500);
                }
                if(is_null($this->response)) {
                    throw new Exception('Method not allowed', 405);
                }
            } catch (Exception $re) {
                $this->responseStatus = $re->getCode();
                $this->response = array('ErrorCode' => $re->getCode(), 'ErrorMessage' => $re->getMessage());
            }
            $this->response()->send();
        }
1311668
public function execute()
    {
        $this->_setOptions();

        $sResponse = $this->_execute();
        $this->_saveStatusCode();

        $iCurlErrorNumber = $this->_getErrorNumber();

        $this->_close();

        if ($iCurlErrorNumber) {
            $host = $this->getUrl();
            $parameters = http_build_query($this->getParameters(), "", "&");
            $message = $this->_getErrorMessage($iCurlErrorNumber);
            throw new Exception("cURL failed with message '$message' when calling '$host?$parameters'");
        }

        return $sResponse;
    }
793132
public function route()
    {
        global $logger;
        try {
            // TODO : Refactor Code :
            // $url    = URLParser::parse();
            // $route  = RouteMapper::getRoute($url['route']);
            // $router = new Router();
            // $router->route($route);
            $urlInterpreter = new UrlInterpreter();
            $command        = $urlInterpreter->getCommand();
            $router         = new Router($command);
            $logger->debug('Routing to : '.$command->getControllerName());
            $router->route();
        } catch (Exception $e) {
            throw new RouteNotFoundException();	
        }
    }
758124
private function _setRequest()
	{
		if ($this->_requestUri !== NULL) {
            $this->_request = preg_replace('#(^/)|(^' . ROOT . ')|(/$)#', '', $this->_requestUri);

            if (! $this->_request) {
            	$this->_request = self::DEFAULT_MODULE . '/' . self::DEFAULT_VIEW;
            }

            $this->_requestVars = explode('/', $this->_request);
            if (count($this->_requestVars) == 1) {
				$this->_request       .= '/' . self::DEFAULT_VIEW;
				$this->_requestVars[]  = self::DEFAULT_VIEW;
            }

            return $this;
        }

        throw new Exception('Request URI is not available');
	}
450774
private function RunRequest()
    {
        $result = curl_exec($this->_curlHandle);
        if ($result === false && curl_errno($this->_curlHandle) != 0) {
            $this->logger->error("cURL error: " . curl_error($this->_curlHandle));
            throw new Exception('cURL error: ' . curl_error($this->_curlHandle));
        }

        $response = new HttpResponse();
        $response->ResponseCode = (int) curl_getinfo($this->_curlHandle, CURLINFO_HTTP_CODE);

        curl_close($this->_curlHandle);

        $explode = explode("\r\n\r\n", $result);

        // multiple query (follow redirect) take only the last request
        $explode = array_slice($explode, sizeof($explode) - 2, 2);

        $response->Headers = explode("\n", implode($explode));
        $response->Body = array_pop($explode);

        return $response;
    }
1043117
public function match(MainRouter $router, $route = null)
    {
        $route = $route ?: $this->app->uri->route;
        $route = $route ?: '/';

        $input   = $this->app->input;
        $request = $this->app->request;

        if ($request->hasHeader('X-HTTP-Method-Override')) {
            $method = $request->getHeaderLine('X-HTTP-Method-Override');
        } else {
            $method = $input->get('_method') ?: $input->getMethod();
        }

        // Pass variables to custom method
        if ($input->$method) {
            $httpMethod = $input->getMethod();

            $input->$method->setData($input->$httpMethod->toArray());
        }

        // Prepare option data
        $uri = $request->getUri();

        $options = [
            'scheme' => $uri->getScheme(),
            'host' => $uri->getHost(),
            'port' => $uri->getPort(),
        ];

        try {
            return $router->match($route, $method, $options);
        } catch (RouteNotFoundException $e) {
            // If simple_route is disabled, just continue throw the exception.
            if (!$this->app->get('routing.simple_route', false)) {
                throw $e;
            }

            // Simple routing
            $matched = $this->matchSimpleRouting($route, $method, $router);

            if ($matched === false) {
                throw new RouteNotFoundException($e->getMessage(), $e->getCode(), $e);
            }

            return $matched;
        }
    }
711598
private function invokeMethod()
    {
        if ($this->located_controller === NULL)
        {
            throw new Exception('404 Not Found.');
        }

        $class = "App\\Controller\\" . $this->located_controller;

        if (!class_exists($class))
        {
            throw new Exception("$class class not found.");
        }

        $controller = new $class;
        if (method_exists($controller, $this->located_method))
        {
            $reflection = new ReflectionMethod($controller, $this->located_method);

            # Perpareing method parameter from remaining url.
            $parameter = array();
            $index = 0;
            foreach ($reflection->getParameters() as $param)
            {
                if (array_key_exists($index, $this->remaining_uri))
                {
                    $parameter[$index] = $this->remaining_uri[$index];
                }
                else if ($param->isOptional())
                {
                    $parameter[$index] = $param->getDefaultValue();
                }
                else
                {
                    throw new Exception('Not found.');
                }
                $index++;
            }

            $reflection->invokeArgs($controller, $parameter);
        }
    }
246539
private static function router()
    {
        $files = ['Url'];
        $folder = static::$root.'Router'.'/';

        self::call($files, $folder);

        //New router surface

        $files = ['Route', 'Routes'];
        $folder = static::$root.'Http/Router'.'/';

        self::call($files, $folder);

        $files = ['RouteDuplicatedException', 'RouteMiddlewareNotFoundException', 'RouteNotFoundInRoutesRegisterException', 'NotFoundHttpException'];
        $folder = static::$root.'Http/Router/Exceptions'.'/';

        self::call($files, $folder);
    }
910275
public function run()
    {
        if (!($controller = \Devil::app()->url->getNext())) {
            $controller = 'site';
        }
        if ($controller = $this->loadController($controller)) {
            if (!$nextUrl = \Devil::app()->url->getNext()) {
                $nextUrl = 'index';
            }
            $controller->performAction(NamesHelper::urlToClass($nextUrl));
        } else {
            throw new HttpException(HttpException::NOT_FOUND);
        }
    }