Skip to content

Commit

Permalink
Add a localizedHas macro... (#33)
Browse files Browse the repository at this point in the history
...to easily check if a route name exists without worrying about prepending the locale.
It will either accept a locale as the second argument, or use the current identified locale.
  • Loading branch information
mallardduck authored Nov 2, 2020
1 parent 33bfafa commit 263311f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/LocalizedRoutesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace CodeZero\LocalizedRoutes;

use CodeZero\LocalizedRoutes\Macros\IsLocalizedMacro;
use CodeZero\LocalizedRoutes\Macros\LocalizedHasMacro;
use CodeZero\LocalizedRoutes\Macros\LocalizedUrlMacro;
use CodeZero\LocalizedRoutes\Macros\UriTranslationMacro;
use CodeZero\LocalizedRoutes\Macros\LocalizedRoutesMacro;
Expand Down Expand Up @@ -49,6 +50,7 @@ public function register()
protected function registerMacros()
{
IsLocalizedMacro::register();
LocalizedHasMacro::register();
LocalizedRoutesMacro::register();
LocalizedUrlMacro::register();
UriTranslationMacro::register();
Expand Down
26 changes: 26 additions & 0 deletions src/Macros/LocalizedHasMacro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace CodeZero\LocalizedRoutes\Macros;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;

class LocalizedHasMacro
{
/**
* Register the macro.
*
* @return void
*/
public static function register()
{
Route::macro('localizedHas', function ($name, $locale = null) {
$locale = $locale ?? App::getLocale();
if (! $this->routes->hasNamedRoute($locale . ".{$name}")) {
return false;
}

return true;
});
}
}

0 comments on commit 263311f

Please sign in to comment.