Skip to content

Commit 30caefa

Browse files
committed
Update code that resolves the custom UrlGenerator (#77)
1 parent a65d646 commit 30caefa

File tree

2 files changed

+10
-32
lines changed

2 files changed

+10
-32
lines changed

src/Illuminate/Routing/UrlGenerator.php

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,13 @@
22

33
namespace CodeZero\LocalizedRoutes\Illuminate\Routing;
44

5-
use Illuminate\Http\Request;
6-
use Illuminate\Routing\RouteCollection;
75
use Illuminate\Routing\UrlGenerator as BaseUrlGenerator;
86
use Illuminate\Support\Facades\App;
97
use Illuminate\Support\Facades\Config;
108
use Illuminate\Support\Facades\Route;
119

1210
class UrlGenerator extends BaseUrlGenerator
1311
{
14-
/**
15-
* Create a new URL Generator instance.
16-
*
17-
* @param \Illuminate\Routing\RouteCollection $routes
18-
* @param \Illuminate\Http\Request $request
19-
* @param string $assetRoot
20-
*/
21-
public function __construct($routes, Request $request, $assetRoot = null)
22-
{
23-
if (!$routes instanceof RouteCollection && !(interface_exists('\Illuminate\Routing\RouteCollectionInterface') && is_subclass_of($routes, '\Illuminate\Routing\RouteCollectionInterface'))) {
24-
throw new \InvalidArgumentException('The $routes parameter has to be of type RouteCollection or RouteCollectionInterface for L6+.');
25-
}
26-
27-
parent::__construct($routes, $request, $assetRoot);
28-
}
29-
3012
/**
3113
* Resolve the URL to a named route or a localized version of it.
3214
*

src/LocalizedRoutesServiceProvider.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use CodeZero\LocalizedRoutes\Macros\Route\LocalizedMacro;
1010
use CodeZero\LocalizedRoutes\Macros\Route\LocalizedUrlMacro;
1111
use CodeZero\Localizer\LocalizerServiceProvider;
12-
use Illuminate\Container\Container;
12+
use Illuminate\Contracts\Routing\UrlGenerator as UrlGeneratorContract;
1313
use Illuminate\Support\ServiceProvider;
1414

1515
class LocalizedRoutesServiceProvider extends ServiceProvider
@@ -92,10 +92,8 @@ protected function registerProviders()
9292
}
9393

9494
/**
95-
* Register the URL generator service.
96-
*
97-
* The UrlGenerator class that is instantiated is determined
98-
* by the "use" statement at the top of this file.
95+
* Register a custom URL generator that extends the one that comes with Laravel.
96+
* This will override a few methods that enables us to generate localized URLs.
9997
*
10098
* This method is an exact copy from:
10199
* \Illuminate\Routing\RoutingServiceProvider
@@ -104,24 +102,22 @@ protected function registerProviders()
104102
*/
105103
protected function registerUrlGenerator()
106104
{
107-
$this->app->singleton('url', function () {
108-
$app = Container::getInstance();
109-
105+
$this->app->singleton('url', function ($app) {
110106
$routes = $app['router']->getRoutes();
111107

112108
// The URL generator needs the route collection that exists on the router.
113109
// Keep in mind this is an object, so we're passing by references here
114110
// and all the registered routes will be available to the generator.
115111
$app->instance('routes', $routes);
116112

117-
$url = $app->make(UrlGenerator::class, [
118-
'routes' => $routes,
119-
'request' => $app->rebinding(
113+
return new UrlGenerator(
114+
$routes, $app->rebinding(
120115
'request', $this->requestRebinder()
121-
),
122-
'assetRoot' => $app['config']['app.asset_url']
123-
]);
116+
), $app['config']['app.asset_url']
117+
);
118+
});
124119

120+
$this->app->extend('url', function (UrlGeneratorContract $url, $app) {
125121
// Next we will set a few service resolvers on the URL generator so it can
126122
// get the information it needs to function. This just provides some of
127123
// the convenience features to this URL generator like "signed" URLs.

0 commit comments

Comments
 (0)