官术网_书友最值得收藏!

Retrieving and updating user info after logging in

After a user is logged in, we will need to get the information we have about him/her. In this recipe, we'll see how to get that information.

Getting ready

We will be using the code created in the Setting up and configuring the Auth library and Creating an authentication system recipes as the basis for this recipe.

How to do it...

To complete this recipe, follow these steps:

  1. Update the profile route with this code:
    Route::get('profile', function()
    {
        if (Auth::check())
        {
            return View::make('profile')->with('user',Auth::user());
        }
        else
        {
            return Redirect::to('login')->with('login_error','You must login first.');
        }
    });
  2. Create our profile view in the app/views directory by creating a file named as profile.php:
    <?php echo Session::get('notify') ?  "<p style='color:
        green'>" . Session::get('notify') . "</p>" : "" ?>
    <h1>Welcome <?php echo $user->name ?></h1>
    <p>Your email: <?php echo $user->email ?></p>
    <p>Your account was created on: <?php echo $user
        ->created_at ?></p>
    <p><a href="<?= URL::to('profile-edit') ?>">Edit your
        information</a></p>
  3. Make a route to hold our form to edit the information:
    Route::get('profile-edit', function()
    {
        if (Auth::check())
        {
            $user = Input::old() ? (object) Input::old() :Auth::user();
            return View::make('profile_edit')->with('user',$user);
        }
    });
  4. Create a view for our edit form:
    <h2>Edit User Info</h2>
    <?php $messages =  $errors->all('<p style="color:red">:message</p>') ?>
    <?php foreach ($messages as $msg): ?>
        <?= $msg ?>
    <?php endforeach; ?>
    <?= Form::open() ?>
    <?= Form::label('email', 'Email address: ') ?>
    <?= Form::text('email', $user->email) ?>
    <br>
    <?= Form::label('password', 'Password: ') ?>
    <?= Form::password('password') ?>
    <br>
    <?= Form::label('password_confirm', 'Retype Password: ') ?>
    <?= Form::password('password_confirm') ?>
    <br>
    <?= Form::label('name', 'Name: ') ?>
    <?= Form::text('name',  $user->name) ?>
    <br>
    <?= Form::submit('Update!') ?>
    <?= Form::close() ?>
  5. Make a route to process the form:
    Route::post('profile-edit', function()
    {
        $rules = array(
            'email'    => 'required|email',
            'password' => 'same:password_confirm',
            'name'     => 'required'
        );
        $validation = Validator::make(Input::all(), $rules);
    
        if ($validation->fails())
        {
            return Redirect::to('profile-edit')->withErrors($validation)->withInput();
        }
    
        $user = User::find(Auth::user()->id);
        $user->email = Input::get('email');
        if (Input::get('password')) {
            $user->password = Hash::make(Input::get('password'));
        }
        $user->name = Input::get('name');
        if ($user->save())
        {
            return Redirect::to('profile')->with('notify','Information updated');
        }
        return Redirect::to('profile-edit')->withInput();
    });

How it works...

To get our user's information and allow him/her to update it, we start by reworking on our profile route. We create a profile view and pass Auth::user() to it in the variable $user. Then, in the view file, we simply echo out any of the information we collected. We're also creating a link to a page where the user can edit his/her information.

Our profile edit page first checks to make sure the user is logged in. If so, we want to populate the $user variable. Since we'll redisplay the form if there is a validation error, we first check if there's anything in Input::old(). If not, this is probably a new visit to the page, so we just use Auth::user(). If Input::old() is being used, we'll recast it as an object, since it's normally an array, and use that in our $user variable.

Our edit view form is very similar to our registration form, except that, if we're logged in, the form is already populated.

When the form is submitted, it is run through some validation. If everything is valid, we need to get the User from the database, using User::find() and the user ID that's stored in Auth::user(). We then add our form input to the user object. With the password field, if it was left empty, we can assume that the user didn't want to change it. So we'll only update the password if something was already entered.

Finally, we save the user information and redirect him/her back to the profile page.

There's more...

The e-mail value in our database will probably need to be unique. For this recipe, we might want to do a quick check of the user's table, and make sure the e-mail address being updated isn't used somewhere else.

See also

  • The Creating an authentication system recipe
主站蜘蛛池模板: 镇平县| 巧家县| 曲沃县| 中阳县| 延庆县| 忻城县| 平江县| 屯留县| 科技| 比如县| 龙里县| 平利县| 莒南县| 林西县| 宜城市| 车险| 德钦县| 湛江市| 河北省| 泰安市| 中江县| 滨海县| 枝江市| 德江县| 天津市| 东乌珠穆沁旗| 社旗县| 武平县| 宁阳县| 屯昌县| 会理县| 仁怀市| 化州市| 资阳市| 高雄市| 阜康市| 彭山县| 合阳县| 巴楚县| 上饶县| 鹿泉市|