【发布时间】:2015-03-28 18:38:52
【问题描述】:
我遇到了 Slim 框架并一直在使用它。我遇到了 404 问题。使用下面的代码:我知道这与 url 有关,但我不太确定如何解决此问题。我是否必须在某处定义“个人资料”的网址?
我的目录也是这样的:
包括
dbhandler.php
config.php
- dbConnect.php
库
- 苗条
v1
- .htaccess
- index.php
此代码中的问题:mysite.com//parentfolder/v1/profiles 抛出 404 错误
$app->post('/profiles', 'authenticate', function() use ($app) {
// check for required params
verifyRequiredParams(array('gender'));
$response = array();
$gender = $app->request->post('gender');
global $user_id;
$db = new DbHandler();
// creating new task
$profile_id = $db->createUserProfile($user_id, $gender);
if ($profile_id != NULL) {
$response["error"] = false;
$response["message"] = "Profile created successfully";
$response["profile_id"] = $profile_id;
echoRespnse(201, $response);
} else {
$response["error"] = true;
$response["message"] = "Failed to create profile. Please try again";
echoRespnse(200, $response);
}
});
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]
我也有这个功能很好用。
$app->post('/register', function() use ($app) {
// check for required params
verifyRequiredParams(array('name', 'email', 'password'));
$response = array();
// reading post params
$name = $app->request->post('name');
$email = $app->request->post('email');
$password = $app->request->post('password');
// validating email address
validateEmail($email);
$db = new DbHandler();
$res = $db->createUser($name, $email, $password);
if ($res == USER_CREATED_SUCCESSFULLY) {
$response["error"] = false;
$response["message"] = "You are successfully registered";
} else if ($res == USER_CREATE_FAILED) {
$response["error"] = true;
$response["message"] = "Oops! An error occurred while registereing";
} else if ($res == USER_ALREADY_EXISTED) {
$response["error"] = true;
$response["message"] = "Sorry, this email already existed";
}
// echo json response
echoRespnse(201, $response);
});
【问题讨论】:
-
您可以尝试使用this .htaccess 并检查它是否有效吗?
-
看看
$app->post('/profiles', 'authenticate', ...。我知道authenticate是您的中间件的函数名称。那么,您也可以发布您的authenticate代码吗?
标签: php rest http-status-code-404 slim