Skip to content

Commit

Permalink
Merge pull request #3 from mtymek/use_zend_url_helper
Browse files Browse the repository at this point in the history
Use UrlHelper::setBasePath instead of custom helper
  • Loading branch information
mtymek committed Jan 25, 2016
2 parents 77a7a62 + 6bcee27 commit 10169c5
Show file tree
Hide file tree
Showing 9 changed files with 9 additions and 139 deletions.
22 changes: 3 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,9 @@ echo $request->getAttribute(BaseUrlMiddleware::BASE_PATH);

### Generating URLs

While `BaseUrlMiddleware` fixes the routing process, all URLs generated by your application
will likely point outside the subdirectory. `Blast\BaseUrl` comes with replacement `UrlHelper`,

Here's how to enable it:

```php
return [
'dependencies' => [
'factories' => [
// setup factory
Blast\BaseUrl\UrlHelper::class => Blast\BaseUrl\UrlHelperFactory::class,
],
'aliases' => [
// alias default UrlHelper with Blast\BaseUrl alternative
Zend\Expressive\Helper\UrlHelper::class => Blast\BaseUrl\UrlHelper::class,
],
],
];
```
`BaseUrlMiddleware` is able to automatically configure `UrlHelper`, so that all URLs generated
by this helper will have appropriate prefix. This will be done automatically if `UrlHelper`
is available in service container.

### Accessing assets - base path

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"phpunit/phpunit": "^4.6",
"squizlabs/php_codesniffer": "^2.3",
"zendframework/zend-servicemanager": "^2.6",
"zendframework/zend-expressive-helpers": "^1.2",
"zendframework/zend-expressive-helpers": "^1.4 || ^2.0",
"zendframework/zend-diactoros": "^1.3",
"zendframework/zend-expressive-zendviewrenderer": "^1.0"
},
Expand Down
3 changes: 2 additions & 1 deletion src/BaseUrlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Expressive\Helper\UrlHelper;

class BaseUrlMiddleware
{
Expand Down Expand Up @@ -84,7 +85,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
}

if ($this->urlHelper) {
$this->urlHelper->setBaseUrl($baseUrl);
$this->urlHelper->setBasePath($baseUrl);
}

if ($this->basePathHelper) {
Expand Down
1 change: 1 addition & 0 deletions src/BaseUrlMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Blast\BaseUrl;

use Interop\Container\ContainerInterface;
use Zend\Expressive\Helper\UrlHelper;

class BaseUrlMiddlewareFactory
{
Expand Down
24 changes: 0 additions & 24 deletions src/UrlHelper.php

This file was deleted.

29 changes: 0 additions & 29 deletions src/UrlHelperFactory.php

This file was deleted.

5 changes: 2 additions & 3 deletions test/BaseUrlMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

use Blast\BaseUrl\BasePathHelper;
use Blast\BaseUrl\BaseUrlMiddleware;
use Blast\BaseUrl\UrlHelper;
use PHPUnit_Framework_TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Expressive\Helper\UrlHelper;

class BaseUrlMiddlewareTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -46,7 +45,7 @@ public function testMiddlewareInjectsUrlHelperWithBaseUrl()
$middleware = new BaseUrlMiddleware();

$urlHelper = $this->prophesize(UrlHelper::class);
$urlHelper->setBaseUrl('/index.php')->shouldBeCalled();
$urlHelper->setBasePath('/index.php')->shouldBeCalled();
$middleware->setUrlHelper($urlHelper->reveal());

$middleware($request, new Response(), function (ServerRequestInterface $request) {
Expand Down
41 changes: 0 additions & 41 deletions test/UrlHelperFactoryTest.php

This file was deleted.

21 changes: 0 additions & 21 deletions test/UrlHelperTest.php

This file was deleted.

0 comments on commit 10169c5

Please sign in to comment.