CakePHP を使用してログインするための REST API を作成していました。私の質問は:
- では
routes.php
、何を記入しmapresources("xxx")
ますか? - POST
/XXX.format XXXController::add() <=
これはドキュメントに記載されています。アプリ フォルダーが次のような場合:/localhost/FC/app/webroot/
など。JSON 形式のユーザー名とパスワードを送信するポスト リクエストの 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;