2017-05-11 6 views
0

カスタムミドルウェアクラスを作成しようとしていますが、extend \Slim\Middlewareが見つからないためそのように呼び出すことができません。スリムフレームワーク - クラス 'Slim \ Middleware'が見つかりません

これはこれはこれは、カスタムミドルウェア・ファイルである私のtester_auth.php

<?php 
use Slim\Middleware; 

class TestAuth extends \Slim\Middleware { 

    public function __construct() { 
     //Define the urls that you want to exclude from Authentication, aka public urls  
     $this->whiteList = array('\/login'); 
    } 

    public function deny_access() { 
     $res = $this->app->response(); 
     $res->status(401); 
    } 

    public function isPublicUrl($url) { 
     $patterns_flattened = implode('|', $this->whiteList); 
     $matches = null; 
     preg_match('/' . $patterns_flattened . '/', $url, $matches); 
     return (count($matches) > 0); 
    } 

    public function call() { 
     //Get the token sent from jquery 
     $tokenAuth = $this->app->request->headers->get('Authorization'); 
     //We can check if the url requested is public or protected 
     if ($this->isPublicUrl($this->app->request->getPathInfo())) { 
      //if public, then we just call the next middleware and continue execution normally 
      $this->next->call(); 
     } else { 
      $this->deny_access(); 
     } 
    } 
} 

それは私のmiddleware.php

<?php 
require_once("tester_auth.php"); 

$app->add(new TestAuth()); 

である私のindex.php

<?php 

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

session_start(); 

// Instantiate the app 
$settings = require __DIR__ . '/../src/settings.php'; 
$app = new \Slim\App($settings); 

// Register middleware 
require __DIR__ . '/../src/middleware.php'; 

// Run app 
$app->run(); 

です正常に私のTestAuthになるが、私にエラーを与えるClass 'Slim\\Middleware' not found in /var/www/html/src/tester_auth.php on line 6 ...

編集:まず

、私のcomposer.jsonはミドルウェアがインストールされていませんでしたが、私はので、それを更新して、それが今、まだ同じ問題を抱えてありますので、私はこの問題を抱えていた。..

"require": { 
     "php": ">=5.5.0", 
     "slim/slim": "^3.8", 
     "slim/php-view": "^2.0", 
     "slim/middleware": "*", 
     "monolog/monolog": "^1.17" 
    } 
+0

'作曲ダンプ-autoload'私は' PHP composer.pharダンプ-autoload'をしましたが、同じ問題はあなたがスリム2とスリム3コードを混合している –

+0

@JeremyHarrisを実行してみてください。 – Walker

+0

起こっている –

答えて

0

PHPをキャッシュする場合は、HTTPサーバー(ngixn、PHP Webサーバーなど)を再起動してみてください。

BTW、slim/middlewareはスリム2用です。ですが、3を使用しています。

関連する問題