tuupola / cors-middleware

PSR-7 and PSR-15 CORS middleware
MIT License
132 stars 16 forks source link

Fatal error: Uncaught Error: Call to undefined method Slim\Slim::pipe() #43

Closed rubenssb closed 4 years ago

rubenssb commented 4 years ago

Cors Middleware is normally installed with composer.

My index.php of API is:


<?php

$loader = require 'vendor/autoload.php';

//Consults Routes
$app = new \Slim\Slim(array(
    'templates.path' => 'templates'
));

use Tuupola\Middleware\CorsMiddleware;

#$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(CorsMiddleware::class);

$app->add(new Tuupola\Middleware\CorsMiddleware([
    "origin" => ["*"],
    "methods" => ["GET", "POST", "PUT", "PATCH", "DELETE"],
    "headers.allow" => [],
    "headers.expose" => [],
    "credentials" => false,
    "cache" => 0,
]));

$app->get('/consults/', function() use ($app){
    (new \controllers\Consults($app))->list();
});

$app->get('/consults/:id', function($id) use ($app){
    (new \controllers\Consults($app))->get($id);
});

$app->post('/consults/', function() use ($app){
    (new \controllers\Consults($app))->new();
});

$app->put('/consults/:id', function($id) use ($app){
    (new \controllers\Consults($app))->edit($id);
});

$app->delete('/consults/:id', function($id) use ($app){
    (new \controllers\Consults($app))->delete($id);
});

//Vehicles Routes
$app->get('/vehicles/', function() use ($app){
    (new \controllers\Vehicles($app))->list();
});

$app->get('/vehicles/:id', function($id) use ($app){
    (new \controllers\Vehicles($app))->get($id);
});

$app->get('/vehicles-top/', function() use ($app){
    (new \controllers\Vehicles($app))->listTop();
});

$app->post('/vehicles/', function() use ($app){
    (new \controllers\Vehicles($app))->new();
});

$app->put('/vehicles/:id', function($id) use ($app){
    (new \controllers\Vehicles($app))->edit($id);
});

$app->delete('/vehicles/:id', function($id) use ($app){
    (new \controllers\Vehicles($app))->delete($id);
});

$app->put('/vehicles/:id', function($id) use ($app){
    (new \controllers\Vehicles($app))->incrementConsult($id);
});

$app->run();

I have a error with the line

$app->pipe(CorsMiddleware::class);

Fatal error: Uncaught Error: Call to undefined method Slim\Slim::pipe() in C:\wamp64\www\tabela-fipe\backend\tabelafipe-backend\api\index.php on line 15

Is slim the problem here?

tuupola commented 4 years ago

Slim does not have pipe() method. Instead it uses add().

$app->add(new Tuupola\Middleware\CorsMiddleware);
rubenssb commented 4 years ago

Thanks, @tuupola

Now, I have this error with the line above:

$app->add(new Tuupola\Middleware\CorsMiddleware);

Error: Fatal error: Uncaught Error: Class 'Tuupola\Middleware\CorsMiddleware' not found

If, I change the class to Cors, I have other error:

$app->add(new Tuupola\Middleware\Cors);

Error: Fatal error: Uncaught TypeError: Argument 1 passed to Slim\Slim::add() must be an instance of Slim\Middleware, instance of Tuupola\Middleware\Cors given...

tuupola commented 4 years ago

It seems you have installed version 0.5.2 or older. This is quite old. Try again with the latest. For this you need PHP 7.1 or higher.

tuupola commented 4 years ago

Looking at the error message:

Error: Fatal error: Uncaught TypeError: Argument 1 passed to Slim\Slim::add() 
must be an instance of Slim\Middleware, instance of Tuupola\Middleware\Cors given...

Are you running Slim 2? It is not supported. This middleware can be used with Slim 3 or higher.

rubenssb commented 4 years ago

@tuupola I checked here and my PHP version is 7.2.18. I'm using WAMP server locally.

My Slim version is 3.12.2

tuupola commented 4 years ago

Ok. I would try installing the latest version of middleware. Judging from the classname you have 0.5.2 or older.