Search code examples
phpslimstatus

Call to undefined method 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'
                });
            }
        });
    });
});

Solution

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

    Change

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

    to this:

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