-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ARCANEDEV/develop
Adding a custom router for localized routes
- Loading branch information
Showing
14 changed files
with
221 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,4 @@ public function handle(Request $request, Closure $next) | |
|
||
return $next($request); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,16 @@ | |
use Arcanedev\Localization\Middleware\LocaleSessionRedirect; | ||
use Arcanedev\Localization\Middleware\LocalizationRedirect; | ||
use Arcanedev\Localization\Middleware\LocalizationRoutes; | ||
use Arcanedev\Support\ServiceProvider; | ||
use Closure; | ||
use Illuminate\Routing\Router; | ||
use Illuminate\Routing\RoutingServiceProvider as ServiceProvider; | ||
use Arcanedev\Localization\Routing\Router; | ||
|
||
/** | ||
* Class RouterServiceProvider | ||
* Class RoutingServiceProvider | ||
* | ||
* @package Arcanedev\Localization\Providers | ||
* @author ARCANEDEV <[email protected]> | ||
*/ | ||
class RouterServiceProvider extends ServiceProvider | ||
class RoutingServiceProvider extends ServiceProvider | ||
{ | ||
/* ------------------------------------------------------------------------------------------------ | ||
| Properties | ||
|
@@ -38,10 +37,23 @@ class RouterServiceProvider extends ServiceProvider | |
*/ | ||
public function register() | ||
{ | ||
$router = $this->app['router']; | ||
$this->registerRouter(); | ||
} | ||
|
||
$this->registerMiddlewares($router); | ||
$this->registerMacros($router); | ||
/* ------------------------------------------------------------------------------------------------ | ||
| Router Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Register the router instance. | ||
*/ | ||
protected function registerRouter() | ||
{ | ||
$this->app['router'] = $this->app->share(function ($app) { | ||
return new Router($app['events'], $app); | ||
}); | ||
|
||
$this->registerMiddlewares($this->app['router']); | ||
} | ||
|
||
/** | ||
|
@@ -68,41 +80,23 @@ private function registerMiddleware(Router $router, $name, $class) | |
{ | ||
$router->middleware($name, $class); | ||
|
||
if ($this->app['config']->get('localization.route.middleware.' . $name, false)) { | ||
if ($this->getMiddleware($name)) { | ||
$this->middleware[] = $name; | ||
} | ||
} | ||
|
||
/* ------------------------------------------------------------------------------------------------ | ||
| Route Macros | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Register the router macros. | ||
* Get the middleware status. | ||
* | ||
* @param Router $router | ||
*/ | ||
private function registerMacros(Router $router) | ||
{ | ||
$this->registerLocalizedGroupMacro($router); | ||
} | ||
|
||
/** | ||
* Register the 'localizedGroup' macro. | ||
* @param string $name | ||
* | ||
* @param Router $router | ||
* @return bool | ||
*/ | ||
private function registerLocalizedGroupMacro(Router $router) | ||
private function getMiddleware($name) | ||
{ | ||
$middleware = $this->middleware; | ||
/** @var \Illuminate\Config\Repository $config */ | ||
$config = $this->app['config']; | ||
|
||
$router->macro('localizedGroup', function (Closure $callback) use ($router, $middleware) { | ||
$attributes = [ | ||
'prefix' => localization()->setLocale(), | ||
'middleware' => $middleware, | ||
]; | ||
|
||
$router->group($attributes, $callback); | ||
}); | ||
return (bool) $config->get('localization.route.middleware.' . $name, false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
<?php namespace Arcanedev\Localization\Routing; | ||
|
||
use Closure; | ||
use Illuminate\Routing\Router as IlluminateRouter; | ||
|
||
/** | ||
* Class Router | ||
* | ||
* @package Arcanedev\Localization\Routing | ||
* @author ARCANEDEV <[email protected]> | ||
*/ | ||
class Router extends IlluminateRouter | ||
{ | ||
/* ------------------------------------------------------------------------------------------------ | ||
| Route Functions | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
/** | ||
* Create a route group with shared attributes. | ||
* | ||
* @param array $attributes | ||
* @param \Closure $callback | ||
*/ | ||
public function localizedGroup(Closure $callback, $attributes = []) | ||
{ | ||
$attributes = array_merge($attributes, [ | ||
'prefix' => localization()->setLocale(), | ||
'middleware' => $this->getMiddleware(), | ||
]); | ||
|
||
$this->group($attributes, $callback); | ||
} | ||
|
||
/** | ||
* @param string $trans | ||
* @param \Closure|array|string $action | ||
* | ||
* @return \Illuminate\Routing\Route | ||
*/ | ||
public function transGet($trans, $action) | ||
{ | ||
$uri = localization()->transRoute($trans); | ||
|
||
return $this->get($uri, $action); | ||
} | ||
|
||
/** | ||
* @param string $trans | ||
* @param \Closure|array|string $action | ||
* | ||
* @return \Illuminate\Routing\Route | ||
*/ | ||
public function transPost($trans, $action) | ||
{ | ||
$uri = localization()->transRoute($trans); | ||
|
||
return $this->post($uri, $action); | ||
} | ||
|
||
/** | ||
* @param string $trans | ||
* @param \Closure|array|string $action | ||
* | ||
* @return \Illuminate\Routing\Route | ||
*/ | ||
public function transPut($trans, $action) | ||
{ | ||
$uri = localization()->transRoute($trans); | ||
|
||
return $this->put($uri, $action); | ||
} | ||
|
||
/** | ||
* Register a new PATCH route with the router. | ||
* | ||
* @param string $trans | ||
* @param \Closure|array|string $action | ||
* | ||
* @return \Illuminate\Routing\Route | ||
*/ | ||
public function transPatch($trans, $action) | ||
{ | ||
$uri = localization()->transRoute($trans); | ||
|
||
return $this->patch($uri, $action); | ||
} | ||
|
||
/** | ||
* Register a new DELETE route with the router. | ||
* | ||
* @param string $trans | ||
* @param \Closure|array|string $action | ||
* | ||
* @return \Illuminate\Routing\Route | ||
*/ | ||
public function transDelete($trans, $action) | ||
{ | ||
$uri = localization()->transRoute($trans); | ||
|
||
return $this->delete($uri, $action); | ||
} | ||
|
||
/** | ||
* Register a new OPTIONS route with the router. | ||
* | ||
* @param string $trans | ||
* @param \Closure|array|string $action | ||
* | ||
* @return \Illuminate\Routing\Route | ||
*/ | ||
public function transOptions($trans, $action) | ||
{ | ||
$uri = localization()->transRoute($trans); | ||
|
||
return $this->options($uri, $action); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php namespace Arcanedev\Localization\Traits; | ||
|
||
/** | ||
* Class LocalizationKernelTrait | ||
* | ||
* @package Arcanedev\Localization\Traits | ||
* @author ARCANEDEV <[email protected]> | ||
* | ||
* @property \Illuminate\Foundation\Application app | ||
* @property \Arcanedev\Localization\Routing\Router router | ||
*/ | ||
trait LocalizationKernelTrait | ||
{ | ||
/** | ||
* Get the route dispatcher callback. | ||
* | ||
* @return \Closure | ||
*/ | ||
protected function dispatchToRouter() | ||
{ | ||
$this->router = $this->app['router']; | ||
|
||
return parent::dispatchToRouter(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php namespace Arcanedev\Localization\Tests\Stubs\Http; | ||
|
||
use Arcanedev\Localization\Traits\LocalizationKernelTrait; | ||
use Orchestra\Testbench\Http\Kernel as HttpKernel; | ||
|
||
/** | ||
* Class Kernel | ||
* | ||
* @package Arcanedev\Localization\Tests\Stubs\Http | ||
* @author ARCANEDEV <[email protected]> | ||
*/ | ||
class Kernel extends HttpKernel | ||
{ | ||
/* ------------------------------------------------------------------------------------------------ | ||
| Traits | ||
| ------------------------------------------------------------------------------------------------ | ||
*/ | ||
use LocalizationKernelTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.