1

私はLaravelを初めて使い、非常に単純なログインフォームを作成しようとしています.

このフォームには「Remember Me」チェックボックスがあります。を使用してその機能を実装しようとしましたが、保存するにはCookie::make()a を返す必要があることがわかりました。Response

ブラウザに保存されている Cookie を確認localhostすると、 という名前の Cookie が見つかりませんusername。私はいくつかの調査を行いましたが、Cookie を a に添付してResponseから返す必要があることがわかりました。

Response問題は、私は!!を返したくないです。

Auth学習過程でまだクラスに到達していません。したがって、このクラスを使用しないソリューションがより適しています。

これが私のコードです:

public function processForm(){
    $data = Input::all();
    if($data['username'] == "rafael" & $data['password'] == "123456"){
        if(Input::has('rememberme')){
            $cookie = Cookie::make('username', $data['username'], 20);
        }
        Session::put('username', $data['username']);
        return Redirect::to('result');
    } else {
        $message_arr = array('message' => 'Invalid username or password!');
        return View::make('signup', $message_arr);
    }
}

私のsignup.blade.php

@extends('layout')

@section('content')
    @if(isset($message))
        <p>Invalid username or password.</p>
    @endif
    <form action="{{ URL::current() }}" method="post">
        <input type="text" name="username"/>
        <br>
        <input type="text" name="password"/>
        <br>
        <input type="checkbox" name="rememberme" value="true"/>
        <input type="submit" name="submit" value="Submit" />
    </form>
@stop

routes.php:

Route::get('signup', 'ActionController@showForm');

Route::post('signup', 'ActionController@processForm');

Route::get('result', 'ActionController@showResult');
4

1 に答える 1

5

ユーザーの認証に関する Laravel 4 のドキュメントを確認してください。

http://laravel.com/docs/security#authenticating-users

基本的に、$data を Auth::attempt() に渡すことでユーザーを認証できます。2 番目のパラメーターとして true を Auth::attempt() に渡し、将来のログインのためにユーザーを記憶します。

$data = Input::all();

if (Auth::attempt($data, ($data['rememberme'] == 'on') ? true : false)
    return Redirect::to('result');
else
{
    $message_arr = array('message' => 'Invalid username or password!');
    return View::make('signup', $message_arr);
}

パスワードリマインダーなどを処理するため、認証には Laravel の方法を使用する必要があります。

于 2013-08-31T19:09:02.850 に答える