php - My Slim API gives me a 404 error page -
i'm making api angular app allow me access database slim. followed tutorial http://anjanawijesundara.blogspot.ca/2015/04/crud-application-with-angularjs.html
i have 'angularjs' folder. in it, have index.html file, 'api' folder api, 'app' folder, angular app, , 'assets' folder css, img , other js file.
i installed slim in 'api' folder composer (it created vendor folder) , have 'index.php' file next vendor folder.
my 'index.php' file (in api folder) looks far:
<?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 * 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 * pt_type 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() .'}}'; } } ?>
i'm suppose able use api code:
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); }); }; } ]);
the first function works, second returns me 404 error. can see happen tree url:
http://kevdug.webfactional.com/#/database
http://kevdug.webfactional.com/api/types
http://kevdug.webfactional.com/api/types/1 <--- can id 1 4
it appears using slim v3 (judging $app = new \slim\app;
), appears route format of slim v2.
$app->get('/types/:id', 'gettypebyid');
should more $app->get('/types/{id}', gettypebyid);
. can provide restrictions accepts $app->get('/types/{id:\d+}', gettypebyid);
edit: using invalid function signature slim v3, why when navigating your example url literal :id
instead of number errors. should use function signature like
function gettypebyid(\slim\http\request $req, \slim\http\response $res, $args) { $id = $args["id"]; // rest of code }
finally, recommend looking basic sql injection protection tutorials, because if current /types/:id route worked correctly, have sql injection vulnerability. since not target of question, i'll leave warning.
Comments
Post a Comment