2016-01-16 25 views
0

我正在为我的角应用程序制作一个API,这将允许我使用Slim访问我的数据库。我遵循本教程http://anjanawijesundara.blogspot.ca/2015/04/crud-application-with-angularjs.html我的Slim API给了我一个404错误页面

我有一个'Angularjs'文件夹。其中,我有我的index.html文件,我的'api'文件夹,这个API在哪里,'app'文件夹,角度应用程序和'assets'文件夹用于css,img和其他js文件。

我用Composer(它创建了一个供应商文件夹)在'API'文件夹中安装了Slim,并且在供应商文件夹旁边有一个'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); 
      }); 
     }; 
    } 
]); 

第一个函数有效,但第二个函数返回404错误。你可以看到发生什么事自己与那些树网址:

http://kevdug.webfactional.com/#/database

http://kevdug.webfactional.com/api/types

http://kevdug.webfactional.com/api/types/1 < ---可以是任何ID为1至4

+0

如何直接使用函数名而不是引用它,例如'$ app-> get('/ Types',getTypes);'?函数名称作为字符串在技术上可以在PHP中调用,但框架似乎使用字符串路由处理程序来引用控制器类的方法 – mzulch

+0

您是否尝试过在URL上键入“index.php”? 类似于http:// localhost:8080/angularjs/api/index.php/Types – PekosoG

+0

对于你的评论,mzulch,我真的不知道该怎么回答你,因为我只是不明白你说的话......对于Pekoso,如果我有index.php,它会工作,但如果我尝试添加一个id来获取特定类型,它会给我一个404错误。我更详细地更新了我的问题,并通过3链接真正了解发生了什么。希望有人能帮助! –

回答

0

看来,您使用的超薄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注入防护教程,因为如果你的电流/类型/:ID的路由工作正常,那么它将有一个SQL注入漏洞。但由于这不是这个问题的目标,我只是留下一个警告。

+0

谢谢soooooooo多!它的工作原理,但我有很多问题给你: - 当我看到Slim在网站上给出的例子时,他们不得不使用顶部的命令(你可以在网站上查看它)。那些是必要的,他们做了什么? - 你是指无效函数签名,我必须使用$ req和$ res,因为我从来没有在我的代码中使用这些变量? –

+0

如果您是指每个示例顶部的'use \ Namespace \ Class;'代码,那么您就不需要使用完全限定名称来进行类型提示。至于函数签名,在Slim v3中,实现并使用了PSR-7,它通过Request和Response类来检索和输出数据,这在评论中有点复杂,所以你需要阅读它在别处。 – Nikey646

+0

我尝试删除$ arg之前在我的函数中出现的所有内容,但它给了我一个错误。这是否意味着如果我想使用$ arg,我必须在它之前使用$ req和$ res?还有,是不是?如果我用“请求$请求,响应$响应”替换这两个,因为我在文档的顶部都使用了声明? –

相关问题