2017-07-31 153 views
0

下午好。我有一個很大的問題,我使用一些參數來輸入ajax,但是當我發送ajax到這個代碼時,它只是簡單地重定向而不做任何事情。我相信你有一個更好的方法,但文檔沒有解釋任何東西!如果你能幫助我,謝謝!Cakephp 3 - Ajax登錄

class ContaController extends AppController { 
    public function initialize() { 
     parent::initialize(); 
     $this->loadComponent('Flash'); 
     $this->loadComponent('Auth', [ 
      'authenticate' => [ 
       'Form' => [ 
        'fields' => ['username' => 'email', 'password' => 'senha'], 
        'userModel' => 'Conta', 
       ] 
      ], 
      'loginAction' => [ 
       'controller' => 'Conta', 
       'action' => 'index' 
      ], 
      'loginRedirect' => [ 
       'controller' => 'Conta', 
       'action' => 'minhaAgenda' 
      ], 
      'logoutRedirect' => [ 
       'controller' => '/' 
      ], 
      'storage' => 'Memory' 
     ]); 
     $this->Auth->allow(['index']); 
    } 

    public function index() { 
     if ($this->request->is('ajax') || $this->request->is('post')) { 
      $user = $this->Auth->identify(); 
      if ($user) { 
       $this->Auth->setUser($user); 
       echo 'success'; 
      } else { 
       echo 'incorrect'; 
      } 
     } 
    } 
    public function sair() { 
     return $this->redirect($this->Auth->logout()); 
    } 
} 
+0

可以分享視圖文件的代碼嗎? –

回答

0

Carrego o component Auth bem parecido com o seu,porémtem o authorize。 觀察員一linha '授權'=> [ '控制器']

$this->loadComponent('Auth', [ 
       'authorize' => ['Controller'], 
       'loginAction' => [ 
          'controller' => 'Users', 
          'action' => 'login' 
       ], 
       'loginRedirect' => [ 
        'controller' => '/', 
       ], 
       'logoutRedirect' => [ 
          'controller' => 'Users', 
          'action' => 'login' 
       ], 
       'authenticate' => [ 
        'Form' => [ 
         'fields' => [ 
            'username'  => 'username', 
            'password'  => 'password' 
           ], 

        ] 
       ], 
       'unauthorizedRedirect' => $this->referer(), 
       // 'storage' => 'Session' 
      ]); 

ö方法方法日登錄

public function login() 
    { 

    if ($this->request->is('post') || $this->request->is('ajax')) { 
      $user = $this->Auth->identify(); 
      if ($user) { 
       $this->Auth->setUser($user); 
       return $this->redirect($this->Auth->redirectUrl()); 
      } 
      $this->Flash->error(__('Usuário ou senha incorretos.')); 
     } 

    } 

娜視圖觀察OS輸入

<?= $this->Form->input('username',['class'=>'fields]) ?> 
<?= $this->Form->input('password',['label'=>'Senha','class'=>' fields']) ?> 

Existem outras FORMAS德亞米表sem o序列化claro。 Os dados devem ser submetidos e via post e「chegarem」nométodono mesmo formato do submit sem Jquery,Php puro。 012vNa duvida debug($ this-> request-> data); seguido de um die()e veja no console do navegador(caso do ajax)。

A saida de ebug($ this-> request-> data); deve ser algo parecido com isso

[ 
    'username' => 'nomeuser', 
    'password' => 'asdfasdf-essa-e-a-senha' 
]