.htaccess SLIM 3 logn中间件进入循环

.htaccess SLIM 3 logn中间件进入循环,.htaccess,slim,middleware,slim-3,.htaccess,Slim,Middleware,Slim 3,我对SLIM 3登录中间件有一些问题。 在我尝试走的任何路线中,我都会收到以下浏览器错误:“ERR_TOO_MANY_REDIRECTS”。 似乎SLIM进入了一个循环,无法呈现登录页面。 我能做什么? 显然,我已经打印了会话变量,当然它最初是空的,只有在正确登录后才填充 这是我的索引php: <? use Slim\Views\PhpRenderer; session_start(); define( "BASE_URL", "/test/"); define("ROOT_PATH

我对SLIM 3登录中间件有一些问题。 在我尝试走的任何路线中,我都会收到以下浏览器错误:“ERR_TOO_MANY_REDIRECTS”。 似乎SLIM进入了一个循环,无法呈现登录页面。 我能做什么? 显然,我已经打印了会话变量,当然它最初是空的,只有在正确登录后才填充

这是我的索引php:

<?

use Slim\Views\PhpRenderer;

session_start();

define( "BASE_URL", "/test/");
define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/test/");

require 'vendor/autoload.php';
require 'config/db.php';
require('functions/middleware.php');

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
use Illuminate\Database\Capsule\Manager as Capsule;

$capsule = new Capsule;
$capsule->addConnection([
    'driver' => 'mysql',
    'host' => DB_HOST,
    'port' => DB_PORT,
    'database' => DB_NAME,
    'username' => DB_USER,
    'password' => DB_PASSWORD,
    'charset' => 'utf8',
    'collation' => 'utf8_unicode_ci',
]);

$capsule->bootEloquent();
$capsule->setAsGlobal();

$config['displayErrorDetails'] = true;
$config['addContentLengthHeader'] = false;
$config['determineRouteBeforeAppMiddleware'] = true;

$app = new \Slim\App(['settings' => $config]);

$container = $app->getContainer();
$container['renderer'] = new PhpRenderer("./templates");
$container['notFoundHandler'] = function ($container) {
    return function ($request, $response) use ($container) {
        return $container['renderer']->render($response, "/404.php");
    };
};

// Apply the middleware to every request.
$app->add($loggedInMiddleware);

include 'routes.php';

$app->run();
最后这是我的.htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

我试过很多东西;我不确定这是否是正确的答案,但它解决了问题(至少在我这边):

我已将您的middelware修改为:

<?php
function (\Slim\Http\Request $request, \Slim\Http\Response $response, $next) {

    $route = $request->getAttribute('route');

    if (empty($route)) {
        throw new \Slim\Exception\NotFoundException($request, $response);
    }

    $routeName = $route->getName();

    // Define routes that user does not have to be logged in with. All other routes, the user
    // needs to be logged in with.
    $publicRoutesArray = array(
        'login',
        'logout'
    );

    if ( empty($_SESSION['user']) && !in_array($routeName, $publicRoutesArray)) {
        // redirect the user to the login page and do not proceed.
        return $response->withRedirect('/test/login');
    }

    // Proceed as normal...
    return $next($request, $response);
}
?>

我真诚地希望它能有所帮助

嗯,我试过很多东西;我不确定这是否是正确的答案,但它解决了问题(至少在我这边):

我已将您的middelware修改为:

<?php
function (\Slim\Http\Request $request, \Slim\Http\Response $response, $next) {

    $route = $request->getAttribute('route');

    if (empty($route)) {
        throw new \Slim\Exception\NotFoundException($request, $response);
    }

    $routeName = $route->getName();

    // Define routes that user does not have to be logged in with. All other routes, the user
    // needs to be logged in with.
    $publicRoutesArray = array(
        'login',
        'logout'
    );

    if ( empty($_SESSION['user']) && !in_array($routeName, $publicRoutesArray)) {
        // redirect the user to the login page and do not proceed.
        return $response->withRedirect('/test/login');
    }

    // Proceed as normal...
    return $next($request, $response);
}
?>

我真诚地希望这对你有帮助

你有一个会议,因为你从一开始就开始了会议:

<?

use Slim\Views\PhpRenderer;

session_start();
...
因此,当他们点击登录路径(并且没有登录)时,应用程序将进入else语句,这将把他们重定向到仪表板(很确定这是“错误的”,因为你希望他们留在那里提交表单登录)。当它们点击仪表板时,该路由不是公共路由的一部分,因此它会将它们重定向回登录页面。因此,这种循环将永远持续下去

也许改变:

if (!$_SESSION && in_array($routeName, $publicRoutesArray) === FALSE) {

    // redirect the user to the login page and do not proceed.
    $response = $response->withRedirect('/test/login');

} else {


您有一个会话,因为您从一开始就开始会话:

<?

use Slim\Views\PhpRenderer;

session_start();
...
因此,当他们点击登录路径(并且没有登录)时,应用程序将进入else语句,这将把他们重定向到仪表板(很确定这是“错误的”,因为你希望他们留在那里提交表单登录)。当它们点击仪表板时,该路由不是公共路由的一部分,因此它会将它们重定向回登录页面。因此,这种循环将永远持续下去

也许改变:

if (!$_SESSION && in_array($routeName, $publicRoutesArray) === FALSE) {

    // redirect the user to the login page and do not proceed.
    $response = $response->withRedirect('/test/login');

} else {


它工作得很好!!非常感谢!我只有最后一个问题:使用“$app->add($loggedInMiddleware)”;“每个路由都将受到保护……如果我想在我想要保护的路由组中将此中间件用作函数,我该怎么办?只需将其用作
$this->group(“”,function(){})->add($yourmidleware)
它工作得非常好!!非常感谢!我只有最后一个问题:使用“$app->add($loggedInMiddleware)”;“每个路由都将受到保护……如果我想将此中间件用作我要保护的路由组中的函数,我该怎么办?只需将其用作
$this->group('',function(){})->add($yourmidleware)
if (!$_SESSION && in_array($routeName, $publicRoutesArray) === FALSE) {

    // redirect the user to the login page and do not proceed.
    $response = $response->withRedirect('/test/login');

} else {
if (!isset($_SESSION['user'] && in_array($routeName, $publicRoutesArray) === FALSE) {

    // redirect the user to the login page and do not proceed.
    $response = $response->withRedirect('/test/login');

} else {