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.

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

报告相同问题?

悬赏问题

  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