Skip to content

Commit

Permalink
Fix multi domain websites
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-kl1 committed Mar 18, 2024
1 parent fb4ca71 commit 8992f22
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
6 changes: 4 additions & 2 deletions Plugin/App/Request/PathInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,22 @@
namespace Opengento\StorePathUrl\Plugin\App\Request;

use Magento\Framework\App\Request\PathInfo as PathInfoSubject;
use Magento\Framework\App\RequestInterface;
use Opengento\StorePathUrl\Model\Config;
use Opengento\StorePathUrl\Service\StorePathFixer;

class PathInfo
{
public function __construct(
private Config $config,
private StorePathFixer $storePathFixer
private StorePathFixer $storePathFixer,
private RequestInterface $request
) {}

public function beforeGetPathInfo(PathInfoSubject $subject, string $requestUri, string $baseUrl): array
{
if ($this->config->isEnabled()) {
$requestUri = $this->storePathFixer->fix($baseUrl, $requestUri);
$requestUri = $this->storePathFixer->fix($baseUrl ? $requestUri : $this->request->getUriString(), $requestUri);
}

return [$requestUri, $baseUrl];
Expand Down
17 changes: 5 additions & 12 deletions Service/StorePathFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,24 @@
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\Store;

use function parse_url;
use function str_starts_with;

use const PHP_URL_PATH;

class StorePathFixer
{
public function __construct(
private StoreRepositoryInterface $storeRepository,
private UriUtils $uriUtils
) {}

/**
* Replace the request uri path with the store code, if the base url match any of the registered store base url.
*/
public function fix(string $baseUrl, string $requestUri): string
{
/** @var Store $store */
foreach ($this->storeRepository->getList() as $store) {
if ($store->getId()) {
if ($baseUrl === '') {
$path = parse_url($store->getBaseUrl(), PHP_URL_PATH);
if (str_starts_with($requestUri . '/', $path)) {
return $this->uriUtils->replacePathCode($requestUri, $store);
}
} elseif (str_starts_with($baseUrl . $requestUri, $store->getBaseUrl())) {
return $this->uriUtils->replacePathCode($requestUri, $store);
}
if ($store->getId() && str_starts_with($baseUrl, $store->getBaseUrl())) {
return $this->uriUtils->replacePathCode($requestUri, $store);
}
}

Expand Down

0 comments on commit 8992f22

Please sign in to comment.