-
Notifications
You must be signed in to change notification settings - Fork 195
added cookbook for set up custom 404 page #81
Conversation
} | ||
``` | ||
|
||
After it, we need to register the middleware into `invokables` type in our service config: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is specific to a single container implementation, zend-servicemanager. We should likely either (a) provide setup for multiple containers, or (b) leave container configuration as an exercise for the reader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated.
You did. Error middleware in Stratigility and Expressive must have the following signature: function ($error, ServerRequestInterface $request, ResponseInterface $response, callable $next) Note the first argument! The way you have set this up is correct; it's not actually error middleware; it's middleware that you want to intercept if no other middleware has executed by that point, and is indicative of a "not found" situation. That said, this is only one of several approaches. The current recommend approach is to use the final handler. The final handler is invoked when the stack is exausted (i.e., no middleware returns a response), and that can then determine if we have a 404 situation. The TemplatedErrorHandler will use a template for rendering the 404 page. This allows us to have something baked into the library for the situation. The approach you're suggesting here is more suited for custom 404 handling; e.g., you may want to log such requests, or provide suggestions based on the URI for other pages, etc. In those cases, I'd use middleware. For a generic 404 page, however, the final handler approach is easier, and already provided to the user. So, my suggestions:
|
26e971c
to
93d76f4
Compare
I did The TemplatedErrorHandler example in skeleton, and it shows blank page, even I create a template file under |
@samsonasik the 404 condition may be related to #93; looking into that today. |
4f2efe6
to
ff07e76
Compare
@weierophinney it works now 👍 , I've updated the wording. |
@weierophinney merge-able now ? |
@samsonasik I'm still evaluating. As noted above, I think the recommendation for 404 pages should be to use one of the final handler implementations we ship. However, I do think it would make sense to have some examples of different types of error handlers. As an example, on my site, I have an "Unauthorized" error handler that looks to see if the response status has been set to 401 or 403 and/or certain crtieri; in such a case, it then renders a template with a login form and sets the appropriate status. That sort of approach would be a useful example for the cookbook. |
ff07e76
to
e23ae6b
Compare
rebased. |
|
||
```php | ||
$app->pipe($services->get('Application\NotFound')); | ||
$app->run(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick note: the above only works if one of the following criteria are met:
- You have first called
$app->pipeRoutingMiddleware()
; or - if you pulled the
Application
instance from the container, and used theApplicationFactory
shipped with Expressive (which ensures the routing middleware has been added to the pipeline).
I'll add that note when merging.
added cookbook for set up custom 404 page
- Incorporated the feedback I presented. - Modified the structure to present the two possible ways to do 404 handling (either explicitly delegating to error middleware and/or the final handler, or generating and returning a 404 response).
@weierophinney this is what I can get from your site: https://github.com/weierophinney/mwop.net/blob/master/config/autoload/global.php . Still not sure about using 404 page with
WhoopsErrorHandler
orTemplatedErrorHandler
.Note:
defining
'error' => true
inmiddleware_pipeline
config make it not working. Please let me know if I missed something. Thanks.