fomvasss /
laravel-url-aliases
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * Created by PhpStorm. |
||||
| 4 | * User: fomvasss |
||||
| 5 | * Date: 11.02.19 |
||||
| 6 | * Time: 21:51 |
||||
| 7 | */ |
||||
| 8 | |||||
| 9 | namespace Fomvasss\UrlAliases; |
||||
| 10 | |||||
| 11 | class UrlAlias |
||||
| 12 | { |
||||
| 13 | protected $app; |
||||
| 14 | |||||
| 15 | protected $config; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * UrlAlias constructor. |
||||
| 19 | * |
||||
| 20 | * @param $app |
||||
| 21 | * @throws \Exception |
||||
| 22 | */ |
||||
| 23 | public function __construct($app = null) |
||||
| 24 | { |
||||
| 25 | if (!$app) { |
||||
| 26 | $app = app(); //Fallback when $app is not given |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 27 | } |
||||
| 28 | $this->app = $app; |
||||
| 29 | |||||
| 30 | $this->config = $this->app['config']; |
||||
| 31 | } |
||||
| 32 | |||||
| 33 | public function route(string $systemName, $parameters = [], $absolute = true, $forceWithLocalePrefix = false): string |
||||
| 34 | { |
||||
| 35 | $parameters = array_wrap($parameters); |
||||
| 36 | |||||
| 37 | if (! empty($parameters[0]) && ($parameters[0] instanceof \Illuminate\Database\Eloquent\Model)) { |
||||
| 38 | $entity = $parameters[0]; |
||||
| 39 | if ($entity->urlAlias) { |
||||
| 40 | unset($parameters[0]); |
||||
| 41 | if (! $this->config->get('url-aliases.use_localization')) { |
||||
| 42 | $alias = $entity->urlAlias->alias; |
||||
| 43 | } elseif ($this->config->get('url-aliases-laravellocalization.origin_default_locale') == $entity->urlAlias->locale && $this->config->get('url-aliases-laravellocalization.hideDefaultLocaleInURL') && !$forceWithLocalePrefix) { |
||||
| 44 | $alias = $entity->urlAlias->alias; |
||||
| 45 | } else { |
||||
| 46 | $alias = $entity->urlAlias->localeAlias; |
||||
| 47 | } |
||||
| 48 | |||||
| 49 | if (count($parameters)) { |
||||
| 50 | $alias .= '?' . http_build_query($parameters); |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | return $absolute ? url($alias) : $alias; |
||||
|
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...
|
|||||
| 54 | } |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | if (!empty($this->config['url-aliases']['use_localization'])) { |
||||
| 58 | $relativePath = trim(route($systemName, $parameters, false), '/'); |
||||
|
0 ignored issues
–
show
The function
route 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...
|
|||||
| 59 | |||||
| 60 | if ($absolute) { |
||||
| 61 | return url($this->config['app']['locale'] . '/' . $relativePath); |
||||
| 62 | } |
||||
| 63 | return $relativePath; |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | return route($systemName, $parameters, $absolute); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | public function current($absolute = true): string |
||||
| 70 | { |
||||
| 71 | $path = request()->server('ALIAS_REQUEST_URI', request()->path()); |
||||
|
0 ignored issues
–
show
The function
request 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...
|
|||||
| 72 | |||||
| 73 | return $absolute ? url($path) : $path; |
||||
|
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...
|
|||||
| 74 | } |
||||
| 75 | } |