mirror of
https://github.com/Oreolek/kohana-multilang.git
synced 2024-05-02 17:19:23 +03:00
commit
d4e7d8d246
|
@ -110,8 +110,6 @@ class Multilang_Route extends Kohana_Route {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Altered method to handle multilingual uris.
|
|
||||||
*
|
|
||||||
* Generates a URI for the current route based on the parameters given.
|
* Generates a URI for the current route based on the parameters given.
|
||||||
*
|
*
|
||||||
* // Using the "default" route: "users/profile/10"
|
* // Using the "default" route: "users/profile/10"
|
||||||
|
@ -121,8 +119,7 @@ class Multilang_Route extends Kohana_Route {
|
||||||
* 'id' => '10'
|
* 'id' => '10'
|
||||||
* ));
|
* ));
|
||||||
*
|
*
|
||||||
* @param array URI parameters
|
* @param array $params URI parameters
|
||||||
* @param string $lang a language code
|
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Kohana_Exception
|
* @throws Kohana_Exception
|
||||||
* @uses Route::REGEX_Key
|
* @uses Route::REGEX_Key
|
||||||
|
@ -135,12 +132,125 @@ class Multilang_Route extends Kohana_Route {
|
||||||
$params['lang'] = ($lang === NULL ? $this->lang : $lang);
|
$params['lang'] = ($lang === NULL ? $this->lang : $lang);
|
||||||
}
|
}
|
||||||
|
|
||||||
$uri = parent::uri($params);
|
// Start with the routed URI
|
||||||
// If it's the default route, we add a trailing slash
|
$uri = $this->_uri;
|
||||||
if(Route::name($this) === 'default')
|
|
||||||
|
if (strpos($uri, '<') === FALSE AND strpos($uri, '(') === FALSE)
|
||||||
{
|
{
|
||||||
$uri .= '/';
|
// This is a static route, no need to replace anything
|
||||||
|
|
||||||
|
if ( ! $this->is_external())
|
||||||
|
return $uri;
|
||||||
|
|
||||||
|
// If the localhost setting does not have a protocol
|
||||||
|
if (strpos($this->_defaults['host'], '://') === FALSE)
|
||||||
|
{
|
||||||
|
// Use the default defined protocol
|
||||||
|
$params['host'] = Route::$default_protocol.$this->_defaults['host'];
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Use the supplied host with protocol
|
||||||
|
$params['host'] = $this->_defaults['host'];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compile the final uri and return it
|
||||||
|
return rtrim($params['host'], '/').'/'.$uri;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Keep track of whether an optional param was replaced
|
||||||
|
$provided_optional = FALSE;
|
||||||
|
|
||||||
|
while (preg_match('#\([^()]++\)#', $uri, $match))
|
||||||
|
{
|
||||||
|
|
||||||
|
// Search for the matched value
|
||||||
|
$search = $match[0];
|
||||||
|
|
||||||
|
// Remove the parenthesis from the match as the replace
|
||||||
|
$replace = substr($match[0], 1, -1);
|
||||||
|
|
||||||
|
while (preg_match('#'.Route::REGEX_KEY.'#', $replace, $match))
|
||||||
|
{
|
||||||
|
list($key, $param) = $match;
|
||||||
|
|
||||||
|
if (isset($params[$param]) AND $params[$param] !== Arr::get($this->_defaults, $param))
|
||||||
|
{
|
||||||
|
// Future optional params should be required
|
||||||
|
$provided_optional = TRUE;
|
||||||
|
|
||||||
|
// Replace the key with the parameter value
|
||||||
|
$replace = str_replace($key, $params[$param], $replace);
|
||||||
|
}
|
||||||
|
elseif ($provided_optional)
|
||||||
|
{
|
||||||
|
// Look for a default
|
||||||
|
if (isset($this->_defaults[$param]))
|
||||||
|
{
|
||||||
|
$replace = str_replace($key, $this->_defaults[$param], $replace);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Ungrouped parameters are required
|
||||||
|
throw new Kohana_Exception('Required route parameter not passed: :param', array(
|
||||||
|
':param' => $param,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// This group has missing parameters
|
||||||
|
$replace = '';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace the group in the URI
|
||||||
|
$uri = str_replace($search, $replace, $uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
while (preg_match('#'.Route::REGEX_KEY.'#', $uri, $match))
|
||||||
|
{
|
||||||
|
list($key, $param) = $match;
|
||||||
|
|
||||||
|
if ( ! isset($params[$param]))
|
||||||
|
{
|
||||||
|
// Look for a default
|
||||||
|
if (isset($this->_defaults[$param]))
|
||||||
|
{
|
||||||
|
$params[$param] = $this->_defaults[$param];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Ungrouped parameters are required
|
||||||
|
throw new Kohana_Exception('Required route parameter not passed: :param', array(
|
||||||
|
':param' => $param,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$uri = str_replace($key, $params[$param], $uri);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim all extra slashes from the URI
|
||||||
|
//$uri = preg_replace('#//+#', '/', rtrim($uri, '/'));
|
||||||
|
$uri = preg_replace('#//+#', '/', $uri);
|
||||||
|
|
||||||
|
if ($this->is_external())
|
||||||
|
{
|
||||||
|
// Need to add the host to the URI
|
||||||
|
$host = $this->_defaults['host'];
|
||||||
|
|
||||||
|
if (strpos($host, '://') === FALSE)
|
||||||
|
{
|
||||||
|
// Use the default defined protocol
|
||||||
|
$host = Route::$default_protocol.$host;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clean up the host and prepend it to the URI
|
||||||
|
$uri = rtrim($host, '/').'/'.$uri;
|
||||||
|
}
|
||||||
|
|
||||||
return $uri;
|
return $uri;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue