我在UsersController中创建了一个在数据库中添加新用户的方法。在cakephp ctp视图文件中一切正常,因为我的请求不是黑洞。我正在使用帖子。但是当我将视图移动到angularjs时,请求是黑洞的。我不明白。有人可以帮助我。
这是UsersController.php代码,名为add的函数是执行这些操作的函数:
<?php
class UsersController extends AppController {
public $components = array(
'RequestHandler',
'Security',
'Session',
'Auth'
);
public function login() {
if ($this->Session->read('Auth.User')) {
$this->set(array(
'message' => array(
'text' => __('You are logged in!'),
'type' => 'error'
),
'_serialize' => array('message')
));
}
if ($this->request->is('post')) {
if(!empty($this->request->data)){
$userDetails = $this->User->find('first', array(
'conditions' => array(
'User.username' => $this->request->data['username'],
'User.password' => $this->request->data['password']
)));
debug($userDetails);
debug($this->Auth->login());
}
if ($this->Auth->login()) {
$this->set(array(
'user' => $this->Session->read('Auth.User'),
'_serialize' => array('user')
));
} else {
$this->set(array(
'message' => array(
'text' => __('Invalid username or password, try again'),
'type' => 'error'
),
'_serialize' => array('message')
));
$this->response->statusCode(401);
}
}
}
public function logout() {
if ($this->Auth->logout()) {
$this->set(array(
'message' => array(
'text' => __('Logout successfully'),
'type' => 'info'
),
'_serialize' => array('message')
));
}
}
public function add(){
if($this->request->is('post')){
if(!empty($this->request->data)){
$password = $this->request->data['User']['password'];
$username = $this->request->data['User']['username'];
//$password = Security::hash($this->request->data['User']['password'], 'sha1', true);
$password = Security::hash($password.$username, 'sha1', true);
debug($password);
}
}
//$this->set(array('message',array('error' => __("No data sent")), '_serialize' => array('message')));
}
public function index() {
$this -> user = $this -> Auth -> user();
if ($this -> user) {
$this -> set('users', $this -> User -> find('all'));
$this -> set('_serialize', array('users'));
}
else {
$this -> set('error', 'user not logged in');
$this -> set('_serialize', array('error'));
}
}
public function user($id = null) {
$this -> layout = null;
if (!$id) {
throw new NotFoundException(__('Invalid user'));
}
$user = $this -> User -> findById($id);
if (!$user) {
throw new NotFoundException(__('Invalid user'));
}
$this -> set('user', $user);
}
}
?>
这是角度控制器:
angular.module('addUser.controllers', []).controller('addUserCtrl', function($scope, $http) {
$scope.register = function() {
data = {'User':{
'username' : $scope.username,
'password' : $scope.password
}};
if(data.User.username != undefined && data.User.password != undefined){
$http.post('API/users/add', data).success(function(data) {
$scope.users = data.users;
console.log(data);
});
}else{
console.log('can\'t login');
}
/**/
};
});
PS:我是cakephp的初学者。 非常感谢和快乐的编码;)
答案 0 :(得分:0)
我找到了问题的解决方案,因为我浏览了整个cakephp书并得到了这个解决方案,不知道它是否是正确的,但对我来说工作正常。 这是参考链接,对我来说就像书中的线条一样:
public function beforeFilter() {
$this -> Security -> blackHoleCallback = 'blackhole';
}
public function blackhole($type) {
return $type;// don't know if it is good or bad practice like this feel free to comment
}