/** * Method called to check if route matches * * @param Request $request * @return bool */ public function matchRoute(Request $request) { /* Skip if prefix doesn't match */ if ($this->prefix !== null && stripos($request->getUri(), $this->prefix) === false) { return false; } return $this->matchDomain($request); }
public function handleError(Request $request, ILoadableRoute &$route = null, \Exception $error) { // Return json errors if we encounter an error on the API. if (stripos($request->getUri(), '/api') !== false) { response()->json(['error' => $error->getMessage()]); } if ($error instanceof NotFoundHttpException) { $request->setUri(url('page.notfound')); return $request; } throw $error; }
/** * Check if the url matches the urls in the except property * @param Request $request * @return bool */ protected function skip(Request $request) { if ($this->except === null || is_array($this->except) === false) { return false; } $max = count($this->except) - 1; for ($i = $max; $i >= 0; $i--) { $url = $this->except[$i]; $url = rtrim($url, '/'); if ($url[strlen($url) - 1] === '*') { $url = rtrim($url, '*'); $skip = stripos($request->getUri(), $url) === 0; } else { $skip = $url === rtrim($request->getUri(), '/'); } if ($skip === true) { return true; } } return false; }
/** * Get url for a route by using either name/alias, class or method name. * * The name parameter supports the following values: * - Route name * - Controller/resource name (with or without method) * - Controller class name * * When searching for controller/resource by name, you can use this syntax "route.name@method". * You can also use the same syntax when searching for a specific controller-class "MyController@home". * If no arguments is specified, it will return the url for the current loaded route. * * @param string|null $name * @param string|array|null $parameters * @param array|null $getParams * @throws \InvalidArgumentException * @return string */ public function getUrl($name = null, $parameters = null, $getParams = null) { if ($getParams !== null && is_array($getParams) === false) { throw new \InvalidArgumentException('Invalid type for getParams. Must be array or null'); } /* Only merge $_GET when all parameters are null */ if ($name === null && $parameters === null && $getParams === null) { $getParams = $_GET; } else { $getParams = (array) $getParams; } /* Return current route if no options has been specified */ if ($name === null && $parameters === null) { $url = rtrim(parse_url($this->request->getUri(), PHP_URL_PATH), '/'); return ($url === '' ? '/' : $url . '/') . $this->arrayToParams($getParams); } /* If nothing is defined and a route is loaded we use that */ if ($name === null && $this->loadedRoute !== null) { return $this->loadedRoute->findUrl($this->loadedRoute->getMethod(), $parameters, $name) . $this->arrayToParams($getParams); } /* We try to find a match on the given name */ $route = $this->findRoute($name); if ($route !== null) { return $route->findUrl($route->getMethod(), $parameters, $name) . $this->arrayToParams($getParams); } /* Using @ is most definitely a controller@method or alias@method */ if (strpos($name, '@') !== false) { list($controller, $method) = explode('@', $name); /* Loop through all the routes to see if we can find a match */ $max = count($this->processedRoutes) - 1; /* @var $route ILoadableRoute */ for ($i = $max; $i >= 0; $i--) { $route = $this->processedRoutes[$i]; /* Check if the route contains the name/alias */ if ($route->hasName($controller)) { return $route->findUrl($method, $parameters, $name) . $this->arrayToParams($getParams); } /* Check if the route controller is equal to the name */ if ($route instanceof IControllerRoute && strtolower($route->getController()) === strtolower($controller)) { return $route->findUrl($method, $parameters, $name) . $this->arrayToParams($getParams); } } } /* No result so we assume that someone is using a hardcoded url and join everything together. */ $url = trim(join('/', array_merge((array) $name, (array) $parameters)), '/'); return ($url === '' ? '/' : '/' . $url . '/') . $this->arrayToParams($getParams); }
public function matchRoute(Request $request) { $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); $url = rtrim($url, '/') . '/'; /* Match global regular-expression for route */ $domainMatch = $this->matchRegex($request, $url); if ($domainMatch !== null) { return $domainMatch; } /* Make regular expression based on route */ $parameters = $this->parseParameters($this->url, $url); if ($parameters === null) { return false; } $this->setParameters($parameters); return true; }
public function matchRoute(Request $request) { $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); $url = rtrim($url, '/') . '/'; /* Match global regular-expression for route */ $domainMatch = $this->matchRegex($request, $url); if ($domainMatch !== null) { return $domainMatch; } $route = rtrim($this->url, '/') . '/{id?}/{action?}'; $parameters = $this->parseParameters($route, $url); if ($parameters === null) { return false; } $this->parameters = (array) $parameters; $action = isset($this->parameters['action']) ? $this->parameters['action'] : null; unset($this->parameters['action']); $method = $request->getMethod(); // Delete if ($method === static::REQUEST_TYPE_DELETE && isset($this->parameters['id'])) { return $this->call($this->methodNames['destroy']); } // Update if (isset($this->parameters['id']) && in_array($method, [static::REQUEST_TYPE_PATCH, static::REQUEST_TYPE_PUT], false)) { return $this->call($this->methodNames['update']); } // Edit if ($method === static::REQUEST_TYPE_GET && isset($this->parameters['id']) && strtolower($action) === 'edit') { return $this->call($this->methodNames['edit']); } // Create if ($method === static::REQUEST_TYPE_GET && strtolower($action) === 'create') { return $this->call($this->methodNames['create']); } // Save if ($method === static::REQUEST_TYPE_POST) { return $this->call($this->methodNames['store']); } // Show if ($method === static::REQUEST_TYPE_GET && isset($this->parameters['id'])) { return $this->call($this->methodNames['show']); } // Index return $this->call($this->methodNames['index']); }
public function matchRoute(Request $request) { $url = parse_url(urldecode($request->getUri()), PHP_URL_PATH); $url = rtrim($url, '/') . '/'; /* Match global regular-expression for route */ if ($this->matchRegex($request, $url) === true) { return true; } if (stripos($url, $this->url) === 0 && strtolower($url) === strtolower($this->url)) { $strippedUrl = trim(str_ireplace($this->url, '/', $url), '/'); $path = explode('/', $strippedUrl); if (count($path) > 0) { $method = !isset($path[0]) || trim($path[0]) === '' ? $this->defaultMethod : $path[0]; $this->method = $method; $this->parameters = array_slice($path, 1); // Set callback $this->setCallback($this->controller . '@' . $this->method); return true; } } return false; }