|
| 1 | +# ServerUrlHelper |
| 2 | + |
| 3 | +`Zend\Expressive\Helper\ServerUrlHelper` provides the ability to generate a full |
| 4 | +URI by passing only the path to the helper; it will then use that path with the |
| 5 | +current `Psr\Http\Message\UriInterface` instance provided to it in order to |
| 6 | +generate a fully qualified URI. |
| 7 | + |
| 8 | +## Usage |
| 9 | + |
| 10 | +When you have an instance, use either its `generate()` method, or call the |
| 11 | +instance as an invokable: |
| 12 | + |
| 13 | +```php |
| 14 | +// Using the generate() method: |
| 15 | +$url = $helper->generate('/foo'); |
| 16 | + |
| 17 | +// is equivalent to invocation: |
| 18 | +$url = $helper('/foo'); |
| 19 | +``` |
| 20 | + |
| 21 | +The helper is particularly useful when used in conjunction with the |
| 22 | +[UrlHelper](url-helper.md), as you can then create fully qualified URIs for use |
| 23 | +with headers, API hypermedia links, etc.: |
| 24 | + |
| 25 | +```php |
| 26 | +$url = $serverUrl($url('resource', ['id' => 'sha1'])); |
| 27 | +``` |
| 28 | + |
| 29 | +The signature for the ServerUrlHelper `generate()` and `__invoke()` methods is: |
| 30 | + |
| 31 | +```php |
| 32 | +function ($path = null) : string |
| 33 | +``` |
| 34 | + |
| 35 | +Where: |
| 36 | + |
| 37 | +- `$path`, when provided, can be a string path to use to generate a URI. |
| 38 | + |
| 39 | +## Creating an instance |
| 40 | + |
| 41 | +In order to use the helper, you will need to inject it with the current |
| 42 | +`UriInterface` from the request instance. To automate this, we provide |
| 43 | +`Zend\Expressive\Helper\ServerUrlMiddleware`, which composes a `ServerUrl` |
| 44 | +instance, and, when invoked, injects it with the URI instance. |
| 45 | + |
| 46 | +As such, you will need to: |
| 47 | + |
| 48 | +- Register the `ServerUrlHelper` as a service in your container. |
| 49 | +- Register the `ServerUrlMiddleware` as a service in your container. |
| 50 | +- Register the `ServerUrlMiddleware` as pre_routing pipeline middleware. |
| 51 | + |
| 52 | +The following examples demonstrate registering the services. |
| 53 | + |
| 54 | +```php |
| 55 | +use Zend\Expressive\Helper\ServerUrlHelper; |
| 56 | +use Zend\Expressive\Helper\ServerUrlMiddleware; |
| 57 | +use Zend\Expressive\Helper\ServerUrlMiddlewareFactory; |
| 58 | + |
| 59 | +// zend-servicemanager: |
| 60 | +$services->setInvokableClass(ServerUrlHelper::class, ServerUrlHelper::class); |
| 61 | +$services->setFactory(ServerUrlMiddleware::class, ServerUrlMiddlewareFactory::class); |
| 62 | + |
| 63 | +// Pimple: |
| 64 | +$pimple[ServerUrlHelper::class] = $pimple->share(function ($container) { |
| 65 | + return new ServerUrlHelper(); |
| 66 | +}); |
| 67 | +$pimple[ServerUrlMiddleware::class] = $pimple->share(function ($container) { |
| 68 | + $factory = new ServerUrlMiddlewareFactory(); |
| 69 | + return $factory($container); |
| 70 | +}); |
| 71 | + |
| 72 | +// Aura.Di: |
| 73 | +$container->set(ServerUrlHelper::class, $container->lazyNew(ServerUrlHelper::class)); |
| 74 | +$container->set(ServerUrlMiddlewareFactory::class, $container->lazyNew(ServerUrlMiddlewareFactory::class)); |
| 75 | +$container->set( |
| 76 | + ServerUrlMiddleware::class, |
| 77 | + $container->lazyGetCall(ServerUrlMiddlewareFactory::class, '__invoke', $container) |
| 78 | +); |
| 79 | +``` |
| 80 | + |
| 81 | +To register the `ServerUrlMiddleware` as pre-routing pipeline middleware: |
| 82 | + |
| 83 | +```php |
| 84 | +use Zend\Expressive\Helper\ServerUrlMiddleware; |
| 85 | + |
| 86 | +// Do this early, before piping other middleware or routes: |
| 87 | +$app->pipe(ServerUrlMiddleware::class); |
| 88 | + |
| 89 | +// Or use configuration: |
| 90 | +// [ |
| 91 | +// 'middleware_pipeline' => [ |
| 92 | +// 'pre_routing' => [ |
| 93 | +// ['middleware' => ServerUrlMiddleware::class], |
| 94 | +// ], |
| 95 | +// ], |
| 96 | +// ] |
| 97 | +``` |
| 98 | + |
| 99 | +The following dependency configuration will work for all three when using the |
| 100 | +Expressive skeleton: |
| 101 | + |
| 102 | +```php |
| 103 | +return [ |
| 104 | + 'dependencies' => [ |
| 105 | + 'invokables' => [ |
| 106 | + ServerUrlHelper::class => ServerUrlHelper::class, |
| 107 | + ], |
| 108 | + 'factories' => [ |
| 109 | + ServerUrlMiddleware::class => ServerUrlMiddlewareFactory::class, |
| 110 | + ], |
| 111 | + ], |
| 112 | + 'middleware_pipeline' => [ |
| 113 | + 'pre_routing' => [ |
| 114 | + ['middleware' => ServerUrlMiddleware::class], |
| 115 | + ], |
| 116 | + ], |
| 117 | +] |
| 118 | +``` |
| 119 | + |
| 120 | +> ## Skeleton configures helpers |
| 121 | +> |
| 122 | +> If you started your project using the Expressive skeleton package, the |
| 123 | +> `ServerUrlHelper` and `ServerUrlMiddleware` factories are already registered |
| 124 | +> for you, as is the `ServerUrlMiddleware` pre_routing pipeline middleware. |
| 125 | +
|
| 126 | +## Using the helper in middleware |
| 127 | + |
| 128 | +Compose the helper in your middleware (or elsewhere), and then use it to |
| 129 | +generate URI paths: |
| 130 | + |
| 131 | +```php |
| 132 | +use Zend\Expressive\Helper\ServerUrlHelper; |
| 133 | + |
| 134 | +class FooMiddleware |
| 135 | +{ |
| 136 | + private $helper; |
| 137 | + |
| 138 | + public function __construct(ServerUrlHelper $helper) |
| 139 | + { |
| 140 | + $this->helper = $helper; |
| 141 | + } |
| 142 | + |
| 143 | + public function __invoke($request, $response, callable $next) |
| 144 | + { |
| 145 | + $response = $response->withHeader( |
| 146 | + 'Link', |
| 147 | + $this->helper->generate() . '; rel="self"' |
| 148 | + ); |
| 149 | + return $next($request, $response); |
| 150 | + } |
| 151 | +} |
| 152 | +``` |
0 commit comments