/** * 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; }
public function matchRegex(Request $request, $url) { /* Match on custom defined regular expression */ if ($this->regex === null) { return null; } $parameters = []; if (preg_match($this->regex, $request->getHost() . $url, $parameters) > 0) { /* Remove global match */ $this->parameters = array_slice($parameters, 1); return true; } return false; }
public function handleError(Request $request, RouterEntry $router = null, \Exception $error) { if ($request->getMethod() !== 'get') { response()->json(array('error' => $error->getMessage(), 'code' => $error->getCode())); } if ($error instanceof OrganisationDisabledException) { $this->showDisabledImage(); } if ($error->getCode() === 404) { $this->showMissingImage($request); } $this->showErrorImage($request); // Might be optimised. die($error->getMessage()); }
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']); }
/** * 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; }
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; }
public function handle(Request $request) { $domain = env('DEBUG', false) ? 'dscuz' : str_ireplace('.' . Registry::getInstance()->get('host'), '', $request->getHost()); $request->source = Memcache::getInstance()->put('source_' . $domain, 60 * 60, function () use($domain) { return Source::getBySubdomain($domain); }); $this->setParameters($request); if (!$request->source->hasRow()) { throw new \ErrorException('Invalid source.'); } if ($request->source->getOrganisation() !== null && $request->source->getOrganisation()->disabled) { throw new OrganisationDisabledException('Source has been disabled'); } if ($request->source->require_ssl && !$request->getIsSecure()) { redirect('https://' . $request->getHost() . $request->getUri(), 101); } // Check for valid api-token if posting if ($request->getMethod() !== 'get') { $token = request()->getHeader('X-Auth-Token'); if ($token === null || $token !== $request->source->getOrganisation()->api_token) { throw new \InvalidArgumentException('Invalid X-Auth-Token.'); } } }
/** * 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); }
/** * Get all get/post items * @param array|null $filter Only take items in filter * @return array */ public function all(array $filter = null) { $output = $_POST; if ($this->request->getMethod() === 'post') { $contents = file_get_contents('php://input'); if (strpos(trim($contents), '{') === 0) { $output = json_decode($contents, true); if ($output === false) { $output = []; } } } $output = array_merge($_GET, $output); if ($filter !== null) { $output = array_filter($output, function ($key) use($filter) { return in_array($key, $filter) === true; }, ARRAY_FILTER_USE_KEY); } return $output; }
public function handleError(\Pecee\Http\Request $request, \Pecee\SimpleRouter\Route\ILoadableRoute &$route = null, \Exception $error) { echo 'ExceptionHandler 1 loaded' . chr(10); $request->setUri('/'); return $request; }