Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 6be6281

Browse files
committed
Merge branch 'hotfix/skeleton-51'
Close #238 Fixes zendframework/zend-expressive-skeleton#51
2 parents f9ee4bc + f9c6184 commit 6be6281

File tree

4 files changed

+195
-2
lines changed

4 files changed

+195
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ Fifth release candidate.
1111
- [#233](https://github.com/zendframework/zend-expressive/pull/233) adds a
1212
documentation page detailing projects using and tutorials written on
1313
Expressive.
14+
- [#238](https://github.com/zendframework/zend-expressive/pull/238) adds a
15+
cookbook recipe detailing how to handle serving an Expressive application from
16+
a subdirectory of your web root.
1417

1518
### Deprecated
1619

doc/book/cookbook/bookdown.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{"Route-specific middleware pipelines": "route-specific-pipeline.md"},
66
{"Setting custom 404 page handling": "custom-404-page-handling.md"},
77
{"Registering custom view helpers when using zend-view": "using-custom-view-helpers.md"},
8-
{"Using zend-form view helpers": "using-zend-form-view-helpers.md"}
8+
{"Using zend-form view helpers": "using-zend-form-view-helpers.md"},
9+
{"Using Expressive from a subdirectory": "using-a-base-path.md"}
910
]
1011
}
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
# How can I tell my application about a base path?
2+
3+
In some environments, your application may be running in a subdirectory of your
4+
web root. For example:
5+
6+
```
7+
var/
8+
|- www/
9+
| |- wordpress/
10+
| |- expressive/
11+
| | |- public/
12+
| | | |- index.php
13+
```
14+
15+
where `/var/www` is the web root, and your Expressive application is in the
16+
`expressive/` subdirectory. How can you make your application work correctly in
17+
this environment?
18+
19+
## .htaccess in the application root.
20+
21+
If you are using Apache, your first step is to add an `.htaccess` file to your
22+
application root, with directives for rewriting to the `public/` directory:
23+
24+
```ApacheConf
25+
RewriteEngine On
26+
RewriteRule (.*) ./public/$1
27+
```
28+
29+
> ### Using other web servers
30+
>
31+
> If you are using a web-server other than Apache, and know how to do a similar
32+
> rewrite, we'd love to know! Please submit ideas/instructions to
33+
> [our issue tracker](https://github.com/zendframework/zend-expressive/issues)!
34+
35+
## Use middleware to rewrite the path
36+
37+
The above step ensures that clients can hit the website. Now we need to ensure
38+
that the application can route to middleware!
39+
40+
To do this, we will add pre_routing pipeline middleware to intercept the
41+
request, and rewrite the URL accordingly.
42+
43+
At the time of writing, we have two suggestions:
44+
45+
- [los/basepath](https://github.com/Lansoweb/basepath) provides the basic
46+
mechanics of rewriting the URL, and has a stable release.
47+
- [mtymek/blast-base-url](https://github.com/mtymek/blast-base-url) provides the
48+
URL rewriting mechanics, as well as utilities for generating URIs that retain
49+
the base path, but does not have a stable release yet.
50+
51+
### los/basepath
52+
53+
To use `los/basepath`, install it via Composer, copy the configuration files to
54+
your application, and then edit the configuration.
55+
56+
To install and copy the configuration:
57+
58+
```bash
59+
$ composer require los/basepath
60+
$ cp vendor/los/basepath/config/los-basepath.global.php.dist config/autoload/los-basepath.global.php
61+
```
62+
63+
We recommend copying the global configuration to a local configuration file as
64+
well; this allows you to have the production settings in your global
65+
configuration, and development settings in a local configuration (which is
66+
excluded from git by default):
67+
68+
```bash
69+
$ cp config/autoload/los-basepath.global.php config/autoload/los-basepath.local.php
70+
```
71+
72+
Then edit one or both, to change the `los_basepath` settings:
73+
74+
```php
75+
return [
76+
'los_basepath' => '<base path here>',
77+
/* ... */
78+
];
79+
```
80+
81+
The base path should be the portion of the web root leading up to the
82+
`index.php` of your application. In the above example, this would be
83+
`/expressive`.
84+
85+
### mtymek/blast-base-url
86+
87+
To use `mtymek/blast-base-url`, install it via Composer, and register some
88+
configuration.
89+
90+
To install it:
91+
92+
```bash
93+
$ composer require "mtymek/blast-base-url:dev-master@dev"
94+
```
95+
96+
To configure it, update the file `config/autoload/middleware-pipeline.global.php`,
97+
with the following contents:
98+
99+
```php
100+
return [
101+
'dependencies' => [
102+
'factories' => [
103+
Blast\BaseUrl\BaseUrlMiddleware::class => Blast\BaseUrl\BaseUrlMiddlewareFactory::class,
104+
/* ... */
105+
],
106+
/* ... */
107+
],
108+
'middleware_pipeline' => [
109+
'pre_routing' => [
110+
[ 'middleware' => [ Blast\BaseUrl\BaseUrlMiddleware::class ] ],
111+
/* ... */
112+
],
113+
/* ... */
114+
],
115+
];
116+
```
117+
118+
At this point, the middleware will take care of the rewriting for you. No
119+
configuration is necessary, as it does auto-detection of the base path based on
120+
the request URI and the operating system path to the application.
121+
122+
The primary advantage of `mtymek/blast-base-url` is in its additional features:
123+
124+
- it provides an override of `Zend\Expressive\Helper\UrlHelper` that is aware of
125+
the base path, allowing you to create route-based URLs relative to the base
126+
path.
127+
- it provides a new helper, `Blast\BaseUrl\BasePathHelper`, which allows you to
128+
create URLs relative to the base path; this is particularly useful for assets.
129+
130+
To enable these features, we'll add some configuration to
131+
`config/autoload/dependencies.global.php` file:
132+
133+
```php
134+
return [
135+
'dependencies' => [
136+
'invokables' => [
137+
Blast\BaseUrl\BasePathHelper::class => Blast\BaseUrl\BasePathHelper::class,
138+
/* ... */
139+
],
140+
'factories' => [
141+
Blast\BaseUrl\UrlHelper::class => Blast\BaseUrl\UrlHelperFactory::class,
142+
/* ... */
143+
],
144+
'aliases' => [
145+
// alias default UrlHelper with Blast\BaseUrl alternative
146+
Helper\UrlHelper::class => Blast\BaseUrl\UrlHelper::class,
147+
/* ... */
148+
],
149+
],
150+
];
151+
```
152+
153+
Additionally, remove the following from the same file:
154+
155+
```php
156+
'factories' => [
157+
// Remove the following line only:
158+
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
159+
/* ... */
160+
],
161+
```
162+
163+
Finally, if you're using zend-view, you can register a new "basePath" helper in
164+
your `config/autoload/templates.global.php`:
165+
166+
```
167+
return [
168+
/* ... */
169+
'view_helpers' => [
170+
'factories' => [
171+
'basePath' => Blast\BaseUrl\BasePathViewHelperFactory::class,
172+
/* ... */
173+
],
174+
/* ... */
175+
],
176+
];
177+
```
178+
179+
Usage of the `BasePath` helper is as follows:
180+
181+
```php
182+
// where $basePathHelper is an instance of Blast\BaseUrl\BasePathHelper
183+
// as pulled from your container:
184+
echo $basePathHelper('/icons/favicon.ico');
185+
186+
// or, from zend-view's PhpRenderer:
187+
echo $this->basePath('/icons/favicon.ico');
188+
```

mkdocs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pages:
1313
- { Helpers: [{ Introduction: helpers/intro.md }, { UrlHelper: helpers/url-helper.md }, { ServerUrlHelper: helpers/server-url-helper.md }] }
1414
- { Emitters: emitters.md }
1515
- { Examples: usage-examples.md }
16-
- { Cookbook: [{ 'Prepending a common path to all routes': cookbook/common-prefix-for-routes.md }, { 'Route-specific middleware pipelines': cookbook/route-specific-pipeline.md }, { 'Setting custom 404 page handling': cookbook/custom-404-page-handling.md }, { 'Registering custom view helpers when using zend-view': cookbook/using-custom-view-helpers.md }, { 'Using zend-form view helpers': cookbook/using-zend-form-view-helpers.md }] }
16+
- { Cookbook: [{ 'Prepending a common path to all routes': cookbook/common-prefix-for-routes.md }, { 'Route-specific middleware pipelines': cookbook/route-specific-pipeline.md }, { 'Setting custom 404 page handling': cookbook/custom-404-page-handling.md }, { 'Registering custom view helpers when using zend-view': cookbook/using-custom-view-helpers.md }, { 'Using zend-form view helpers': cookbook/using-zend-form-view-helpers.md }, { 'Using Expressive from a subdirectory': cookbook/using-a-base-path.md }] }
17+
- { 'Expressive Projects': expressive-projects.md }
1718
site_name: zend-expressive
1819
site_description: 'zend-expressive: PSR-7 Middleware Microframework'
1920
repo_url: 'https://github.com/zendframework/zend-expressive'

0 commit comments

Comments
 (0)