Skip to content

Commit

Permalink
Added language prefix to route() method
Browse files Browse the repository at this point in the history
  • Loading branch information
grinry committed Sep 3, 2015
1 parent 597161d commit ddbd9c8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ Now, try creating hyperlinks:

<?= URL::to('main/index'); ?> //or
<?= url('main/index'); ?> // will create route to /en/main/index (it will use default language as prefix)
<?= URL::to('main/index', ['language'=>'ru']) // will create create route/change default language to /ru/main/index
<?= URL::to('main/index', ['language'=>'ru']) // will create route/change default language to /ru/main/index
<?= route('profile') // will create url to assigned route with language prefix
<?= route('profile', ['language'=>'ru']) // will create url to route/change default language to /ru/main/index
You can access current language like before:

App::getLocale();
Expand Down
27 changes: 27 additions & 0 deletions UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,31 @@ private function toUrl($path, $extra = [], $secure = null)

return $this->trimUrl($root, $path, $tail);
}

/**
* @param \Illuminate\Routing\Route $route
* @param mixed $parameters
* @param bool $absolute
* @return string
*/
protected function toRoute($route, $parameters, $absolute)
{
$parameters = $this->formatParameters($parameters);

$language = App::getLocale();
if (array_key_exists('language', $parameters)) {
$language = $parameters['language'];
unset($parameters['language']);
}

$domain = $this->getRouteDomain($route, $parameters);

$uri = strtr(rawurlencode($this->addQueryString($this->trimUrl(
$root = $this->replaceRoot($route, $domain, $parameters),
$language,
$this->replaceRouteParameters($route->uri(), $parameters)
), $parameters)), $this->dontEncode);

return $absolute ? $uri : '/'.ltrim(str_replace($root, '', $uri), '/');
}
}

0 comments on commit ddbd9c8

Please sign in to comment.