13
13
use Magento \Framework \HTTP \PhpEnvironment \Response ;
14
14
use Laminas \Http \Response \Stream ;
15
15
use Magento \UpwardConnector \Model \Prerender ;
16
+ use Magento \Store \Model \StoreManagerInterface ;
17
+ use Magento \Framework \UrlInterface ;
18
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
19
+ use Magento \Store \Model \ScopeInterface ;
16
20
17
21
class Upward implements FrontControllerInterface
18
22
{
@@ -31,20 +35,44 @@ class Upward implements FrontControllerInterface
31
35
*/
32
36
private $ prerender ;
33
37
38
+ /**
39
+ * @var StoreManagerInterface
40
+ */
41
+ private $ storeManager ;
42
+
43
+ /**
44
+ * @var \Magento\Framework\UrlInterface
45
+ */
46
+ private $ url ;
47
+
48
+ /**
49
+ * @var \Magento\Framework\App\Config\ScopeConfigInterface
50
+ */
51
+ private $ scopeConfig ;
52
+
34
53
/**
35
54
* Upward constructor.
36
55
* @param Response $response
37
56
* @param UpwardControllerFactory $upwardFactory
38
57
* @param Prerender $prerender
58
+ * @param StoreManagerInterface $storeManager
59
+ * @param UrlInterface $url
60
+ * @param ScopeConfigInterface $scopeConfig
39
61
*/
40
62
public function __construct (
41
63
Response $ response ,
42
64
UpwardControllerFactory $ upwardFactory ,
43
- Prerender $ prerender
65
+ Prerender $ prerender ,
66
+ StoreManagerInterface $ storeManager ,
67
+ UrlInterface $ url ,
68
+ ScopeConfigInterface $ scopeConfig
44
69
) {
45
70
$ this ->response = $ response ;
46
71
$ this ->upwardFactory = $ upwardFactory ;
47
72
$ this ->prerender = $ prerender ;
73
+ $ this ->storeManager = $ storeManager ;
74
+ $ this ->url = $ url ;
75
+ $ this ->scopeConfig = $ scopeConfig ;
48
76
}
49
77
50
78
/**
@@ -55,6 +83,24 @@ public function __construct(
55
83
*/
56
84
public function dispatch (RequestInterface $ request )
57
85
{
86
+ $ baseUrl = $ this ->storeManager ->getStore ()->getBaseUrl (
87
+ UrlInterface::URL_TYPE_WEB ,
88
+ $ this ->storeManager ->getStore ()->isCurrentlySecure ()
89
+ );
90
+ // phpcs:disable Magento2.Functions.DiscouragedFunction
91
+ $ uri = parse_url ($ baseUrl );
92
+ if ($ request ->getUri ()->getHost () !== $ uri ['host ' ]) {
93
+ $ redirectUrl = $ this ->url ->getRedirectUrl (
94
+ $ this ->url ->getDirectUrl (ltrim ($ request ->getPathInfo (), '/ ' ), ['_nosid ' => true ])
95
+ );
96
+ $ redirectCode = (int )$ this ->scopeConfig ->getValue (
97
+ 'web/url/redirect_to_base ' ,
98
+ ScopeInterface::SCOPE_STORE
99
+ ) !== 301 ? 302 : 301 ;
100
+ $ this ->response ->setRedirect ($ redirectUrl , $ redirectCode );
101
+ return $ this ->response ;
102
+ }
103
+
58
104
$ prerenderedResponse = null ;
59
105
if ($ this ->prerender ->shouldShowPrerenderedPage ($ request )) {
60
106
/** @var \Laminas\Http\Response $prerenderedResponse */
0 commit comments