fomvasss /
laravel-url-aliases
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Fomvasss\UrlAliases\Middleware; |
||||||
| 4 | |||||||
| 5 | use Closure; |
||||||
| 6 | use Fomvasss\UrlAliases\UrlAliasLocalization; |
||||||
| 7 | use Illuminate\Http\Request; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 8 | |||||||
| 9 | class UrlAliasMiddleware |
||||||
| 10 | { |
||||||
| 11 | /** @var */ |
||||||
| 12 | protected $config; |
||||||
| 13 | |||||||
| 14 | /** @var bool */ |
||||||
| 15 | protected $useLocalization = false; |
||||||
| 16 | |||||||
| 17 | /** |
||||||
| 18 | * Handle an incoming request. |
||||||
| 19 | * |
||||||
| 20 | * @param \Illuminate\Http\Request $request |
||||||
| 21 | * @param \Closure $next |
||||||
| 22 | * @return mixed |
||||||
| 23 | */ |
||||||
| 24 | public function handle($request, Closure $next) |
||||||
| 25 | { |
||||||
| 26 | $this->app = app(); |
||||||
|
0 ignored issues
–
show
The function
app was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 27 | $this->config = $this->app['config']; |
||||||
| 28 | |||||||
| 29 | if ($this->isAvailableMethod($request) && $this->isAvailableCheckPath($request)) { |
||||||
| 30 | |||||||
| 31 | $path = $request->path(); |
||||||
| 32 | |||||||
| 33 | // Check localization support |
||||||
| 34 | if ($this->useLocalization = $this->config->get('url-aliases.use_localization') && $this->isAvailableLocalizationPath($request)) { |
||||||
|
0 ignored issues
–
show
|
|||||||
| 35 | $localization = $this->app->make(UrlAliasLocalization::class); |
||||||
| 36 | |||||||
| 37 | $localizationResult = $localization->prepareLocalizePath($request->path()); |
||||||
| 38 | |||||||
| 39 | if (isset($localizationResult['redirect'])) { |
||||||
| 40 | $params = count($request->all()) ? '?' . http_build_query($request->all()) : ''; |
||||||
| 41 | |||||||
| 42 | // Hide default locale in URL |
||||||
| 43 | return redirect()->to($localizationResult['redirect'] . $params, 302); |
||||||
|
0 ignored issues
–
show
The function
redirect was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 44 | } elseif (isset($localizationResult['path'])) { |
||||||
| 45 | $path = $localizationResult['path']; |
||||||
| 46 | } |
||||||
| 47 | } |
||||||
| 48 | |||||||
| 49 | $urlModels = $this->getByPath($path); |
||||||
| 50 | |||||||
| 51 | // If visited source - system path |
||||||
| 52 | if ($urlModel = $urlModels->where('source', $path)->where('type', null)->first()) { |
||||||
| 53 | |||||||
| 54 | $redirectStatus = $this->config->get('url-aliases.redirect_for_system_path', 301) == 301 ? 301 : 302; |
||||||
| 55 | |||||||
| 56 | // Redirect to alias path |
||||||
| 57 | $params = count($request->all()) ? '?' . http_build_query($request->all()) : ''; |
||||||
| 58 | |||||||
| 59 | if ($this->useLocalization) { |
||||||
| 60 | return redirect()->to(url($urlModel->localeAlias) . '/' . $params, $redirectStatus); |
||||||
|
0 ignored issues
–
show
The function
url was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 61 | } |
||||||
| 62 | |||||||
| 63 | return redirect()->to(url($urlModel->alias) . '/' . $params, $redirectStatus); |
||||||
| 64 | |||||||
| 65 | // If visited alias |
||||||
| 66 | } elseif ($urlModel = $urlModels->where('alias', $path)->where('locale', $this->app->getLocale())->where('type', null)->first()) { |
||||||
| 67 | |||||||
| 68 | $newRequest = $this->makeNewRequest($request, $urlModel); |
||||||
| 69 | |||||||
| 70 | return $next($newRequest); |
||||||
| 71 | |||||||
| 72 | // If custom redirection |
||||||
| 73 | } elseif ($urlModel = $urlModels->where('alias', $path)->where('type', '<>', null)->first()) { |
||||||
| 74 | |||||||
| 75 | return redirect(url($urlModel->source), $urlModel->type); |
||||||
| 76 | |||||||
| 77 | // Check if isset facet in current url and find aliased path without facet |
||||||
| 78 | } elseif ($customReturn = $this->customize($request, $next, $path)) { |
||||||
| 79 | |||||||
| 80 | return $customReturn; |
||||||
| 81 | } |
||||||
| 82 | } |
||||||
| 83 | |||||||
| 84 | return $next($request); |
||||||
| 85 | } |
||||||
| 86 | |||||||
| 87 | /** |
||||||
| 88 | * Remake request. |
||||||
| 89 | * |
||||||
| 90 | * @param Request $request |
||||||
| 91 | * @param $urlModel |
||||||
| 92 | * @return Request |
||||||
| 93 | */ |
||||||
| 94 | protected function makeNewRequest(Request $request, $urlModel, $getParams = []) |
||||||
| 95 | { |
||||||
| 96 | $newRequest = $request; |
||||||
| 97 | $newRequest->server->set('REQUEST_URI', $urlModel->source); |
||||||
| 98 | $newRequest->initialize( |
||||||
| 99 | $request->query->all(), |
||||||
| 100 | $request->request->all(), |
||||||
| 101 | $request->attributes->all(), |
||||||
| 102 | $request->cookies->all(), |
||||||
| 103 | $request->files->all(), |
||||||
| 104 | $newRequest->server->all() + ['ALIAS_REQUEST_URI' => $request->path(), 'ALIAS_ID' => $urlModel->id, 'ALIAS_LOCALE_BOUND' => $urlModel->locale_bound], |
||||||
| 105 | $request->getContent() |
||||||
| 106 | ); |
||||||
| 107 | |||||||
| 108 | $newRequest->merge($getParams); |
||||||
| 109 | |||||||
| 110 | return $newRequest; |
||||||
| 111 | } |
||||||
| 112 | |||||||
| 113 | /** |
||||||
| 114 | * @param $request |
||||||
| 115 | * @param \Closure $next |
||||||
| 116 | * @return mixed |
||||||
| 117 | */ |
||||||
| 118 | public function customize($request, Closure $next, $path) |
||||||
| 119 | { |
||||||
| 120 | $newRequest = $request; |
||||||
| 121 | $newRequest->server->set('REQUEST_URI', $path); |
||||||
| 122 | $newRequest->initialize( |
||||||
| 123 | $request->query->all(), |
||||||
| 124 | $request->request->all(), |
||||||
| 125 | $request->attributes->all(), |
||||||
| 126 | $request->cookies->all(), |
||||||
| 127 | $request->files->all(), |
||||||
| 128 | $newRequest->server->all(), |
||||||
| 129 | $request->getContent() |
||||||
| 130 | ); |
||||||
| 131 | |||||||
| 132 | //... |
||||||
| 133 | return $next($request); |
||||||
| 134 | } |
||||||
| 135 | |||||||
| 136 | /** |
||||||
| 137 | * TODO: IS DEPRECATED |
||||||
| 138 | * If type redirect - redirect. |
||||||
| 139 | * @param $urlModel |
||||||
| 140 | * @return bool|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
||||||
|
0 ignored issues
–
show
The type
Illuminate\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
The type
Illuminate\Routing\Redirector was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 141 | */ |
||||||
| 142 | protected function isTypeRedirect($urlModel) |
||||||
| 143 | { |
||||||
| 144 | if (in_array($urlModel->type, [301, 302])) { |
||||||
| 145 | if ($this->useLocalization) { |
||||||
| 146 | return redirect(url($urlModel->localeSource), $urlModel->type); |
||||||
|
0 ignored issues
–
show
The function
url was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
The function
redirect was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 147 | } |
||||||
| 148 | |||||||
| 149 | return redirect()->to(url($urlModel->source), $urlModel->type); |
||||||
| 150 | } |
||||||
| 151 | |||||||
| 152 | return false; |
||||||
| 153 | } |
||||||
| 154 | |||||||
| 155 | /** |
||||||
| 156 | * @param Request $request |
||||||
| 157 | * @return bool |
||||||
| 158 | */ |
||||||
| 159 | protected function isAvailableCheckPath(Request $request) |
||||||
| 160 | { |
||||||
| 161 | if ($request->is(...$this->config->get('url-aliases.ignored_paths', []))) { |
||||||
| 162 | return false; |
||||||
| 163 | } |
||||||
| 164 | |||||||
| 165 | return true; |
||||||
| 166 | } |
||||||
| 167 | |||||||
| 168 | protected function isAvailableLocalizationPath(Request $request) |
||||||
| 169 | { |
||||||
| 170 | if ($request->is(...$this->config->get('url-aliases-laravellocalization.urlsIgnored', []))) { |
||||||
| 171 | return false; |
||||||
| 172 | } |
||||||
| 173 | |||||||
| 174 | return true; |
||||||
| 175 | } |
||||||
| 176 | |||||||
| 177 | /** |
||||||
| 178 | * @param Request $request |
||||||
| 179 | * @return bool |
||||||
| 180 | */ |
||||||
| 181 | protected function isAvailableMethod(Request $request) |
||||||
| 182 | { |
||||||
| 183 | if (in_array($request->getMethod(), $this->config->get('url-aliases.available_methods', [])) || empty($this->config->get('url-aliases.available_methods', []))) { |
||||||
| 184 | return true; |
||||||
| 185 | } |
||||||
| 186 | |||||||
| 187 | return false; |
||||||
| 188 | } |
||||||
| 189 | |||||||
| 190 | /** |
||||||
| 191 | * @param $path |
||||||
| 192 | * @return mixed |
||||||
| 193 | */ |
||||||
| 194 | protected function getByPath($path) |
||||||
| 195 | { |
||||||
| 196 | $model = $this->config->get('url-aliases.model', \Fomvasss\UrlAliases\Models\UrlAlias::class); |
||||||
| 197 | |||||||
| 198 | return $model::byPath($path)->get(); |
||||||
| 199 | } |
||||||
| 200 | } |
||||||
| 201 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths