1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Foundation\Overrides\Illuminate\Routing; |
6
|
|
|
|
7
|
|
|
use Illuminate\Routing\UrlGenerator as BaseUrlGenerator; |
8
|
|
|
|
9
|
|
|
class UrlGenerator extends BaseUrlGenerator |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Generate an absolute URL to the given path. |
13
|
|
|
* |
14
|
|
|
* @param string $path |
15
|
|
|
* @param mixed $extra |
16
|
|
|
* @param bool|null $secure |
17
|
|
|
* |
18
|
|
|
* @return string |
19
|
|
|
*/ |
20
|
|
|
public function to($path, $extra = [], $secure = null): string |
21
|
|
|
{ |
22
|
|
|
if (! config('cortex.foundation.route.trailing_slash')) { |
23
|
|
|
return parent::to($path, $extra, $secure); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// First we will check if the URL is already a valid URL. If it is we will not |
27
|
|
|
// try to generate a new one but will simply return the URL as is, which is |
28
|
|
|
// convenient since developers do not always have to check if it's valid. |
29
|
|
|
if ($this->isValidUrl($path)) { |
30
|
|
|
return $path; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$tail = implode('/', array_map( |
34
|
|
|
'rawurlencode', (array) $this->formatParameters($extra)) |
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
// Once we have the scheme we will compile the "tail" by collapsing the values |
38
|
|
|
// into a single string delimited by slashes. This just makes it convenient |
39
|
|
|
// for passing the array of parameters to this URL as a list of segments. |
40
|
|
|
$root = $this->formatRoot($this->formatScheme($secure)); |
41
|
|
|
|
42
|
|
|
[$path, $query] = $this->extractQueryString($path); |
|
|
|
|
43
|
|
|
|
44
|
|
|
return $this->format( |
45
|
|
|
$root, '/'.trim($path.'/'.$tail, '/') |
46
|
|
|
).'/'.$query; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
protected function routeUrl() |
53
|
|
|
{ |
54
|
|
|
if (config('cortex.foundation.route.trailing_slash') && ! $this->routeGenerator) { |
55
|
|
|
$this->routeGenerator = new RouteUrlGenerator($this, $this->request); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return parent::routeUrl(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function previous($fallback = false) |
65
|
|
|
{ |
66
|
|
|
return ($previousUrl = $this->request->input('previous_url')) ? $this->to($previousUrl) : parent::previous($fallback); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
protected function toRoute($route, $parameters, $absolute) |
73
|
|
|
{ |
74
|
|
|
// Bind {locale} route parameter |
75
|
|
|
if (config('cortex.foundation.route.locale_prefix') && in_array('locale', $route->parameterNames()) && ! isset($parameters['locale'])) { |
76
|
|
|
$urlLocale = $this->request->segment(1); |
77
|
|
|
$sessionLocale = session('locale', $defaultLocale = app('laravellocalization')->getCurrentLocale()); |
78
|
|
|
$parameters['locale'] = app('laravellocalization')->checkLocaleInSupportedLocales($urlLocale) ? $urlLocale |
79
|
|
|
: (app('laravellocalization')->checkLocaleInSupportedLocales($sessionLocale) ? $sessionLocale : $defaultLocale); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Bind {subdomain} route parameter |
83
|
|
|
if (in_array('subdomain', $route->parameterNames()) && ! isset($parameters['subdomain'])) { |
84
|
|
|
$parameters['subdomain'] = $route->hasParameter('subdomain') ? $route->parameter('subdomain') : explode('.', $this->request->getHost())[0]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->routeUrl()->to( |
88
|
|
|
$route, $this->formatParameters($parameters), $absolute |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.