Slim v3使用Custom Class添加中间件

时间:2017-01-23 00:30:16

标签: php middleware slim-3

我想将中间件添加到我的Slim项目中,该项目在允许用户访问之前检查用户的ip。

我的中间件类:

<?php 

namespace App\Middleware;


Class IpFilter
{


protected $request_ip; 
protected $allowed_ip;

public function __construct($allowedip = array('127.0.0.1')) 
{

    $this->request_ip = app()->request()->getIp(); 
    $this->allowed_ip = $allowedip; 
}

public function call() 
{ 
    $checkit = checkIp(); 
    $this->next->call(); 
}

protected function checkIp() 
{ 
    if (!in_array($this->request_ip, $this->allowed_ip)) 
    $app->halt(403); 
}
}

My Bootstrap index.php:

<?php

// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
    return false;
}

require __DIR__ . '/../vendor/autoload.php';
require '../app/middleware/ipfilter.php';


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


$app->get('/test', function() {
   echo "You look like you're from around here"; 
}); 

// Set up dependencies
require __DIR__ . '/../app/dependencies.php';

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

// Register routes
require __DIR__ . '/../app/routes.php';


$app->add(new IpFilter); 
// Run
$app->run();

我正在使用slim skeleton project进行项目设置。运行此代码时出现以下错误。

Fatal error: Class 'IpFilter' not found in
 /Applications/XAMPP/xamppfiles/htdocs/slimtest/my-app/public/index.php 
on line 34

我仍然没有正确理解如何在slim中添加中间件的自定义类。我已经看过几个教程只是创建类并使用$app->add('new class)添加中间件但我无法弄明白。有没有我需要更新的文件,我只是错过了它?

这是一个漫长的周末,有着纤细而没有很多资源,所以任何帮助都会受到高度赞赏。

更新:

当我从ipfilter.php中删除namespace App\Middleware时,我没有得到同样的错误。这次我得到了

Fatal error: Call to undefined method IpFilter::request() in /Applications/XAMPP/xamppfiles/htdocs/slimtest/my-app/app/middleware/ipfilter.php on line 15

我理解为什么,但我认为它可能有助于排除故障并找到问题的根源。

1 个答案:

答案 0 :(得分:2)

好的,终于搞定了。

Index.php

 <?php

// To help the built-in PHP dev server, check if the request was actually for
// something which should probably be served as a static file
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
    return false;
}

use App\Middleware\IpFilter; 

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../app/middleware/ipfilter.php';



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


$app->get('/test', function() {
   echo "You look like you're from around here"; 
}); 

// Set up dependencies
require __DIR__ . '/../app/dependencies.php';

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

// Register routes
require __DIR__ . '/../app/routes.php';

$app->add(new IpFilter); 

// Run
$app->run();

ipfilter.php

<?php 

namespace App\Middleware;


Class IpFilter
{

private $whitelist = arrray('127.0.0.1')
protected $request_ip; 

   public function __invoke($request, $response, $next)
  {

    $request_ip = $request->getAttribute('ip_address'); 

    return $next($request, $response); 
 }


   public function call() 
  { 
    $checkit = checkIp(); 
    $this->next->call(); 
   }

    protected function checkIp() 
    { 
    if (!in_array($this->request_ip, $this->whitelist) 
    $app->halt(403); 
   }
}

KEY:在index.php中使用App\Middleware\Ipfilter。我虽然使用require来添加这个类就足够但显然没有。

向codecourse.com致敬,真的很有帮助。