bos570
bos570

Reputation: 1523

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.

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:

<?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. 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. 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.

Upvotes: 2

Views: 2303

Answers (1)

bos570
bos570

Reputation: 1523

Okay, Finally got it to work.

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: Using App\Middleware\Ipfilter in the index.php. I though using require to add the class would be enough but apparently no.

Shout out to codecourse.com, really helped.

Upvotes: 2

Related Questions