PHP whoops error on slim framework

Overview

Slim whoops

PHP whoops error on slim framework

Status

Build Status Coverage Status Downloads this Month Latest Stable Version

Installation

Install the composer

curl -sS https://getcomposer.org/installer | php

Edit composer.json

Slim Whoops Version Global Mode PHP DI
1 n/a 0.1.* no no
2 1.* 0.3.* no no
3 <= 1.* 0.4.* no no
3 >= 2.* 0.5.* no no
3 >= 2.* 0.6.* yes yes
4 >= 2.* 0.7.* no no

For Slim framework 4, The composer.json will looks like

{
    "require": {
        "zeuxisoo/slim-whoops": "0.7.*"
    }
}

Now, install or update the dependencies

php composer.phar install

Basic Usage

Add to middleware with default settings

$app->add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware());

Or you can pass more settings to the WhoopsMiddleware

$app->add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware([
    'enable' => true,
    'editor' => 'sublime',
    'title'  => 'Custom whoops page title',
]));

Custom Editor String

If your editor do not included in default editor list, you can custom it like

$app->add(new Zeuxisoo\Whoops\Slim\WhoopsMiddleware([
    'editor' => function($file, $line) {
        return "http://localhost:8091?message=%file:%line";
    }
]));

Custom Handler Usage

In this usage, you can make your own handler for whoops, like:

$simplyErrorHandler = function($exception, $inspector, $run) {
    $message = $exception->getMessage();
    $title   = $inspector->getExceptionName();

    echo "{$title} -> {$message}";
    exit;
};

And then pass it to the WhoopsMiddleware:

new Zeuxisoo\Whoops\Slim\WhoopsMiddleware([], [$simplyErrorHandler]);

Important Note

Version 0.3.0 or above version

Version 0.2.0

  • You must to install the whoops library manually.
