tuupola / cors-middleware

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

PHP Fatal error: Class 'Tuupola\Middleware\CorsMiddleware' not found #16

Closed jaimecuellar14 closed 6 years ago

jaimecuellar14 commented 6 years ago

Hey, I just download your cors-middleware, and this is my middleware.php

`<?php
 // Application middleware

 // e.g: $app->add(new \Slim\Csrf\Guard);
 use Tuupola\Middleware\HttpBasicAuthentication;
  use Tuupola\Middleware\CorsMiddleware;
   $app->add(new Tuupola\Middleware\CorsMiddleware);

      $container = $app->getContainer();
      $container['logger'] = function($c) {
      $logger = new \Monolog\Logger('my_logger');
      $file_handler = new \Monolog\Handler\StreamHandler("../logs/app.log");
      $logger->pushHandler($file_handler);
       return $logger;
       };

        $container["jwt"] = function ($container) {
        return new StdClass;
        };

        $app->add(new \Slim\Middleware\JwtAuthentication([
        "path" => "/",
        "logger" => $container['logger'],
         "secret" => "123456789helo_secret",
         "rules" => [
          //Si se quiere agregar una ruta que no requiere del token
          //Se debe agregar a la siguiente lista
          new \Slim\Middleware\JwtAuthentication\RequestPathRule([
           "path" => "/",
            "passthrough" => ["/usuarios", "/login","/usuario"]
           ]),
          new \Slim\Middleware\JwtAuthentication\RequestMethodRule([
          "passthrough" => ["OPTIONS"]
          ]),
          ],
          "callback" => function ($request, $response, $arguments) use ($container) {
          $container["jwt"] = $arguments["decoded"];
           },
           "error" => function ($request, $response, $arguments) {
           $data["status"] = "error";
           $data["message"] = $arguments["message"];
            return $response
             ->withHeader("Content-Type", "application/json")
             ->write(json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
             }
             ]));

             $app->add(new \Slim\Middleware\HttpBasicAuthentication([
             "path" => "/api/token",
             "users" => [
             "user" => "password"
              ]
               ]));

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

And I get PHP Fatal error: Class 'Tuupola\Middleware\CorsMiddleware' not found any idea what I am doing wrong and how to solve that? Because everytime I make a call to any route i get the error. By the way, thanks for JWT auth, very helpfull.

tuupola commented 6 years ago

Install tuupola/cors-middleware with composer by issuing the command:

$ composer require tuupola/cors-middleware
jaimecuellar14 commented 6 years ago

I did installed it

tuupola commented 6 years ago

And you are requiring the autoload file as required by composer?

require __DIR__ . "/vendor/autoload.php";
jaimecuellar14 commented 6 years ago

I have that require in my /public/index.php, do i need to add it to middleware.php? Because your JWT auth worked just fine.

tuupola commented 6 years ago

Sorry I have no idea what is wrong. Do the usual debugging, check what is installed in vendor, update composer to the latest, make sure you have latest version of the middleware installed, do you actually get the error from middleware.php file etc.

jaimecuellar14 commented 6 years ago

Ill check it out, because in my vendor folder I do have the folder tuupola/cors-middleware

tuupola commented 6 years ago

Which version? You can check from composer.json.

jaimecuellar14 commented 6 years ago

"tuupola/cors-middleware": "^0.5.2",

tuupola commented 6 years ago

Latest is 0.7.0. Install the latest.

$ composer remove tuupola/cors-middleware
$ composer require tuupola/cors-middleware
jaimecuellar14 commented 6 years ago

I had to do it differently, just adding few lines in my routes.php thanks for everything anyways.

tuupola commented 6 years ago

Now when I think of it you are probably running PHP 5.6, which means composer will install 0.5.2 which is the last version supported by PHP 5.6.

https://github.com/tuupola/cors-middleware/blob/master/CHANGELOG.md

sarath254 commented 6 years ago

I had to do it differently, just adding few lines in my routes.php thanks for everything anyways.

Could you please let me know how to fix this. I get the same error message when I tried to use ajax.

jaimecuellar14 commented 6 years ago

I had to do it differently, just adding few lines in my routes.php thanks for everything anyways.

Could you please let me know how to fix this. I get the same error message when I tried to use ajax.

When I have the source code, i'll let you know, for the moment i dont have it and i forgot how i did it.

aashiskhl commented 6 years ago

If you have latest slim (3.0), add following piece of code to routes.php -it worked for me.

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

tuupola commented 6 years ago

@aashiskhl It means you are using quite old version of the middleware. Classname was changed to CorsMiddleware in 0.6.0,

aashiskhl commented 6 years ago

Ah that's was the reason. However "composer require tuupola/cors-middleware" gives me that Cors.php class by default.

tuupola commented 6 years ago

@aashiskhl Maybe you are running old PHP verion? 7.1 is current requirement.