Zend expressive - php error reporting

2019-05-24 11:53发布

问题:

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',
        ],
    ],
];

回答1:

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,
        ],
        /* ... */
    ],
];