/** * Finds and serves the requested backend controller. * If the controller cannot be found, returns the Cms page with the URL /404. * If the /404 page doesn't exist, returns the system 404 page. * @param string $url Specifies the requested page URL. * If the parameter is omitted, the current URL used. * @return string Returns the processed page content. */ public function run($url = null) { $params = RouterHelper::segmentizeUrl($url); /* * Look for a Module controller */ $module = isset($params[0]) ? $params[0] : 'backend'; $controller = isset($params[1]) ? $params[1] : 'index'; self::$action = $action = isset($params[2]) ? $this->parseAction($params[2]) : 'index'; self::$params = $controllerParams = array_slice($params, 3); $controllerClass = '\\' . $module . '\\Controllers\\' . $controller; if ($controllerObj = $this->findController($controllerClass, $action, base_path() . '/modules')) { return $controllerObj->run($action, $controllerParams); } /* * Look for a Plugin controller */ if (count($params) >= 2) { list($author, $plugin) = $params; $controller = isset($params[2]) ? $params[2] : 'index'; self::$action = $action = isset($params[3]) ? $this->parseAction($params[3]) : 'index'; self::$params = $controllerParams = array_slice($params, 4); $controllerClass = '\\' . $author . '\\' . $plugin . '\\Controllers\\' . $controller; if ($controllerObj = $this->findController($controllerClass, $action, plugins_path())) { return $controllerObj->run($action, $controllerParams); } } /* * Fall back on Cms controller */ return App::make('Cms\\Classes\\Controller')->run($url); }
/** * Checks whether a given URL matches a given pattern. * @param string $url The URL to check. * @param array $parameters A reference to a PHP array variable to return the parameter list fetched from URL. * @return boolean Returns true if the URL matches the pattern. Otherwise returns false. */ public function resolveUrl($url, &$parameters) { $parameters = []; $patternSegments = $this->segments; $patternSegmentNum = count($patternSegments); $urlSegments = Helper::segmentizeUrl($url); /* * Only one wildcard can be used, if found, pull out the excess segments */ if ($this->wildSegmentCount === 1) { $wildSegments = $this->captureWildcardSegments($urlSegments); } /* * If the number of URL segments is more than the number of pattern segments - return false */ if (count($urlSegments) > count($patternSegments)) { return false; } /* * Compare pattern and URL segments */ foreach ($patternSegments as $index => $patternSegment) { $patternSegmentLower = mb_strtolower($patternSegment); if (strpos($patternSegment, ':') !== 0) { /* * Static segment */ if (!array_key_exists($index, $urlSegments) || $patternSegmentLower != mb_strtolower($urlSegments[$index])) { return false; } } else { /* * Dynamic segment. Initialize the parameter */ $paramName = Helper::getParameterName($patternSegment); $parameters[$paramName] = false; /* * Determine whether it is optional */ $optional = Helper::segmentIsOptional($patternSegment); /* * Check if the optional segment has no required segments following it */ if ($optional && $index < $patternSegmentNum - 1) { for ($i = $index + 1; $i < $patternSegmentNum; $i++) { if (!Helper::segmentIsOptional($patternSegments[$i])) { $optional = false; break; } } } /* * If the segment is optional and there is no corresponding value in the URL, assign the default value (if provided) * and skip to the next segment. */ $urlSegmentExists = array_key_exists($index, $urlSegments); if ($optional && !$urlSegmentExists) { $parameters[$paramName] = Helper::getSegmentDefaultValue($patternSegment); continue; } /* * If the segment is not optional and there is no corresponding value in the URL, return false */ if (!$optional && !$urlSegmentExists) { return false; } /* * Validate the value with the regular expression */ $regexp = Helper::getSegmentRegExp($patternSegment); if ($regexp) { try { if (!preg_match($regexp, $urlSegments[$index])) { return false; } } catch (\Exception $ex) { } } /* * Set the parameter value */ $parameters[$paramName] = $urlSegments[$index]; /* * Determine if wildcard and add stored paramters as a suffix */ if (Helper::segmentIsWildcard($patternSegment) && count($wildSegments)) { $parameters[$paramName] .= Helper::rebuildUrl($wildSegments); } } } return true; }