Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when I add PSR-15 middleware with class string #61

Closed
ghost opened this issue Apr 13, 2020 · 5 comments
Closed

Error when I add PSR-15 middleware with class string #61

ghost opened this issue Apr 13, 2020 · 5 comments
Labels

Comments

@ghost
Copy link

ghost commented Apr 13, 2020

When I add PSR-15 middleware on Slim (with Bridge) with class string, the following error appears:

Fatal error: Uncaught Invoker\Exception\NotCallableException: Instance of App\Middleware\ExampleMiddleware is not a callable in C:\Users\Ashura\Desktop\Projetos\ignitor\vendor\php-di\invoker\src\Exception\NotCallableException.php:33 

Stack trace: 
#0 C:\Users\Ashura\Desktop\Projetos\ignitor\vendor\php-di\invoker\src\CallableResolver.php(44): Invoker\Exception\NotCallableException::fromInvalidCallable(Object(App\Middleware\ExampleMiddleware), true) 
#1 C:\Users\Ashura\Desktop\Projetos\ignitor\vendor\php-di\slim-bridge\src\CallableResolver.php(26): Invoker\CallableResolver->resolve(Object(App\Middleware\ExampleMiddleware))
#2 C:\Users\Ashura\Desktop\Projetos\ignitor\vendor\slim\slim\Slim\MiddlewareDispatcher.php(195): DI\Bridge\Slim\CallableResolver->resolve('App\\Middleware\\...') 
#3 C:\Users\Ashura\Desktop\Projetos\ignitor\vendor\selective\basepath\src\BasePathMiddleware.php(52): class@anonymous->handle(Object(Slim\Http\ServerRequest)) 
#4 C:\Users\Ashura\Desktop\Projetos\ignitor\vendor\slim\slim\Slim\MiddlewareDispatcher.php in C:\Users\Ashura\Desktop\Projetos\ignitor\vendor\php-di\invoker\src\Exception\NotCallableException.php on line 33

Code:

<?php

// config/middleware.php

$app->add(ExampleMiddleware::class);

// ExampleMiddleware.php
namespace App\Middleware;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Server\MiddlewareInterface;

class ExampleMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        return $handler->handle($request);
    }
}

But when I add middleware as invokable class the middleware works:

<?php

namespace App\Middleware;

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;

class ExampleMiddleware
{
    public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        return $handler->handle($request);
    }
}
@mnapoli
Copy link
Member

mnapoli commented Apr 17, 2020

I think $app->add(ExampleMiddleware::class); should be $app->add([ExampleMiddleware::class, 'process']); instead 🤔

@ghost
Copy link
Author

ghost commented Apr 17, 2020

With this format of array, is giving me this error:

Fatal error: Uncaught Error: Using $this when not in object context in C:\Users\thelu\Desktop\tests\index.php:26 
Stack trace: 
#0 C:\Users\thelu\Desktop\tests\vendor\slim\slim\Slim\MiddlewareDispatcher.php(283): ExampleMiddleware::process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner)) 
#1 C:\Users\thelu\Desktop\tests\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(60): class@anonymous->handle(Object(Slim\Psr7\Request)) 
#2 C:\Users\thelu\Desktop\tests\vendor\slim\slim\Slim\MiddlewareDispatcher.php(140): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous)) 
#3 C:\Users\thelu\Desktop\tests\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): class@anonymous->handle(Object(Slim\Psr7\Request)) 
#4 C:\Users\thelu\Desktop\tests\vendor\slim\slim\Slim\App.php(215): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) 
#5 C:\Users\thelu\Desktop\tests\vendor\slim\slim\Slim\App.php(199): Slim\App->handle(Object(Slim\Psr7\Request)) 
#6 C:\Users\thelu\Desktop\tests\index.p in C:\Users\thelu\Desktop\tests\index.php on line 26

Code:

// Middleware
use Psr\Http\Server\MiddlewareInterface as Middleware;
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Psr\Http\Message\ResponseInterface as Response;

class ExampleMiddleware implements Middleware
{
    public function __construct(Foo $foo)
    {
        $this->foo = $foo;
    }

    public function process(ServerRequest $request, RequestHandler $handler): Response
    {
        $response = $handler->handle($request);

        $response->getBody()->write($this->foo->hello());

        return $response;
    }
}

// config/middleware.php
$app->add([ExampleMiddleware::class, 'process']);

@jDolba
Copy link

jDolba commented Apr 19, 2020

you should pass MIddlewareInterface instance
so with PHP-DI bridge you should use Container for passing instancefor middleware

eg.

$app->add( $container->get(ExampleMiddleware::class) );

or
the Slim way to register middleware/route should be with method-to-call string definition (I do not prefer this one so not 100% sure)

$app->add(ExampleMiddleware::class . '::process');

@ghost
Copy link
Author

ghost commented Apr 19, 2020

With Middleware::class . '::process' syntax I can successfully add the middleware, thanks guys.

I think it would be nice to also support this method for adding Slim middleware:

$app->add(ExampleMiddleware::class);

I think I can try to add this method and send a PR. Thanks :)

@ghost ghost closed this as completed Jun 3, 2020
@Rarst
Copy link
Contributor

Rarst commented Apr 29, 2021

Duplicates #51, trying to get that and related stuff work. :)

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants