Skip to content

Commit d12143d

Browse files
committed
Automatically prefix internal requests and properly set the default domain, version and format in the provider.
Signed-off-by: Jason Lewis <[email protected]>
1 parent 0bdd0af commit d12143d

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/Dispatcher.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ class Dispatcher
131131
*/
132132
protected $vendor;
133133

134+
/**
135+
* API prefix.
136+
*
137+
* @var string
138+
*/
139+
protected $prefix;
140+
134141
/**
135142
* Default version.
136143
*
@@ -438,6 +445,8 @@ protected function createRequest($verb, $uri, $parameters)
438445
{
439446
$parameters = array_merge($this->parameters, (array) $parameters);
440447

448+
$uri = $this->addPrefixToUri($uri);
449+
441450
$request = InternalRequest::create($uri, $verb, $parameters, $this->cookies, $this->uploads, [], $this->content);
442451

443452
$request->headers->set('host', $this->getDomain());
@@ -451,6 +460,28 @@ protected function createRequest($verb, $uri, $parameters)
451460
return $request;
452461
}
453462

463+
/**
464+
* Add the prefix to the URI.
465+
*
466+
* @param string $uri
467+
*
468+
* @return string
469+
*/
470+
protected function addPrefixToUri($uri)
471+
{
472+
if (! isset($this->prefix)) {
473+
return $uri;
474+
}
475+
476+
$uri = trim($uri, '/');
477+
478+
if (starts_with($uri, $this->prefix)) {
479+
return $uri;
480+
}
481+
482+
return '/'.trim($this->prefix, '/').'/'.$uri;
483+
}
484+
454485
/**
455486
* Build the "Accept" header.
456487
*
@@ -606,6 +637,18 @@ public function setVendor($vendor)
606637
$this->vendor = $vendor;
607638
}
608639

640+
/**
641+
* Set the prefix.
642+
*
643+
* @param string $prefix
644+
*
645+
* @return void
646+
*/
647+
public function setPrefix($prefix)
648+
{
649+
$this->prefix = $prefix;
650+
}
651+
609652
/**
610653
* Set the default version.
611654
*

src/Provider/ApiServiceProvider.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,17 @@ protected function registerExceptionHandler()
128128
public function registerDispatcher()
129129
{
130130
$this->app->singleton('api.dispatcher', function ($app) {
131-
return new Dispatcher($app, $app['files'], $app['api.router'], $app['api.auth']);
131+
$dispatcher = new Dispatcher($app, $app['files'], $app['api.router'], $app['api.auth']);
132+
133+
$config = $app['config']['api'];
134+
135+
$dispatcher->setVendor($config['vendor']);
136+
$dispatcher->setPrefix($config['prefix']);
137+
$dispatcher->setDefaultVersion($config['version']);
138+
$dispatcher->setDefaultDomain($config['domain']);
139+
$dispatcher->setDefaultFormat($config['defaultFormat']);
140+
141+
return $dispatcher;
132142
});
133143
}
134144

tests/DispatcherTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public function testInternalRequestWithPrefix()
9494
});
9595

9696
$this->assertEquals('test', $this->dispatcher->get('baz/test'));
97+
98+
$this->dispatcher->setPrefix('baz');
99+
100+
$this->assertEquals('test', $this->dispatcher->get('test'));
97101
}
98102

99103
public function testInternalRequestWithDomain()
@@ -105,6 +109,10 @@ public function testInternalRequestWithDomain()
105109
});
106110

107111
$this->assertEquals('test', $this->dispatcher->get('http://foo.bar/test'));
112+
113+
$this->dispatcher->setDefaultDomain('foo.bar');
114+
115+
$this->assertEquals('test', $this->dispatcher->get('test'));
108116
}
109117

110118
/**

0 commit comments

Comments
 (0)