这是我的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().'}';
}
});
解决方法:
好的,您的问题是您基本上创建了三个应用程序实例:一个在index.php中,一个在users.php中,一个在org.php中.
应用程序实例由创建
$app = new \Slim\App;
然后该应用程序由$app-> run启动.
在index.php中,您有$app,它在users.php中被$app覆盖,然后在org.php中被$app覆盖.
这就是为什么需要’../src/routes/users.php’的原因;似乎被忽略了-它在声明$app,添加路由,在下一个文件中,您覆盖$app并添加路由.
要解决此问题,只需删除
$app = new \Slim\App;
除index.php外,其他任何地方都可以.