tuupola/cors-middleware

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

Closed this issue · 17 comments

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.

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

$ composer require tuupola/cors-middleware

I did installed it

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

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

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.

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.

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

Which version? You can check from composer.json.

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

Latest is 0.7.0. Install the latest.

$ composer remove tuupola/cors-middleware
$ composer require tuupola/cors-middleware

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

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

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.

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.

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

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

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

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