dqs13465424392 2014-11-21 16:01
浏览 69
已采纳

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'));
    $user->save();
    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?

UPDATE:

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 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)
    {
        var_dump($user);
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大