1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Endeavors\Components\Routing; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\RoutingServiceProvider as OriginalRoutingServiceProvider; |
6
|
|
|
use Illuminate\Routing\UrlGenerator as OriginalUrlGenerator; |
7
|
|
|
use Illuminate\Routing\Redirector; |
8
|
|
|
use Illuminate\Support\Facades\URL; |
9
|
|
|
use Illuminate\Http\Request; |
10
|
|
|
|
11
|
|
|
class RoutingServiceProvider extends OriginalRoutingServiceProvider |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Register the URL generator service. |
15
|
|
|
* |
16
|
|
|
* @return void |
17
|
|
|
*/ |
18
|
|
|
protected function registerUrlGenerator() |
19
|
|
|
{ |
20
|
|
|
$this->app['url'] = $this->app->share(function($app) { |
21
|
|
|
$routes = $app['router']->getRoutes(); |
22
|
|
|
// The URL generator needs the route collection that exists on the router. |
23
|
|
|
// Keep in mind this is an object, so we're passing by references here |
24
|
|
|
// and all the registered routes will be available to the generator. |
25
|
|
|
$app->instance('routes', $routes); |
26
|
|
|
$url = new UrlGenerator(new OriginalUrlGenerator( |
27
|
|
|
$routes, $app->rebinding( |
28
|
|
|
'request', $this->requestRebinder() |
29
|
|
|
) |
30
|
|
|
)); |
31
|
|
|
$url->setSessionResolver(function() { |
32
|
|
|
return $this->app['session']; |
33
|
|
|
}); |
34
|
|
|
|
35
|
|
|
$url->setKeyResolver(function() { |
36
|
|
|
return $this->app->make('config')->get('app.key'); |
37
|
|
|
}); |
38
|
|
|
// If the route collection is "rebound", for example, when the routes stay |
39
|
|
|
// cached for the application, we will need to rebind the routes on the |
40
|
|
|
// URL generator instance so it has the latest version of the routes. |
41
|
|
|
$app->rebinding('routes', function($app, $routes) { |
42
|
|
|
$app['url']->setRoutes($routes); |
43
|
|
|
}); |
44
|
|
|
return $url; |
45
|
|
|
}); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Register the Redirector service. |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
|
protected function registerRedirector() |
54
|
|
|
{ |
55
|
|
|
$this->app['redirect'] = $this->app->share(function($app) |
56
|
|
|
{ |
57
|
|
|
$redirector = new Redirector($app['url']->getOriginalUrlGenerator()); |
58
|
|
|
|
59
|
|
|
// If the session is set on the application instance, we'll inject it into |
60
|
|
|
// the redirector instance. This allows the redirect responses to allow |
61
|
|
|
// for the quite convenient "with" methods that flash to the session. |
62
|
|
|
if (isset($app['session.store'])) |
63
|
|
|
{ |
64
|
|
|
$redirector->setSession($app['session.store']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $redirector; |
68
|
|
|
}); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|