31 lines
1 KiB
PHP
31 lines
1 KiB
PHP
<?php
|
|
|
|
namespace App\Helpers;
|
|
|
|
use Uwil\LaravelCommon\Controllers\Controller;
|
|
|
|
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, string|null $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 "#";
|
|
}
|
|
}
|