5
5
*/
6
6
namespace Magento \UrlRewrite \Controller ;
7
7
8
+ use Magento \Framework \App \RequestInterface ;
8
9
use Magento \UrlRewrite \Controller \Adminhtml \Url \Rewrite ;
9
10
use Magento \UrlRewrite \Model \OptionProvider ;
10
11
use Magento \UrlRewrite \Model \UrlFinderInterface ;
11
12
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
13
+ use Magento \Framework \App \Request \Http as HttpRequest ;
14
+ use Magento \Framework \App \Response \Http as HttpResponse ;
15
+ use Magento \Framework \UrlInterface ;
16
+ use Magento \Framework \App \Action \Redirect ;
17
+ use Magento \Framework \App \ActionInterface ;
12
18
13
19
/**
14
20
* UrlRewrite Controller Router
@@ -23,7 +29,7 @@ class Router implements \Magento\Framework\App\RouterInterface
23
29
protected $ actionFactory ;
24
30
25
31
/**
26
- * @var \Magento\Framework\ UrlInterface
32
+ * @var UrlInterface
27
33
*/
28
34
protected $ url ;
29
35
@@ -33,7 +39,7 @@ class Router implements \Magento\Framework\App\RouterInterface
33
39
protected $ storeManager ;
34
40
35
41
/**
36
- * @var \Magento\Framework\App\ResponseInterface
42
+ * @var HttpResponse
37
43
*/
38
44
protected $ response ;
39
45
@@ -44,14 +50,14 @@ class Router implements \Magento\Framework\App\RouterInterface
44
50
45
51
/**
46
52
* @param \Magento\Framework\App\ActionFactory $actionFactory
47
- * @param \Magento\Framework\ UrlInterface $url
53
+ * @param UrlInterface $url
48
54
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
49
55
* @param \Magento\Framework\App\ResponseInterface $response
50
56
* @param UrlFinderInterface $urlFinder
51
57
*/
52
58
public function __construct (
53
59
\Magento \Framework \App \ActionFactory $ actionFactory ,
54
- \ Magento \ Framework \ UrlInterface $ url ,
60
+ UrlInterface $ url ,
55
61
\Magento \Store \Model \StoreManagerInterface $ storeManager ,
56
62
\Magento \Framework \App \ResponseInterface $ response ,
57
63
UrlFinderInterface $ urlFinder
@@ -64,48 +70,83 @@ public function __construct(
64
70
}
65
71
66
72
/**
67
- * Match corresponding URL Rewrite and modify request
73
+ * Match corresponding URL Rewrite and modify request.
68
74
*
69
- * @param \Magento\Framework\App\RequestInterface $request
70
- * @return \Magento\Framework\App\ActionInterface|null
75
+ * @param RequestInterface|HttpRequest $request
76
+ *
77
+ * @return ActionInterface|null
71
78
*/
72
- public function match (\ Magento \ Framework \ App \ RequestInterface $ request )
79
+ public function match (RequestInterface $ request )
73
80
{
74
81
if ($ fromStore = $ request ->getParam ('___from_store ' )) {
82
+ //If we're in the process of switching stores then matching rewrite
83
+ //rule from previous store because the URL was not changed yet from
84
+ //old store's format.
75
85
$ oldStoreId = $ this ->storeManager ->getStore ($ fromStore )->getId ();
76
- $ oldRewrite = $ this ->getRewrite ($ request ->getPathInfo (), $ oldStoreId );
77
- if ($ oldRewrite ) {
78
- $ rewrite = $ this ->urlFinder ->findOneByData (
86
+ $ oldRewrite = $ this ->getRewrite (
87
+ $ request ->getPathInfo (),
88
+ $ oldStoreId
89
+ );
90
+ if ($ oldRewrite && $ oldRewrite ->getRedirectType () === 0 ) {
91
+ //If there is a match and it's a correct URL then just
92
+ //redirecting to current store's URL equivalent,
93
+ //otherwise just continuing finding a rule within current store.
94
+ $ currentRewrite = $ this ->urlFinder ->findOneByData (
79
95
[
80
96
UrlRewrite::ENTITY_TYPE => $ oldRewrite ->getEntityType (),
81
97
UrlRewrite::ENTITY_ID => $ oldRewrite ->getEntityId (),
82
- UrlRewrite::STORE_ID => $ this ->storeManager ->getStore ()->getId (),
83
- UrlRewrite::IS_AUTOGENERATED => 1 ,
98
+ UrlRewrite::STORE_ID =>
99
+ $ this ->storeManager ->getStore ()->getId (),
100
+ UrlRewrite::REDIRECT_TYPE => 0 ,
84
101
]
85
102
);
86
- if ($ rewrite && $ rewrite ->getRequestPath () !== $ oldRewrite ->getRequestPath ()) {
87
- return $ this ->redirect ($ request , $ rewrite ->getRequestPath (), OptionProvider::TEMPORARY );
103
+ if ($ currentRewrite
104
+ && $ currentRewrite ->getRequestPath ()
105
+ !== $ oldRewrite ->getRequestPath ()
106
+ ) {
107
+ return $ this ->redirect (
108
+ $ request ,
109
+ $ this ->url ->getUrl (
110
+ '' ,
111
+ ['_direct ' => $ currentRewrite ->getRequestPath ()]
112
+ ),
113
+ OptionProvider::TEMPORARY
114
+ );
88
115
}
89
116
}
90
117
}
91
- $ rewrite = $ this ->getRewrite ($ request ->getPathInfo (), $ this ->storeManager ->getStore ()->getId ());
118
+
119
+ $ rewrite = $ this ->getRewrite (
120
+ $ request ->getPathInfo (),
121
+ $ this ->storeManager ->getStore ()->getId ()
122
+ );
123
+
92
124
if ($ rewrite === null ) {
125
+ //No rewrite rule matching current URl found, continuing with
126
+ //processing of this URL.
93
127
return null ;
94
128
}
95
-
96
129
if ($ rewrite ->getRedirectType ()) {
130
+ //Rule requires the request to be redirected to another URL
131
+ //and cannot be processed further.
97
132
return $ this ->processRedirect ($ request , $ rewrite );
98
133
}
99
-
100
- $ request ->setAlias (\Magento \Framework \UrlInterface::REWRITE_REQUEST_PATH_ALIAS , $ rewrite ->getRequestPath ());
134
+ //Rule provides actual URL that can be processed by a controller.
135
+ $ request ->setAlias (
136
+ UrlInterface::REWRITE_REQUEST_PATH_ALIAS ,
137
+ $ rewrite ->getRequestPath ()
138
+ );
101
139
$ request ->setPathInfo ('/ ' . $ rewrite ->getTargetPath ());
102
- return $ this ->actionFactory ->create (\Magento \Framework \App \Action \Forward::class);
140
+ return $ this ->actionFactory ->create (
141
+ \Magento \Framework \App \Action \Forward::class
142
+ );
103
143
}
104
144
105
145
/**
106
- * @param \Magento\Framework\App\ RequestInterface $request
146
+ * @param RequestInterface $request
107
147
* @param UrlRewrite $rewrite
108
- * @return \Magento\Framework\App\ActionInterface|null
148
+ *
149
+ * @return ActionInterface|null
109
150
*/
110
151
protected function processRedirect ($ request , $ rewrite )
111
152
{
@@ -119,16 +160,17 @@ protected function processRedirect($request, $rewrite)
119
160
}
120
161
121
162
/**
122
- * @param \Magento\Framework\App\ RequestInterface $request
163
+ * @param RequestInterface|HttpRequest $request
123
164
* @param string $url
124
165
* @param int $code
125
- * @return \Magento\Framework\App\ ActionInterface
166
+ * @return ActionInterface
126
167
*/
127
168
protected function redirect ($ request , $ url , $ code )
128
169
{
129
170
$ this ->response ->setRedirect ($ url , $ code );
130
171
$ request ->setDispatched (true );
131
- return $ this ->actionFactory ->create (\Magento \Framework \App \Action \Redirect::class);
172
+
173
+ return $ this ->actionFactory ->create (Redirect::class);
132
174
}
133
175
134
176
/**
0 commit comments