douqing5981 2017-02-11 09:06
浏览 90
已采纳

将路由组织到Slim中无法正常工作的单独文件中

This is my Index.php file:

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;
require '../vendor/autoload.php';
require '../src/config/db.php';

$app = new \Slim\App;
//User Routes
require '../src/routes/users.php'; // This not working
require '../src/routes/org.php'; // Only This working

$app->run();

.htaccess file

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

When there is only one require file it's working fine but when adding two or multiple files then only last one working and others giving page not found error. I'm new in slim and PHP so I have no idea what's wrong.

users.php

<?php
/**
 * User: y34h1a
 * Date: 2/10/17
 * Time: 3:47 AM
 */
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

//Get All Customers
$app->get('/api/users', function (Request $request, Response $response){
    $sql = "Select * from users";

    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();

        $stmt = $db->query($sql);
        $users['users'] = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($users);
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});


//GET SINGLE USER
$app->get('/api/user/{id}', function (Request $request, Response $response){
    $id = $request->getAttribute('id');
    $sql = "Select * from users WHERE id = $id";

    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();

        $stmt = $db->query($sql);
        $user['user'] = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($user);
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});


//ADD USER
$app->post('/api/user/add', function (Request $request, Response $response){
    $name = $request->getParam('name');
    $email = $request->getParam('email');
    $password = $request->getParam('password');
    $sql = "Insert into users(name,email, password) values (:name, :email, :password)";

    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();
        $stmt = $db->prepare($sql);

        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':password', $password);

        $stmt->execute();

        echo '{"notice": {"text": "Customer Added"}}';
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});

//UPDATE USER

$app->put('/api/user/update/{id}', function (Request $request, Response $response){
    $id = $request->getAttribute('id');
    $name = $request->getParam('name');
    $email = $request->getParam('email');
    $password = $request->getParam('password');

    $sql = "UPDATE users SET 
              name = :name, 
              email = :email, 
              password = :password 
            WHERE id = $id";

    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();
        $stmt = $db->prepare($sql);

        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':password', $password);

        $stmt->execute();

        echo '{"notice": {"text": "Customr UPDATED"}}';
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});


//DELETE USER
$app->get('/api/user/delete/{id}', function (Request $request, Response $response){
    $id = $request->getAttribute('id');
    $sql = "DELETE FROM users WHERE id = $id";

    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute();
        $db = null;
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});

org.php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

//Get All Orgs
$app->get('/api/orgs', function (Request $request, Response $response){
    $sql = "Select * from org";

    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();

        $stmt = $db->query($sql);
        $users['orgs'] = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($users);
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});

//GET SINGLE ORG
$app->get('/api/org/{id}', function (Request $request, Response $response){
    $id = $request->getAttribute('id');
    $sql = "Select * from org WHERE id = $id";

    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();

        $stmt = $db->query($sql);
        $user['org'] = $stmt->fetchAll(PDO::FETCH_OBJ);
        $db = null;
        echo json_encode($user);
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});



//ADD ORG
$app->post('/api/org/add', function (Request $request, Response $response){
    $user_id = $request->getParam('user_id');
    $name = $request->getParam('name');
    $address = $request->getParam('address');
    $sql = "Insert into org(user_id, name, address) values (:user_id, :name, :address)";
    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();
        $stmt = $db->prepare($sql);

        $stmt->bindParam(':user_id', $user_id);
        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':address', $address);

        $stmt->execute();

        echo '{"notice": {"text": "Organization Added"}}';
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});


//UPDATE ORG
$app->put('/api/org/update/{id}', function (Request $request, Response $response){
    $id = $request->getAttribute('id');
    $name = $request->getParam('name');
    $address = $request->getParam('address');

    $sql = "UPDATE org SET 
              name = :name, 
              address = :address 
            WHERE id = $id";
    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();
        $stmt = $db->prepare($sql);

        $stmt->bindParam(':name', $name);
        $stmt->bindParam(':address', $address);
        $stmt->execute();

        echo '{"notice": {"text": "Organization Deleted"}}';
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});

//DELETE ORG
$app->delete('/api/org/delete/{id}', function (Request $request, Response $response){
    $id = $request->getAttribute('id');
    $user_id = $request->getParam('user_id');
    $org_id = $request->getParam('org_id');
    $sql = "DELETE FROM org WHERE id = $id AND user_id = $user_id";
    try{
        //Get DB Object
        $db = new db();
        //Connect
        $db = $db->connect();
        $stmt = $db->prepare($sql);
        $stmt->execute();
        $db = null;
        echo '{"notice": {"text": "Sheet Deleted"}}';
    }catch(PDOException $e){
        echo '{"error":{"text":'.$e->getMessage().'}';
    }
});
  • 写回答

1条回答 默认 最新

  • du67560 2017-02-11 09:56
    关注

    Okay, your problem is that you basically create three instances of application: one in index.php, one in users.php, one in org.php.

    Application instance is created by

    $app = new \Slim\App;
    

    And that application is launched by $app->run.

    In index.php you have $app, which is overriten by $app in users.php, then overwritten by $app in org.php.

    This is the reason why require '../src/routes/users.php'; seems to be ignored - it's declaring $app, adds routes and in the next file you overwrite $app and add routes.

    To fix this, simply remove

    $app = new \Slim\App;
    

    from everywhere but index.php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含