I was creating a REST API for logging in using CakePHP. My question is:
- In
routes.php, what do I fill inmapresources("xxx")? - POST
/XXX.format XXXController::add() <=this is given in documentation. If my app folders are like this:/localhost/FC/app/webroot/etc. What would the URL be for the post request through which I would send JSON format username and password? Currently I accessindex.phpin webroot by typinglocalhost/FC. If i name my controller Apis instead of recipes below, like
ApisController.php, where do I do changes in the code below? And how do I use add? it's not given in the documentation: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') )); } }Lastly, if I send a user-id password in json to this url, what command do I do to return a 200 ok response?
I know its a bit much, but I'm really a novice and I'm not able to grasp this concept even though I've been at it for 3 days and am about to faint of exhaustion. Please help!
Right now, the controller is customer:
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;