public static function url($url = null, $full = false)
{
if (!static::$initialized) {
static::_loadRoutes();
}
$params = array('plugin' => null, 'controller' => null, 'action' => 'index');
if (is_bool($full)) {
$escape = false;
} else {
extract($full + array('escape' => false, 'full' => false));
}
$path = array('base' => null);
if (!empty(static::$_requests)) {
$request = static::$_requests[count(static::$_requests) - 1];
$params = $request->params;
$path = array('base' => $request->base, 'here' => $request->here);
}
if (empty($path['base'])) {
$path['base'] = Configure::read('App.base');
}
$base = $path['base'];
$extension = $output = $q = $frag = null;
if (empty($url)) {
$output = isset($path['here']) ? $path['here'] : '/';
if ($full) {
$output = static::fullBaseUrl() . $output;
}
return $output;
} elseif (is_array($url)) {
if (isset($url['base']) && $url['base'] === false) {
$base = null;
unset($url['base']);
}
if (isset($url['full_base']) && $url['full_base'] === true) {
$full = true;
unset($url['full_base']);
}
if (isset($url['?'])) {
$q = $url['?'];
unset($url['?']);
}
if (isset($url['#'])) {
$frag = '#' . $url['#'];
unset($url['#']);
}
if (isset($url['ext'])) {
$extension = '.' . $url['ext'];
unset($url['ext']);
}
if (empty($url['action'])) {
if (empty($url['controller']) || $params['controller'] === $url['controller']) {
$url['action'] = $params['action'];
} else {
$url['action'] = 'index';
}
}
$prefixExists = array_intersect_key($url, array_flip(static::$_prefixes));
foreach (static::$_prefixes as $prefix) {
if (!empty($params[$prefix]) && !$prefixExists) {
$url[$prefix] = true;
} elseif (isset($url[$prefix]) && !$url[$prefix]) {
unset($url[$prefix]);
}
if (isset($url[$prefix]) && strpos($url['action'], $prefix . '_') === 0) {
$url['action'] = substr($url['action'], strlen($prefix) + 1);
}
}
$url += array('controller' => $params['controller'], 'plugin' => $params['plugin']);
$match = false;
foreach (static::$routes as $route) {
$originalUrl = $url;
$url = $route->persistParams($url, $params);
if ($match = $route->match($url)) {
// CUSTOMIZE MODIFY 2016/10/27 ryuring
// 末尾のスラッシュを削除しないよう調整
// スラッシュを削除すると階層が変わってしまい、CSS等のリンク切れを引き起こしてしまう為
// >>>
//$output = trim($match, '/');
// ---
if (get_class($route) == 'BcContentsRoute') {
$output = ltrim($match, '/');
} else {
$output = trim($match, '/');
}
// <<<
break;
}
$url = $originalUrl;
}
if ($match === false) {
$output = static::_handleNoRoute($url);
}
} else {
if (preg_match('/^([a-z][a-z0-9.+\\-]+:|:?\\/\\/|[#?])/i', $url)) {
return $url;
}
if (substr($url, 0, 1) === '/') {
$output = substr($url, 1);
} else {
foreach (static::$_prefixes as $prefix) {
if (isset($params[$prefix])) {
$output .= $prefix . '/';
break;
}
}
if (!empty($params['plugin']) && $params['plugin'] !== $params['controller']) {
$output .= Inflector::underscore($params['plugin']) . '/';
}
$output .= Inflector::underscore($params['controller']) . '/' . $url;
}
}
$protocol = preg_match('#^[a-z][a-z0-9+\\-.]*\\://#i', $output);
if ($protocol === 0) {
$output = str_replace('//', '/', $base . '/' . $output);
if ($full) {
$output = static::fullBaseUrl() . $output;
}
if (!empty($extension)) {
$output = rtrim($output, '/');
}
}
return $output . $extension . static::queryString($q, array(), $escape) . $frag;
}