102 lines
3.3 KiB
PHP
102 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use App\Models\DynamicPage;
|
|
use Illuminate\Routing\Route;
|
|
use Uwil\LaravelCommon\Controllers\Controller;
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;
|
|
|
|
class RouteHelper extends Controller
|
|
{
|
|
/**
|
|
* Check if a route name exists and return its URL.
|
|
*
|
|
* This method verifies if the specified route name is registered in Laravel.
|
|
* If the route exists, it returns the corresponding URL using the route helper.
|
|
* Otherwise, it returns a "#" as a fallback.
|
|
*
|
|
* @param string $routeName The name of the route to check.
|
|
* @param string|null $locale The target locale to get the route.
|
|
* @return string The route URL if it exists, or "#" otherwise.
|
|
*/
|
|
public static function routeNameChecker(string $routeName, mixed $locale = null): string
|
|
{
|
|
if (is_null($locale)) {
|
|
$locale = app()->getLocale();
|
|
}
|
|
$definitiveRouteName = sprintf("$routeName.%s", $locale);
|
|
if (\Illuminate\Support\Facades\Route::has($definitiveRouteName)) {
|
|
return route($definitiveRouteName);
|
|
}
|
|
return "#";
|
|
}
|
|
|
|
/**
|
|
* Return true or false, if the passed route is actually active.
|
|
*
|
|
* @param string $routeName
|
|
* @param mixed $locale
|
|
* @return boolean
|
|
*/
|
|
public static function isRouteActive(string $routeName, mixed $locale = null): bool
|
|
{
|
|
if (is_null($locale)) {
|
|
$locale = app()->getLocale();
|
|
}
|
|
$definitiveRouteName = sprintf("$routeName.%s", $locale);
|
|
return \request()->routeIs($definitiveRouteName);
|
|
}
|
|
|
|
/**
|
|
* Get the localized url in the langage switcher.
|
|
*
|
|
* @param string $localeCode LocaleCode from LaravelLocalization::getSupportedLocales().
|
|
* @return string
|
|
*/
|
|
public static function getLocalizedUrl(string $localeCode): string
|
|
{
|
|
$route = request()->route();
|
|
if (!$route) {
|
|
return LaravelLocalization::getLocalizedURL($localeCode);
|
|
}
|
|
return self::resolve(
|
|
self::getDynamicPageLocalizedUrl($route, $localeCode),
|
|
) ?? LaravelLocalization::getLocalizedURL($localeCode, null, [], false);
|
|
}
|
|
|
|
/**
|
|
* Resolve the url to be returned for the localized switcher.
|
|
*
|
|
* @param string|null ...$urls
|
|
* @return string|null
|
|
*/
|
|
private static function resolve(?string ...$urls): ?string
|
|
{
|
|
foreach ($urls as $url) {
|
|
if ($url !== null) {
|
|
return $url;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Return the localized url for all dynamic pages.
|
|
*
|
|
* @param \Illuminate\Routing\Route $route
|
|
* @param string $localeCode
|
|
* @return string|false|null
|
|
*/
|
|
private static function getDynamicPageLocalizedUrl(Route $route, string $localeCode): string|false|null
|
|
{
|
|
/** @var \App\Models\DynamicPage|null $dynamicPage */
|
|
$dynamicPage = $route->parameter('dynamicPage');
|
|
if ($dynamicPage instanceof DynamicPage) {
|
|
$path = $dynamicPage->getLocalizedPath($localeCode);
|
|
$defPath = $path ?? "";
|
|
return LaravelLocalization::getLocalizedURL($localeCode, "/{$defPath}", [], false);
|
|
}
|
|
return null;
|
|
}
|
|
}
|