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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?