dr5779 2017-05-11 18:43
浏览 129
已采纳

调用未定义的方法Slim \ Http \ Request :: withStatus()

I can not solve this problem $ _SESSION ['usernam'] is wrong on purpose to go to the else

Middleware.php

<?php
$auth = function ($response, $request, $next) {
if (isset($_SESSION['usernam']) and is_array($_SESSION['username'])) {
    $response = $next($response, $request);
    //$response = $response->withStatus(401)->write('403.phtml');
} else {
    $response = $response->withStatus(401)->withHeader('Location', '403.phtml');
}

return $response;
};

Error:

Details

Type: Error Message: Call to undefined method Slim\Http\Request::withStatus() File: C:\Users\Geovane\Documents\Dropbox\www\tennis\src\middleware.php Line: 9 Trace

routes.php

$app->map(['GET', 'POST'], '/login', function ($request, $response, $args) {
//var_dump($_SERVER); exit;
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $data = json_decode(filter_input(INPUT_POST, 'data'));
} else {
    $data = 'data';
}

$table = $this->db->table('_users');
$login = $table->where([
    'username' => $data->username,
    'password' => $data->password
    ])->get();

if($login->count()){
    $_SESSION['username'] = (array)$login->first();
    return json_encode('ok');
} else {
    return false;
}
});

app.js

$(function() {
    $('#log-in').click(function(){
        var data = {'username': $('#username').val(), 'password': $('#password').val()};
        data = JSON.stringify(data);
        $.ajax({
            type : 'POST',
            url : 'login',
            dataType : 'json',
            data : {data:data},
            success: function(data){
                if (data == 'ok'){
                    window.location.replace("athletes");
                } else {
                    new PNotify({
                        title: 'Ooops!',
                        text: 'Username ou Password incorretos.',
                        type: 'danger',
                        styling: 'bootstrap3'
                    });
                };
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                new PNotify({
                    title: 'Oh No!',
                    text: 'Erro! Por favor, contate o administrador.',
                    type: 'warning',
                    styling: 'bootstrap3'
                });
            }
        });
    });
});
  • 写回答

1条回答 默认 最新

  • doufangxian4985 2017-05-12 06:09
    关注

    The order of the parameters is wrong, its request response next.

    Change

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

    to this:

    function ($request, $response, $next) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献