2

I'm trying out zend expressive and this is my config/autoload/zend-expressive.global.php and when I tried to do a request to a path which will go to an action class it returned the error page but I can't see any php error in the apache error log. So I can't tell what's the issue.

Is there a way in zend-expressive to have those php error log? Also any good documentation for zend expressive? It seems the official documentation doesn't really have much examples.

return [
    'debug' => true,    
    'config_cache_enabled' => false,
    'zend-expressive' => [
        'error_handler' => [
            'template_404'   => 'error::404',
            'template_error' => 'error::error',
        ],
    ],
];
2
  • Zend Expressive v2.0 has been released March 7th and options for error handling are slightly changed and improved in this version. Here is the link for latest documentation. Commented Mar 8, 2017 at 15:30
  • Hi. I was using this to install. docs.zendframework.com/zend-expressive/getting-started/skeleton So if I remove everything again and do composer install then will I get the latest version? Commented Mar 8, 2017 at 17:52

1 Answer 1

4

If you want to try expressive I suggest to use the skeleton installer. It gives you options on what to install. One of the options is the whoops error handler which gives a lot of detailed info about exceptions.

The official docs are here with a lot of info: https://docs.zendframework.com/zend-expressive/

The installer docs: https://docs.zendframework.com/zend-expressive/getting-started/skeleton/

Update: Add ErrorHandler logger example

As a base for your ErrorHandler you can use Zend\Stratigility\Middleware\ErrorHandler. You can attach a listener to that ErrorHandler and use it for logging. Alternatively you can copy that class and modify it to your needs.

Next step is creating a ErrorHandlerFactory for it:

<?php 
// src/Factory/ErrorHandler/ErrorHandlerFactory.php

namespace App\Factory\ErrorHandler;

use Interop\Container\ContainerInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
use Throwable;
use Zend\Diactoros\Response;
use Zend\Expressive\Middleware\ErrorResponseGenerator;
use Zend\Stratigility\Middleware\ErrorHandler;

class ErrorHandlerFactory
{
    public function __invoke(ContainerInterface $container)
    {
        $generator = $container->has(ErrorResponseGenerator::class)
            ? $container->get(ErrorResponseGenerator::class)
            : null;

        $errorHandler = new ErrorHandler(new Response(), $generator);

        if ($container->has(LoggerInterface::class)) {
            $logger = $container->get(LoggerInterface::class);
            $errorHandler->attachListener(function (
                Throwable $throwable,
                RequestInterface $request,
                ResponseInterface $response
            ) use ($logger) {
                $logger->error('"{method} {uri}": {message} in {file}:{line}', [
                    'date'    => date('Y-m-d H:i:s'),
                    'method'  => $request->getMethod(),
                    'uri'     => (string) $request->getUri(),
                    'message' => $throwable->getMessage(),
                    'file'    => $throwable->getFile(),
                    'line'    => $throwable->getLine(),
                ]);
            });
        }

        return $errorHandler;
    }
}

After that you need to register the ErrorHandler. You do this by adding it to config/autoload/middleware-pipeline.global.php and specifically in the middleware => always section. This way it will always run. If you register it as first, it will run before anything else.

<?php 
// in config/autoload/middleware-pipeline.global.php

use Acme\Container\ErrorHandlerFactory;
use Zend\Stratigility\Middleware\ErrorHandler;

return [
    'dependencies' => [
        /* ... */
        'factories' => [
            ErrorHandler::class => ErrorHandlerFactory::class,
            /* ... */
        ],
        /* ... */
    ],
    'middleware_pipeline' => [
        'always' => [
            'middleware' => [
                ErrorHandler::class,
                /* ... */
            ],
            'priority' => 10000,
        ],
        /* ... */
    ],
];
Sign up to request clarification or add additional context in comments.

11 Comments

Is there a way to just display those php errors instead of/beside displaying the error page? It seems those whoops error handler requires you to add code or throw exception in your code. I just want to see those php errors like when it has the 500 internal error and the details.
Okay I got it now. Thanks. I found this as I didn't enable whoops. masterzendframework.com/whoops-errorhandler
That error handler is only for local/dev and I wonder if there's a way to enable the php error reporting in the apache error log in production? This only helps for development but I can't see any detail errors in production.
@sparkmix You can write your own errorhandler to not only catch the errors but also log them. Here's an example of something I use myself: ErrorHandlerFactory
The errorhandler and its listener is documented in the stratigility docs: docs.zendframework.com/zend-stratigility/error-handlers/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.