zeuxisoo / php-slim-whoops

PHP whoops error on slim framework
131 stars 10 forks source link

How to add middleware to phpErrorHandler? #17

Closed visualcookie closed 7 years ago

visualcookie commented 7 years ago

I know, when placing the Middleware call into my src/middleware.php file, it will automatically replace Slim's error message with Whoops (haven't tested yet). But sometimes, I run into trouble with PHP errors.

Is there a way to replace them with Whoops too? If so, how? Haven't found it anywhere.

EDIT: Tried the rest, to see if the Error Handler is replaced, but it's not. Doing it like so: $app->response()->status(500);

This is my public/index.php file:

<?php

require __DIR__ . '/../vendor/autoload.php';

session_start();

// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);

// Set up dependencies
require __DIR__ . '/../src/dependencies.php';

// Register middleware
require __DIR__ . '/../src/middleware.php';

// Register routes
require __DIR__ . '/../src/routes.php';

// Run app
$app->run();

In the src/middleware.php is the Middleware call as in the Readme $app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware($app));.

zeuxisoo commented 7 years ago

In this stage, the middleware only replaces the default errorHandler container only not phpErrorHandler. The phpErrorHandler is for PHP7.

And if your application breaks the lifecycle of slim middleware. it will not trigger this middleware that why you cannot see the whoop error. For example, the program throws an exception and without a handle.

Update on 2017-01-18 06:39:

  1. Now, the middleware will replace the default errorHandler and phpErrorHandler.
  2. If you want all errors/exceptions handle by whoops, you can use the global mode to install the whoops. this mode supports the program life cycle broken.
visualcookie commented 7 years ago

Amazing, will try that out as soon as I'm back. 👍

@zeuxisoo Edit Hum... somehow it still shows the default Slim Application Error message. Have tried to implement it as middleware aswell as trying it as global in my depenencies file. To see my file structure look here: https://github.com/visualcookie/slim-sentinel-auth/tree/version-2

Maybe you have an idea. I've not yet committed this, since it's not working, but I think you can kinda imagine what I did.

Edit 2 Have figured it out. I needed to implement it in Middleware as well as set the Global. Now works. :)