1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* CakePHP Plugin : CakePHP Subdomain Routing |
4
|
|
|
* Copyright (c) Multidimension.al (http://multidimension.al) |
5
|
|
|
* Github : https://github.com/multidimension-al/cakephp-subdomains |
6
|
|
|
* |
7
|
|
|
* Licensed under The MIT License |
8
|
|
|
* For full copyright and license information, please see the LICENSE file |
9
|
|
|
* Redistributions of files must retain the above copyright notice. |
10
|
|
|
* |
11
|
|
|
* @copyright (c) Multidimension.al (http://multidimension.al) |
12
|
|
|
* @link https://github.com/multidimension-al/cakephp-subdomains Github |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Multidimensional\Subdomains\Routing\Route; |
17
|
|
|
|
18
|
|
|
use Cake\Network\Request; |
19
|
|
|
use Cake\Routing\Router; |
20
|
|
|
use Cake\Routing\Route\Route; |
21
|
|
|
use Multidimensional\Subdomains\Middleware\SubdomainMiddleware; |
22
|
|
|
|
23
|
|
|
class SubdomainRoute extends Route |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $url |
|
|
|
|
28
|
|
|
* @param string $method |
|
|
|
|
29
|
|
|
* @return bool|array |
|
|
|
|
30
|
|
|
*/ |
31
|
|
|
public function parse($url, $method = '') |
32
|
|
|
{ |
33
|
|
|
list($prefix) = $this->_getPrefixAndHost(); |
34
|
|
|
|
35
|
|
|
if (!$this->_checkPrefix($prefix)) { |
36
|
|
|
return false; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return parent::parse($url, $method); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $url |
|
|
|
|
44
|
|
|
* @param array $context |
|
|
|
|
45
|
|
|
* @return bool|array |
|
|
|
|
46
|
|
|
*/ |
47
|
|
|
public function match(array $url, array $context = []) |
48
|
|
|
{ |
49
|
|
|
if (!isset($url['prefix'])) { |
50
|
|
|
$url['prefix'] = null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!$this->_checkPrefix($url['prefix'])) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
list($prefix, $host) = $this->_getPrefixAndHost($context); |
58
|
|
|
|
59
|
|
|
if ($prefix !== $url['prefix']) { |
60
|
|
|
$url['_host'] = $url['prefix'] === 'false' ? $host : $url['prefix'] . '.' . $host; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return parent::match($url, $context); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $context |
|
|
|
|
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
private function _getPrefixAndHost(array $context = []) |
71
|
|
|
{ |
72
|
|
|
if (empty($context['_host'])) { |
73
|
|
|
$request = Router::getRequest(true) ?: Request::createFromGlobals(); |
74
|
|
|
$host = $request->host(); |
75
|
|
|
} else { |
76
|
|
|
$host = $context['_host']; |
77
|
|
|
} |
78
|
|
|
$subdomainMiddleware = new SubdomainMiddleware(); |
79
|
|
|
|
80
|
|
|
return $subdomainMiddleware->getPrefixAndHost($host); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $prefix |
|
|
|
|
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
private function _checkPrefix($prefix) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$routePrefix = isset($this->defaults['prefix']) ? $this->defaults['prefix'] : null; |
90
|
|
|
|
91
|
|
|
return $prefix === $routePrefix; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|