Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for base url #180

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/UriLocalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(LanguageRepository $languageRepository, Request $req
public function localeFromRequest($segment = 0)
{
$url = $this->request->getUri();
$url = $this->removeBaseUrl($url);
return $this->getLocaleFromUrl($url, $segment);
}

Expand All @@ -40,6 +41,7 @@ public function localeFromRequest($segment = 0)
*/
public function localize($url, $locale, $segment = 0)
{
$url = $this->removeBaseUrl($url);
$cleanUrl = $this->cleanUrl($url, $segment);
$parsedUrl = $this->parseUrl($cleanUrl, $segment);

Expand All @@ -59,6 +61,7 @@ public function localize($url, $locale, $segment = 0)
*/
public function getLocaleFromUrl($url, $segment = 0)
{
$url = $this->removeBaseUrl($url);
return $this->parseUrl($url, $segment)['locale'];
}

Expand All @@ -72,6 +75,7 @@ public function getLocaleFromUrl($url, $segment = 0)
*/
public function cleanUrl($url, $segment = 0)
{
$url = $this->removeBaseUrl($url);
$parsedUrl = $this->parseUrl($url, $segment);
// Remove locale from segments:
if ($parsedUrl['locale']) {
Expand Down Expand Up @@ -141,4 +145,14 @@ protected function removeTrailingSlash($path)
{
return strlen($path) > 0 && substr($path, -1) === '/' ? substr($path, 0, -1) : $path;
}

/**
* Remove base URL from URL string
*
* @param string $url
* @return string
*/
protected function removeBaseUrl($url) {
return str_ireplace(\URL::to('/'), "", rtrim($url, '/')) ?: '/';
}
}