Slim 给了我 404,即使我将它添加为路由

Slim giving me a 404 even though I added it as a route

我正在尝试构建一个简单的 api,它仅来自 MYSQL 服务器 returns JSON。我想实现您可以使用 id 进行搜索的可能性。这是我的代码:

// Load Slim
require 'vendor/autoload.php';

$configuration = [
    'settings' => [
        'displayErrorDetails' => true,
    ],
];
$c = new \Slim\Container($configuration);
$app = new \Slim\App($c);


$app->get('/leerlingen','ll_posts'); //Get all "leerlingen" posts.
$app->get('/docenten', 'do_posts'); //Get all "docenten" posts.
$app->get('/ouders', 'ou_posts'); //Get all "ouders" posts.
$app->get('/bedrijven', 'be_posts'); //Get all bedrijven posts.
$app->get('/ll/search/:id', 'll_search'); //Get info for certain id.


function ll_posts() {
    $sql = "SELECT * FROM leerlingen WHERE approved = 'true'";
    try{
        $db = getConnection();
        $stmt = $db->query($sql);
        $posts = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($posts);
    } catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}}';
    }
}
    function do_posts() {
    $sql = "SELECT * FROM docenten WHERE approved = 'true'";
    try{
        $db = getConnection();
        $stmt = $db->query($sql);
        $posts = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($posts);
    } catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}}';
    }
}
    function ou_posts() {
    $sql = "SELECT * FROM ouders WHERE approved = 'true'";
    try{
        $db = getConnection();
        $stmt = $db->query($sql);
        $posts = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($posts);
    } catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}}';
    }
}
    function be_posts() {
    $sql = "SELECT * FROM bedrijven WHERE approved = 'true'";
    try{
        $db = getConnection();
        $stmt = $db->query($sql);
        $posts = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($posts);
    } catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}}';
    }
}

function ll_search($id) {
    $sql = "SELECT * FROM leerlingen WHERE id = :id";
    try {
        $db = getConnection();
        $stmt = $db->prepare($sql);
        $stmt->bindParam("id", $id);
        $stmt->execute();
        $names = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($names);
    } catch(PDOException $e) {
        echo '{"error":{"text":'. $e->getMessage() .'}}';
    }
}

$app->run();


function getConnection(){
    $dbPost = "localhost";
    $dbUser = "a1070rik";
    $dbPass = "********************";
    $dbName = "portals";
    $dbh = new PDO("mysql:host=$dbHost;dbname=$dbName", $dbUser, $dbPass);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    return $dbh;
}

这是我要访问的页面:

https://***********.io/api/v2/ll/search/11

所有文件夹路径都正常,数据库连接正常。

希望有人有解决办法。

编辑 添加了 .htaccess 文件

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 

RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

我找到了解决方案,

我需要添加旧版本的 slim。

还是谢谢大家

您正在混合使用 Slim 2 和 Slim 3 代码。看来你用的是 Slim 3.

$app = new \Slim\App($c);

但是您使用 Slim 2 语法定义路由。

app->get('/ll/search/:id', 'll_search');

对于 Slim 3,named placeholders 应该像下面这样定义。

app->get('/ll/search/{id}', 'll_search');