我的 Slim API 给我一个 404 错误页面
My Slim API gives me a 404 error page
我正在为我的 angular 应用制作一个 API,它将允许我使用 Slim 访问我的数据库。我遵循了本教程 http://anjanawijesundara.blogspot.ca/2015/04/crud-application-with-angularjs.html
我有一个 'Angularjs' 文件夹。在其中,我有我的 index.html 文件,我的 'api' 文件夹,这个 API,我的 'app' 文件夹,用于 angular 应用程序,和一个 [= css、img 和其他 js 文件的 38=] 文件夹。
我在 'API' 文件夹中安装了 Slim 和 composer(它创建了一个 vendor 文件夹)并且我在 vendor 文件夹旁边有一个 'index.php' 文件。
我的 'index.php' 文件(在 api 文件夹中)目前看起来是这样的:
<?php
require 'vendor/autoload.php';
$app = new \Slim\App;
$app->get('/Types', 'getTypes');
$app->get('/Types/:id', 'getTypeById');
$app->run();
function DB_Connection() {
$dbhost = "localhost";
$dbuser = "kevdug_portfolio";
$dbpass = "*****************";
$dbname = "portfolio";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
function getTypes() {
$sql = "select * FROM pt_type";
try {
$db = DB_Connection();
$stmt = $db->query($sql);
$list = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($list);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getTypeById($id) {
$sql = "select * FROM pt_type WHERE id=".$id;
try {
$db = DB_Connection();
$stmt = $db->query($sql);
$list = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($list);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
?>
我想我的 API 可以通过以下代码使用:
angular.module('appDatabaseCtrl', [])
.controller('databaseCtrl', ['$scope','$routeParams', '$http', '$log',
function($scope, $routeParams, $http, $log){
$scope.testDatabaseTypes = function(){
$http.get('/api/Types').success(function(data) {
$log.info("succes!");
$log.log(data);
})
.error(function (data, status){
$log.error("error!");
$log.log(data);
});
};
$scope.testDatabaseTypesById = function(){
console.log($scope.id);
$http.get('/api/Types/' + $scope.id).success(function(data) {
$log.info("succes!");
$log.log(data);
})
.error(function (data, status){
$log.error("error!");
$log.log(data);
});
};
}
]);
第一个功能有效,但第二个 returns 我遇到了 404 错误。你可以自己看看那些树发生了什么 url:
http://kevdug.webfactional.com/#/database
http://kevdug.webfactional.com/api/types
http://kevdug.webfactional.com/api/types/1 <--- 可以是 1 到 4 之间的任意 id
看来您使用的是 Slim v3(根据 $app = new \Slim\App;
判断),但您的路由格式似乎是 Slim v2。
$app->get('/Types/:id', 'getTypeById');
实际上应该更像 $app->get('/Types/{id}', getTypeById);
。您还可以限制它接受的内容,例如 $app->get('/Types/{id:\d+}', getTypeById);
编辑:您还使用了 Slim v3 的无效函数签名,这就是导航到 Your Example Url with the literal :id
instead of a number it errors 时的原因。您应该使用像
这样的函数签名
function getTypeById(\Slim\Http\Request $req, \Slim\Http\Response $res, $args) {
$id = $args["id"]; // Rest of your code
}
最后,我建议查找一些基本的 SQL 注入保护教程,因为如果您当前的 /Types/:id 路由正常工作,它就会有一个 SQL 注入漏洞。但是因为这不是这个问题的目标,所以我会留下一个警告。
我正在为我的 angular 应用制作一个 API,它将允许我使用 Slim 访问我的数据库。我遵循了本教程 http://anjanawijesundara.blogspot.ca/2015/04/crud-application-with-angularjs.html
我有一个 'Angularjs' 文件夹。在其中,我有我的 index.html 文件,我的 'api' 文件夹,这个 API,我的 'app' 文件夹,用于 angular 应用程序,和一个 [= css、img 和其他 js 文件的 38=] 文件夹。
我在 'API' 文件夹中安装了 Slim 和 composer(它创建了一个 vendor 文件夹)并且我在 vendor 文件夹旁边有一个 'index.php' 文件。
我的 'index.php' 文件(在 api 文件夹中)目前看起来是这样的:
<?php
require 'vendor/autoload.php';
$app = new \Slim\App;
$app->get('/Types', 'getTypes');
$app->get('/Types/:id', 'getTypeById');
$app->run();
function DB_Connection() {
$dbhost = "localhost";
$dbuser = "kevdug_portfolio";
$dbpass = "*****************";
$dbname = "portfolio";
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
function getTypes() {
$sql = "select * FROM pt_type";
try {
$db = DB_Connection();
$stmt = $db->query($sql);
$list = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($list);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getTypeById($id) {
$sql = "select * FROM pt_type WHERE id=".$id;
try {
$db = DB_Connection();
$stmt = $db->query($sql);
$list = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
echo json_encode($list);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
?>
我想我的 API 可以通过以下代码使用:
angular.module('appDatabaseCtrl', [])
.controller('databaseCtrl', ['$scope','$routeParams', '$http', '$log',
function($scope, $routeParams, $http, $log){
$scope.testDatabaseTypes = function(){
$http.get('/api/Types').success(function(data) {
$log.info("succes!");
$log.log(data);
})
.error(function (data, status){
$log.error("error!");
$log.log(data);
});
};
$scope.testDatabaseTypesById = function(){
console.log($scope.id);
$http.get('/api/Types/' + $scope.id).success(function(data) {
$log.info("succes!");
$log.log(data);
})
.error(function (data, status){
$log.error("error!");
$log.log(data);
});
};
}
]);
第一个功能有效,但第二个 returns 我遇到了 404 错误。你可以自己看看那些树发生了什么 url:
http://kevdug.webfactional.com/#/database
http://kevdug.webfactional.com/api/types
http://kevdug.webfactional.com/api/types/1 <--- 可以是 1 到 4 之间的任意 id
看来您使用的是 Slim v3(根据 $app = new \Slim\App;
判断),但您的路由格式似乎是 Slim v2。
$app->get('/Types/:id', 'getTypeById');
实际上应该更像 $app->get('/Types/{id}', getTypeById);
。您还可以限制它接受的内容,例如 $app->get('/Types/{id:\d+}', getTypeById);
编辑:您还使用了 Slim v3 的无效函数签名,这就是导航到 Your Example Url with the literal :id
instead of a number it errors 时的原因。您应该使用像
function getTypeById(\Slim\Http\Request $req, \Slim\Http\Response $res, $args) {
$id = $args["id"]; // Rest of your code
}
最后,我建议查找一些基本的 SQL 注入保护教程,因为如果您当前的 /Types/:id 路由正常工作,它就会有一个 SQL 注入漏洞。但是因为这不是这个问题的目标,所以我会留下一个警告。