2

CakePHP 2.2 を使用して、ブラウザ以外のクライアントがデータに接続して保存/アクセスできるアプリを作成したいと考えています。UserController で次のコード スニペットを使用してログインするユーザーの例を見てきました。

public function login()
{
    if($this->request->is('post'))
    {
        if($this->Auth->login())
        {
            $this->Session->setFlash('Login Passed');
        }
        else
       {
            $this->Session->setFlash('Login Failed');
        }
    }
}

これは、ブラウザ上にフォームを提示するブラウザで行われ、ユーザーは「ユーザー名」と「パスワード」を入力し、ボタンをクリックして送信します。JavascriptとAjaxを使用すると、フォームを「シリアル化」してサーバーに送信できることはわかっていますが、フォームを使用したくない(または使用できない)ため、クライアントに「ユーザー名」という2ビットの情報を送信させるだけだとします。および「パスワード」、上記のように「ログイン」メソッドからこのデータをどのように処理できますか? Auth->loginオプションの引数を取ることは知っていますが、ユーザー名とパスワードの組み合わせから$user取得して、それをに渡す方法はありますか? 私は次のようなものを想像します:$userAuth->login

public function login()
{
    if($this->request->is('post'))
    {
        if($this->Auth->login())
        {
            $this->Session->setFlash('Login Passed');
        }
        else
        {
            $this->Session->setFlash('Login Failed');
        }
    }
    else if ($this->RequestHandler->isAjax())
    {
        $tmpUser = getUser ('username', 'password'); // ????? ===> Whatever call is needed here.
        if($this->Auth->login($tmpUser))
        {
            $this->Session->setFlash('Login Passed');
        }
        else
        {
            $this->Session->setFlash('Login Failed');
        }
    }
}
4

1 に答える 1

2

AuthComponent :: $ajaxLoginプロパティを使用して、Auth と Ajax ログインを実装できます。それに加えて、次のコードを試すことができます。

public function login()
{
if($this->request->is('post'))
{
    if($this->Auth->login())
    {
        $this->Session->setFlash('Login Passed');
    }
    else
    {
        $this->Session->setFlash('Login Failed');
    }
}
else if ($this->RequestHandler->isAjax())
{
    $tmpUser['User']['username'] = $this->request->params['username'];
    $tmpUser['User']['password'] = $this->request->params['password'];
    if($this->Auth->login($tmpUser))
    {
        $this->Session->setFlash('Login Passed');
    }
    else
    {
        $this->Session->setFlash('Login Failed');
    }
}
}

firebug コンソールを使用して応答を確認できます。

于 2012-07-23T05:10:47.933 に答える