简体   繁体   English

Slim v3使用自定义类添加中间件

[英]Slim v3 Using Custom Class to add Middleware

I want to add Middleware to my Slim project that checks the ip of the user before allowing them access. 我想将中间件添加到我的Slim项目中,该项目在允许用户访问之前检查用户的ip。

My middleware class: 我的中间件类:

<?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: 我的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();

I am using a slim skeleton project for my project setup. 我正在为项目设置使用一个苗条的骨架项目 I get the following error when I run this code. 运行此代码时出现以下错误。

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

I still don't properly understand how to add custom classes for middleware in slim. 我仍然不太了解如何在苗条的情况下为中间件添加自定义类。 I've seen several tutorials that just make the class and use $app->add('new class) to add the middleware but I can't figure it out. 我看过几本教程,它们只是使该类成为$app->add('new class)并使用$app->add('new class)添加中间件,但我无法弄清楚。 Is there a file I need to update and I am just missing it? 是否有需要更新的文件,但我只是丢失了?

It's been a long weekend with slim and not a lot of resources out there so any help would be greatly appreciated. 这是一个漫长的周末,身材苗条,没有太多资源,因此不胜感激。

UPDATE: 更新:

When I remove the namespace App\\Middleware from ipfilter.php I don't get the same error. 当我从ipfilter.php删除namespace App\\Middleware ,我没有得到相同的错误。 This time I get 这次我得到

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

Which I understand why but I thought it might help troubleshoot and get to the root of the problem. 我知道为什么,但是我认为这可能有助于解决问题并找到问题的根源。

Okay, Finally got it to work. 好的,终于可以使用了。

Index.php 的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 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: Using App\\Middleware\\Ipfilter in the index.php. 关键字:在index.php中使用App\\Middleware\\Ipfilter I though using require to add the class would be enough but apparently no. 我虽然使用require添加类就足够了,但显然没有。

Shout out to codecourse.com, really helped. 喊到codecourse.com,真的很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM