将路径组织到Slim

时间:2017-02-11 09:06:06

标签: php .htaccess slim

这是我的 Index.php 文件:

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 文件

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

如果只有一个需要文件,它可以正常工作,但是当添加两个或多个文件时,只有最后一个文件正在工作,而其他文件则页面未找到错误。我是苗条和PHP的新手,所以我不知道出了什么问题。

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 个答案:

答案 0 :(得分:4)

好的,你的问题是你基本上创建了三个应用程序实例:一个在index.php,一个在users.php,一个在org.php

应用程序实例由

创建
$app = new \Slim\App;

该应用程序由$app->run启动。

index.php $app $app users.php $app覆盖了org.phprequire '../src/routes/users.php';覆盖了$app

这就是$app似乎被忽略的原因 - 它声明$app = new \Slim\App; ,添加路由,并在下一个文件中覆盖index.php并添加路由。

要解决此问题,只需删除

即可
<div class='clippy'>
  <svg height='800' width='1020' ...>
    ...
  </svg>
</div>

来自所有地方,但.clippy { height: 360px; width: 1020px; overflow: hidden; }