2014-11-21 16:01
浏览 31

Laravel - 如何在返回重定向中使用变量?

I'm new to laravel and am trying to play around with it's features. However, I'm stuck on being able to use the $username variable

In my UserController class I have the following method:

public function handleRegister()
    $user = new User();
    $user->email = Input::get('email');
    $user->username = Input::get('username');
    $user->password = Hash::make(Input::get('password'));
    return Redirect::to('login')->withInput(array('username', $user->username));

This portion of my code works perfectly since when I var_dump(Input::Old()) using 'Jon' as the username, the following is returned for /login:

array(2) { [0]=> string(8) "username" [1]=> string(3) "Jon" }

For the username input field for /login, I attempt to use the value using:

value="{{ Input::old('username') or '' }}"

However, the input field's value is always ''.

What am I doing wrong?


After chaning the return value to the following:

return Redirect::to('login')->withInput(Input::only('username'));

and attempting to retrieve the value:

value="{{ Input::old('username') }}

Instead of 'Jon', the username field's value is set to 1.

I have no idea why.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongwan0574
    dongwan0574 2014-11-21 16:39

    You are sending the variable "input" with the magic function "with", and you are affecting an array to this variable, so in blade template you can access it with the following code :

    // If you want to keep the recent post inputs, use withInput without parameters
    return Redirect::to('login')->withInput();
    // and then use this code to echo it
     {{ Input::old('username') }}

    But I guess it would be better if you use Laravel's From and model binding like so :

    {{ Form::model($user, array('route' => array('user.update', $user->id)))
        {{ Form::label('username', 'Username : ') }}
        {{ Form::text('username') }}
    {{ Form::close() }}

    here the user will be binded to the object with the id in the route, for example : users/1 Laravel will fetch the user with the id of 1 and bind it to your form. But if we're talking about authentication or creation, there's no model binding needed, just use the laravel's form :

    {{ Form::open(array('route' => array('yourRouteName')))
        {{ Form::label('username', 'Username : ') }}
        {{ Form::text('username') }}
    {{ Form::close() }}

    Note that you can use model binding in your routes so when you will have access to the object directly in your controller, for example, I want to edit and object, and following the RESTFul architecture I'll have the following route :

    Route::put('users/{id}', array('as' => 'users.update', 'uses' => 'UserController@update'));

    then in the controller, I'll try to retrieve the user having the id passed in the URL, but when using model binding, i'll get a user object in my controller, you can do so, first by making a change to your route :

    // we create a model binding
    Route::model('user', 'User');
    // and we use it in our route instead of the id
    Route::put('users/{user}', array('as' => 'users.update', 'uses' => 'UserController@update'));

    now in our controller we can accept a user, and do whatever we want with that object :

    public function update($user)

    now when you have a link like : users/4 fetching the user with the ID of 4 is done automatically.

    点赞 评论