-
Notifications
You must be signed in to change notification settings - Fork 34
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
fix locale url change, from default locale to other locale #105
Changes from 7 commits
518aea9
973d0ef
963895c
d464723
966a7cb
eecdcb9
4ec2f8d
2b47ecc
bf8e937
0abec27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,7 @@ | |
use SlmLocale\LocaleEvent; | ||
use Zend\Router\Http\TreeRouteStack; | ||
use Zend\Router\SimpleRouteStack; | ||
use Zend\Stdlib\RequestInterface; | ||
use Zend\Uri\Uri; | ||
|
||
class UriPathStrategy extends AbstractStrategy | ||
|
@@ -103,7 +104,7 @@ public function detect(LocaleEvent $event) | |
return; | ||
} | ||
|
||
$base = $this->getBasePath(); | ||
$base = $this->getBasePath($request); | ||
$locale = $this->getFirstSegmentInPath($request->getUri(), $base); | ||
if (! $locale) { | ||
return; | ||
|
@@ -144,7 +145,7 @@ public function found(LocaleEvent $event) | |
} | ||
} | ||
|
||
$base = $this->getBasePath(); | ||
$base = $this->getBasePath($request); | ||
$found = $this->getFirstSegmentInPath($request->getUri(), $base); | ||
|
||
if ($this->router instanceof TreeRouteStack) { | ||
|
@@ -180,7 +181,7 @@ public function found(LocaleEvent $event) | |
public function assemble(LocaleEvent $event) | ||
{ | ||
$uri = $event->getUri(); | ||
$base = $this->getBasePath(); | ||
$base = $this->getBasePath($event->getRequest()); | ||
$locale = $event->getLocale(); | ||
|
||
if (! $this->redirectToCanonical() && null !== $this->getAliases()) { | ||
|
@@ -193,30 +194,66 @@ public function assemble(LocaleEvent $event) | |
$path = $uri->getPath(); | ||
|
||
// Last part of base is now always locale, remove that | ||
$parts = explode('/', trim($base, '/')); | ||
array_pop($parts); | ||
$parts = explode('/', trim($base, '/')); | ||
$lastElement = count($parts) - 1; | ||
|
||
$removeFirstLocale = true; | ||
if (null !== $this->default && | ||
isset($parts[$lastElement]) && | ||
! in_array($parts[$lastElement], $event->getSupported(), true) && | ||
$parts[$lastElement] !== $this->default | ||
) { | ||
$removeFirstLocale = false; | ||
} | ||
|
||
if (true === $removeFirstLocale) { | ||
// Remove first part | ||
array_pop($parts); | ||
} | ||
|
||
$base = implode('/', $parts); | ||
|
||
if ($base) { | ||
$path = substr($path, strlen($base)); | ||
$path = substr(trim($path, '/'), strlen($base)); | ||
} | ||
$parts = explode('/', trim($path, '/')); | ||
|
||
// Remove first part | ||
array_shift($parts); | ||
$parts = explode('/', trim($path, '/')); | ||
if (true === $removeFirstLocale) { | ||
// Remove first part | ||
array_shift($parts); | ||
} | ||
|
||
if ($locale === $this->default) { | ||
$locale = ''; | ||
} else { | ||
$locale .= '/'; | ||
} | ||
|
||
$path = $base . '/' . $locale . implode('/', $parts); | ||
$path = ($base ? '/' : '') . trim($base, '/') . '/' . $locale . implode('/', $parts); | ||
$uri->setPath($path); | ||
|
||
return $uri; | ||
} | ||
|
||
/** | ||
* @return SimpleRouteStack | ||
*/ | ||
public function getRouter() | ||
{ | ||
return $this->router; | ||
} | ||
|
||
/** | ||
* @param SimpleRouteStack $router | ||
* @return $this | ||
*/ | ||
public function setRouter($router) | ||
{ | ||
$this->router = $router; | ||
|
||
return $this; | ||
} | ||
|
||
protected function getFirstSegmentInPath(Uri $uri, $base = null) | ||
{ | ||
$path = $uri->getPath(); | ||
|
@@ -240,12 +277,21 @@ protected function getAliasForLocale($locale) | |
} | ||
} | ||
|
||
protected function getBasePath() | ||
/** | ||
* @param RequestInterface|null $request | ||
* @return string|null | ||
*/ | ||
protected function getBasePath(RequestInterface $request = null) | ||
{ | ||
$result = null; | ||
if ($this->router instanceof TreeRouteStack) { | ||
return $this->router->getBaseUrl(); | ||
$result = $this->router->getBaseUrl(); | ||
} | ||
|
||
if (null === $result && null !== $request && method_exists($request, 'getBasePath')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why router does not have baseUrl? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check #40 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then we can remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes and no, to get it from the router is much better, as to use it from the request, should just be a fallback. |
||
$result = $request->getBasePath(); | ||
} | ||
|
||
return null; | ||
return $result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,8 +127,7 @@ public function testDetectWithBaseUrlReturnsRightPartOfPath() | |
$this->event->setResponse(new HttpResponse()); | ||
|
||
$locale = $this->strategy->detect($this->event); | ||
$expected = 'en'; | ||
$this->assertEquals($expected, $locale); | ||
$this->assertSame('en', $locale); | ||
} | ||
|
||
public function testFoundRedirectsByDefault() | ||
|
@@ -391,6 +390,7 @@ public function testAssembleWithDefault() | |
$this->event->setLocale('en'); | ||
$this->event->setUri($uri); | ||
|
||
$this->strategy->getRouter()->setBaseUrl('/nl'); | ||
$this->strategy->setOptions([ | ||
'default' => 'en', | ||
]); | ||
|
@@ -409,15 +409,13 @@ public function testAssembleWithDefaultNotMatching() | |
$this->event->setLocale('en'); | ||
$this->event->setUri($uri); | ||
|
||
$this->strategy->getRouter()->setBaseUrl('/nl'); | ||
$this->strategy->setOptions([ | ||
'default' => 'fr', | ||
]); | ||
$this->strategy->assemble($this->event); | ||
|
||
$expected = '/en/foo/bar/baz'; | ||
$actual = $this->event->getUri()->getPath(); | ||
|
||
$this->assertSame($expected, $actual); | ||
$this->assertSame('/en/foo/bar/baz', $this->event->getUri()->getPath()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. strategies should not change base path. Should have been There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but also it is base path @basz am I right here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i will add a test for this (when im home) Document_root There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added 2 tests for this, with default language to other language and without default language There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. install your app in '/var/www/my-app' thats all There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so as I understand baseurl is Do I still not understand something? please rename your public folder so mething like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added more tests, like if you rename your public directory to i also added the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I looked deeper and now understand why you append language and change base path. As I understand that because you testing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added tests for found |
||
} | ||
|
||
public function testDisableUriPathStrategyPhpunit() | ||
|
@@ -442,6 +440,21 @@ public function testDisableUriPathStrategyPhpunit() | |
$_SERVER['DISABLE_URIPATHSTRATEGY'] = false; | ||
} | ||
|
||
public function testAssembleWithDefaultMatchingCurrent() | ||
{ | ||
$uri = new Uri('/foo/bar/baz'); | ||
|
||
$this->event->setLocale('en'); | ||
$this->event->setUri($uri); | ||
|
||
$this->strategy->setOptions([ | ||
'default' => 'fr', | ||
]); | ||
$this->strategy->assemble($this->event); | ||
|
||
$this->assertSame('/en/foo/bar/baz', $this->event->getUri()->getPath()); | ||
} | ||
|
||
protected function getPluginManager($console = false) | ||
{ | ||
$sl = new ServiceManager(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't add gettters/setters we already have it in the constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, i changed it in tests too