我正在创建一个用于使用 CakePHP 登录的 REST API。我的问题是:
- 在
routes.php
,我要填写mapresources("xxx")
什么? - POST
/XXX.format XXXController::add() <=
这是在文档中给出的。如果我的应用程序文件夹是这样的:/localhost/FC/app/webroot/
等等。我将通过它发送 JSON 格式的用户名和密码的 post 请求的 URL 是什么?目前我index.php
通过键入在 webroot 中访问localhost/FC
。 如果我将我的控制器命名为 APIs 而不是下面的食谱,例如
ApisController.php
,我在哪里对下面的代码进行更改?以及如何使用添加?它没有在文档中给出:class RecipesController extends AppController { public $components = array('RequestHandler'); public function index() { $recipes = $this->Recipe->find('all'); $this->set(array( 'recipes' => $recipes, '_serialize' => array('recipes') )); } public function view($id) { $recipe = $this->Recipe->findById($id); $this->set(array( 'recipe' => $recipe, '_serialize' => array('recipe') )); } public function edit($id) { $this->Recipe->id = $id; if ($this->Recipe->save($this->request->data)) { $message = 'Saved'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); } public function delete($id) { if ($this->Recipe->delete($id)) { $message = 'Deleted'; } else { $message = 'Error'; } $this->set(array( 'message' => $message, '_serialize' => array('message') )); } }
最后,如果我将 json 中的用户 ID 密码发送到此 url,我应该执行什么命令来返回 200 ok 响应?
我知道它有点多,但我真的是一个新手,即使我已经玩了 3 天并且即将筋疲力尽,我也无法掌握这个概念。请帮忙!
现在,控制器是客户:
public function login() {
if ($this->Session->check('Customer')) { //to check if already logged in
$this->Session->setFlash('You are already logged in as ' . $this->Session->read('Customer.Customer.fname') . ' ' . $this->Session->read('Customer.Customer.sname'));
$this->redirect($this->Session->read('ref'));
} else {
if ($this->request->is('post')||$this->request->is('ajax')) { //receives data by ajax from popup of login
$name = $this->request->data('name');
$pwd = $this->request->data('pwd');
$pwd = md5($pwd); //hashing of password
$customer = $this->Customer->findByEmail($name);
if (!$customer) {
$msg = 'Wrong Username or password/false';
}
if ($customer['Customer']['active'] == 1) {
$customer = $this->Customer->findByEmailAndPassword($name, $pwd);
if (@$customer) {
$this->Session->write('Customer', $customer);
$msg = $customer['Customer']['fname'].'/true';
if ($this->Session->check('order')) {
$msg = $this->Session->read('loc_id').'/set';
}
} else {
$msg = 'Wrong Username or password/false';
}
} else {
$msg = 'Your account in not active. Please check your mails to get the activation link/false';
}
}
}
echo $msg;