Comments
  • Better DI and preserve request object

    Better DI and preserve request object

    Slim injects the dependency container every time a middleware is created from a class. This commit uses that instead of assuming that the $next callable is going to be an instance of \Slim\App.

    Also, looks like there's no need to get the request object from the dependency container since the middleware is already receiving it as a parameter, otherwise any changes made to the PSR-7 request object are lost.

    The above causes an issue for example when using Slim-Csrfbecause the request would no longer have "attributes" set by this middleware.

    Hope it helps.

    Regards!

    opened by rickycheers 6
  • Cannot get Whoops to be default error handler.

    Cannot get Whoops to be default error handler.

    I must be doing something lame here that is not allowing me to use Whoops to handle errors...

    <?php
    
    /** 
     * Create Slim instance
     */
    
    $app = new \Slim\App([
        'debug' => true,
    ]);
    
    /**
     * Add Whoops middleware into slim application
     */
    $app->add(new \Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware);
    
    /** 
     * Loads environment variables from .env to getenv(), $_ENV and $_SERVER automagically.
     * Access via $app->environment[]
     */
    
    $dotenv = new Dotenv\Dotenv('../');
    $dotenv->load();
    
    /** 
     * Require specific ENV vars to be defined.
     */
    $dotenv->required(['debug']);
    
    /** 
     * Register classes in DI Container
     */
    
    $container = $app->getContainer();
    $container->register(new \App\Services\PageService('../content/pages'));
    $container->register(new \App\Services\GlobalService('../content', 'globals.yml'));
    $container->register(new \Slim\Views\Twig('../templates', [
        'debug' => $app->environment['debug'],
        'cache' => '../cache/templates'
    ]));
    
    /**
     * Get classes and inject into controllers as dependencies
     */
    $container['App\Controllers\PageController'] = function ($c) {
        return new App\Controllers\PageController($c['page'], $c['view']);
    };
    
    /**
     * Add Twig view extensions
     */
    
    $twig = $container->get('view')->getEnvironment();
    
    // Twig core
    $twig->addExtension(new Twig_Extension_Debug());
    
    // Twig extensions
    $twig->addExtension(new Twig_Extensions_Extension_Text());
    $twig->addExtension(new Twig_Extensions_Extension_Date());
    
    // Third party
    $twig->addExtension(new Aptoma\Twig\Extension\MarkdownExtension(new Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine()));
    
    // API
    $twig->addExtension(new App\Extensions\RequestExtension($app->request));
    $twig->addExtension(new App\Extensions\PageExtension($container->get('page')));
    $twig->addExtension(new App\Extensions\GlobalExtension($container->get('global')));
    
    // Helpers
    $twig->addExtension(new App\Extensions\StringExtension());
    
    /**
     * Routes
     */
    
    include('routes.php');
    
    /** 
     * Run app
     */
    
    $app->run();
    

    Any ideas why the default error pages (basic Slim error page handler is showing)?

    opened by ryanscherler 5
  • Call to a member function getName() on a non-object

    Call to a member function getName() on a non-object

    Getting a Whoops\Exception\ErrorException as the title goes.

    File and line number: vendor/zeuxisoo/slim-whoops/src/Zeuxisoo/Whoops/Provider/Slim/WhoopsMiddleware.php:31

    Here is the actual line:

    'Route Name' => $app->router()->getCurrentRoute()->getName() ?: '<none>',
    

    This only happens when I go to an URL that matches no route.

    opened by codeclown 4
  • For a what you disable debug mode?

    For a what you disable debug mode?

    ...
    
    class WhoopsMiddleware extends Middleware {
        public function call() {
            $app = $this->app;
    
            if ($app->config('debug') === true) {
                // Switch to custom error handler by disable debug
                $app->config('debug', false);
    //          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    ...
    

    Why you make this? Debug mode is important for other components!

    opened by tarampampam 3
  • Can you please create a tag for this repo

    Can you please create a tag for this repo

    Please add a tag to your repo so that I can add to my composer.json rather than using dev-master, because if you were to make a breaking change to master, my code would break.

    opened by silentworks 3
  • [Request] Ability to add the resolver

    [Request] Ability to add the resolver

    the setting for editor pretty much only lets you set the editor like "phpstorm" etc. but it doesnt include the option to add the "resolver" part

     $prettyPageHandler->setEditor($this->settings['editor']);
    

    as in

    $prettyPageHandler->addEditor( 'phpstorm', 'http://localhost:8091?message=%file:%line' );
    

    on windows we dont have a native protocol for phpstorm, so using a plugin "remote call" we can trigger the same thing using the above resolver.

    https://plugins.jetbrains.com/plugin/6027-remote-call

    opened by WilliamStam 2
  • Add slim to required dependencies

    Add slim to required dependencies

    I think is better to add slim in the required dependencies. I've slim 3. When i require this package i get the latest version for slim 4 instead of the slim 3 compatible version. Thanks.

    opened by ghost 2
  • where to setup global mode?

    where to setup global mode?

    @zeuxisoo can you recommend where to put your setup code for global mode? I'm using https://github.com/slimphp/Slim-Skeleton so it would be most helpful to know in that context how to insert elegantly :).

    opened by acinader 2
  • How to add middleware to phpErrorHandler?

    How to add middleware to phpErrorHandler?

    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));.

    opened by visualcookie 2
  • [again] Call to undefined method Closure::getContainer()

    [again] Call to undefined method Closure::getContainer()

    This time I am mostly certain it is my fault. But I am using a class to get whoops to work in slim.

    Would you happen to know how to solve this issue ?

    Here is my file : https://github.com/concept-core/laraslim/blob/master/base/AppStarter.php#L47-L72

    opened by wotta 2
  • Fatal error: Cannot use object of type Closure as array

    Fatal error: Cannot use object of type Closure as array

    Fatal error: Cannot use object of type Closure as array in vendor\zeuxisoo\slim-whoops\src\Zeuxisoo\Whoops\Provider\Slim\WhoopsMiddleware.php on line 12

    with Slim 3

    opened by nhymxu 2
  • CORS headers not set

    CORS headers not set

    Hey, I switched to using this middleware (and whooops) also for APIs. Endpoints accessed by javascript that encounter an exception get a 500 error message - see discussion in https://github.com/filp/whoops/issues/666 ... I'd basically need to extend the PrettyPageHandler ... I guess that

    • adding support for a variable PrettyPageHandler
    • adding the ability to pass custom headers via a config option
    • or similar

    might do the trick. Or do you see another way around? Thanks

    opened by killua-eu 1
  • Bump twig/twig from 3.3.7 to 3.3.8

    Bump twig/twig from 3.3.7 to 3.3.8

    Bumps twig/twig from 3.3.7 to 3.3.8.

    Changelog

    Sourced from twig/twig's changelog.

    3.3.8 (2022-02-04)

    • Fix a security issue when in a sandbox: the sort filter must require a Closure for the arrow parameter
    • Fix deprecation notice on round
    • Fix call to deprecated convertToHtml method
    Commits
    • 972d860 Prepare the 3.3.8 release
    • b265233 Merge branch '2.x' into 3.x
    • fca80b5 Bump version
    • 66baa66 Prepare the 2.14.11 release
    • 9e5ca74 Merge branch '2.x' into 3.x
    • 22b9dc3 bug #3641 Disallow non closures in sort filter when the sanbox mode is enab...
    • 2eb3308 Disallow non closures in sort filter when the sanbox mode is enabled
    • 25d410b Merge branch '2.x' into 3.x
    • e056e63 bug #3638 Fix call to deprecated "convertToHtml" method (jderusse)
    • 779fdd0 Fix call to deprecated "convertToHtml" method
    • Additional commits viewable in compare view

    Dependabot compatibility score

    Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


    Dependabot commands and options

    You can trigger Dependabot actions by commenting on this PR:

    • @dependabot rebase will rebase this PR
    • @dependabot recreate will recreate this PR, overwriting any edits that have been made to it
    • @dependabot merge will merge this PR after your CI passes on it
    • @dependabot squash and merge will squash and merge this PR after your CI passes on it
    • @dependabot cancel merge will cancel a previously requested merge and block automerging
    • @dependabot reopen will reopen this PR if it is closed
    • @dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
    • @dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
    • @dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
    • @dependabot use these labels will set the current labels as the default for future PRs for this repo and language
    • @dependabot use these reviewers will set the current reviewers as the default for future PRs for this repo and language
    • @dependabot use these assignees will set the current assignees as the default for future PRs for this repo and language
    • @dependabot use this milestone will set the current milestone as the default for future PRs for this repo and language

    You can disable automated security fix PRs for this repo from the Security Alerts page.

    dependencies 
    opened by dependabot[bot] 0
Owner
Zeuxis
Zeuxis
Slim Auth is an authorization and authentication library for the Slim Framework.

Slim Auth is an authorization and authentication library for the Slim Framework. Authentication is provided by the Zend Framework Zend\Authentication component, and authorization by the Zend Framework Zend\Permissions\Acl component.

Jeremy Kendall 246 Dec 16, 2022
A PHP boilerplate based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready

A PHP boilerplate based on Slim Framework, for start projects with Eloquent ORM, Validation, Auth (JWT), Repositories and Transformers ready.

Damiano Petrungaro 58 Aug 10, 2022
Routes and Middleware for Using OAuth2 Server within a Slim Framework API

Chadicus\Slim\OAuth2 A collection of OAuth2 Server routes, middleware and utilities for use within a Slim 3 Framework API Requirements Chadicus\Slim\O

Chad Gray 126 Oct 8, 2022
UserFrosting is a secure, modern user management system written in PHP and built on top of the Slim Microframework, Twig templating engine, and Eloquent ORM.

UserFrosting is a secure, modern user management system written in PHP and built on top of the Slim Microframework, Twig templating engine, and Eloquent ORM.

UserFrosting 1.6k Jan 1, 2023
Multi-provider authentication framework for PHP

Opauth is a multi-provider authentication framework for PHP, inspired by OmniAuth for Ruby. Opauth enables PHP applications to do user authentication

Opauth – PHP Auth Framework 1.7k Jan 1, 2023
Simple JWT Auth support for Laravel PHP Framework

Laravel JWT Simple JWT Auth for Laravel PHP Framework using Firebase JWT under the hood. Installation Standard Composer package installation: composer

Ricardo Čerljenko 34 Nov 21, 2022
Simple PASETO Auth support for Laravel PHP Framework

Laravel PASETO Simple PASETO Auth for Laravel PHP Framework using paragonie/paseto under the hood. Installation Standard Composer package installation

Ricardo Čerljenko 9 Jan 11, 2022
Authentication REST-API built with Lumen PHP Framework

Authentication REST-API built with Lumen PHP Framework Laravel Lumen is a stunningly fast PHP micro-framework for building web applications with expre

Hüseyin Yağlı 1 Oct 12, 2021
PHP server built using laravel framework.

About Laravel Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experie

Lee 0 Dec 27, 2021
Auth is a module for the Yii PHP framework that provides a web user interface for Yii's built-in authorization manager

Auth is a module for the Yii PHP framework that provides a web user interface for Yii's built-in authorization manager (CAuthManager). You can read more about Yii's authorization manager in the framework documentation under Authentication and Authorization.

Christoffer Niska 134 Oct 22, 2022
LINE strategy for Opauth, Opauth is a multi-provider authentication framework for PHP.

Opauth-LINE Opauth strategy for LINE. Implemented based on https://developers.line.me/web-api/integrating-web-login-v2 using OAuth 2.0. Opauth is a mu

Opauth – PHP Auth Framework 2 Jul 11, 2017
A framework agnostic authentication & authorization system.

Sentinel Sentinel is a PHP 7.3+ framework agnostic fully-featured authentication & authorization system. It also provides additional features such as

Cartalyst 1.4k Dec 30, 2022
This package helps you to associate users with permissions and permission groups with laravel framework

Laravel ACL This package allows you to manage user permissions and groups in a database, and is compatible with Laravel v5.8 or higher. Please check t

Mateus Junges 537 Dec 28, 2022
Simple user-authentication solution, embedded into a small framework.

HUGE Just a simple user authentication solution inside a super-simple framework skeleton that works out-of-the-box (and comes with an auto-installer),

Chris 2.1k Dec 6, 2022
php database agnostic authentication library for php developers

Whoo Whoo is a database agnostic authentication library to manage authentication operation easily. Whoo provides you a layer to access and manage user

Yunus Emre Bulut 9 Jan 15, 2022
Single file PHP that can serve as a JWT based authentication provider to the PHP-CRUD-API project

Single file PHP that can serve as a JWT based authentication provider to the PHP-CRUD-API project

Maurits van der Schee 163 Dec 18, 2022
OAuth 1/2 Provider implementations for chillerlan/php-oauth-core. PHP 7.4+

chillerlan/php-oauth-providers Documentation See the wiki for advanced documentation. Requirements PHP 7.4+ a PSR-18 compatible HTTP client library of

chillerlan 4 Dec 2, 2022
A Native PHP MVC With Auth. If you will build your own PHP project in MVC with router and Auth, you can clone this ready to use MVC pattern repo.

If you will build your own PHP project in MVC with router and Auth, you can clone this ready to use MVC pattern repo. Auth system is implemented. Works with bootstrap 5. Composer with autoload are implemented too for future composer require.

null 2 Jun 6, 2022
A spec compliant, secure by default PHP OAuth 2.0 Server

PHP OAuth 2.0 Server league/oauth2-server is a standards compliant implementation of an OAuth 2.0 authorization server written in PHP which makes work

The League of Extraordinary Packages 6.2k Jan 4, 2023