Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 薄型HttpBasicAuth_Php_Slim_Basic Authentication - Fatal编程技术网

Php 薄型HttpBasicAuth

Php 薄型HttpBasicAuth,php,slim,basic-authentication,Php,Slim,Basic Authentication,我有一个单页REST应用程序,我正在尝试添加一些非常简单的授权。只需登录,无需注册。 我正在使用由Mika Tuupola编写的带有简单PDO验证器的HTTP基本身份验证中间件。这是我代码的一部分: require '../vendor/autoload.php'; require '../src/config.php'; $app = new Slim\App(['settings' => $config]); $container = $app-&

我有一个单页REST应用程序,我正在尝试添加一些非常简单的授权。只需登录,无需注册。 我正在使用由Mika Tuupola编写的带有简单PDO验证器的HTTP基本身份验证中间件。这是我代码的一部分:

    require '../vendor/autoload.php';
    require '../src/config.php';

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


    $container = $app->getContainer();
    $container['db'] = function ($c) {
    $db = $c['settings']['db'];
    $pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'],
        $db['user'], $db['pass']);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    return $pdo;
};


$app->add(new \Slim\Middleware\HttpBasicAuthentication([
    "path" => "/roomPictures",
    "realm" => "Protected",
    "authenticator" => new PdoAuthenticator([
        "pdo" => $pdo
    ])
]));


require '../src/routes.php';

$app->run();
在重新加载mypage.dev/roomPictures后,我只收到500个内部服务器错误。 你们知道我可能做错了什么吗

我不确定我是否理解正确,但在触发/roomPictures后,系统对话框窗口会显示询问用户名和密码的信息吗


提前谢谢

在代码中添加以下行:

use Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator;
或者,将中间件实例化为:

$app->add(new Slim\Middleware\HttpBasicAuthentication([
    "path" => "/roomPictures",
    "realm" => "Protected",
    "authenticator" => new Slim\Middleware\HttpBasicAuthentication\PdoAuthenticator([
        "pdo" => $pdo
    ])
]));

看看你的错误日志。重复的问题,看看你的答案。这不完全一样,在我看来,我尝试了第二种选择。在“pdo”=>$pdo行中,编辑器表示$pdo是一个未定义的变量。我如何解决这个问题?定义$pdo变量,或者将$container['db']传递到定义$pdo变量的地方。像这样$app=new Slim\app(['settings'=>$config])$pdo_var=$container['db'];然后“pdo”=>$pdo_var???