Laravel 4 - 按提交后URL中的用户名更改为{Username}

时间:2014-03-14 16:57:57

标签: url laravel laravel-4

我是Laravel的新手,我遇到了语法问题。我正在尝试创建一个视图,使管理员能够更改用户的密码,但是当我单击提交页面刷新并且URL已替换用户名(例如:public / users / alex / edit to public / users / {名} /编辑)。如果有人能解释为什么这不起作用,我将不胜感激!我做了类似的事情,用户可以更改自己的密码,而且似乎工作正常。我唯一的猜测是我没有正确地携带$ username,但我不知道如何做到这一点。非常感谢你们!任何信息都有帮助!

以下是视图的UserController:

public function getEdit ($username) {   
        $user = User::whereUsername($username)->first();
        return View::make('users.edit', ['user' => $user]);
}

public function postEdit($username){

        $validator = Validator::make(Input::all(),
            array(
                'password'          => 'required|min:6',
                'password_again'    => 'required|same:password'
            )
        );

        if($validator->fails()){
            return Redirect::route('user-edit')
                ->withErrors($validator)
                ->with('username', $username);
        } else {
            /*Change password*/
            $user           = User::whereUsername($username)->first();
            $password       = Input::get('password');
            $user->password = Hash::make($password); 
            /*password is the field $password is the variable that will be used in the password field*/

            if($user->save()){
                return Redirect::route('home')
                    ->with('global', 'The password has been changed.');
            }
        }
        return Redirect::route('account-change-password')
            ->with('global', 'The password could not be changed.');
    }

路线:

/*ADMIN - edit users (GET)*/
    Route::get('users/{username}/edit', array(
        'as'    => 'user-edit',
        'uses'  => 'UserController@getEdit'
    ));

/*ADMIN - edit users (POST)*/
                Route::post('users/{username}/edit', array(
                    'as'    => 'user-edit-post',
                    'uses'  => 'UserController@postEdit'
                ));

和View / Blade:

@extends('layout.main')

@section('content')
    <form action="{{ URL::route('user-edit-post') }}" method="post">

        <div class="field">
            New password: <input type="password" name="password">

            @if($errors->has('password'))
                {{$errors->first('password')}}
            @endif
        </div>

        <div class="field">
            New password again: <input type="password" name="password_again">

            @if($errors->has('password_again'))
                {{$errors->first('password_again')}}
            @endif
        </div>


        <input type="submit" value="Change Password">
        {{ Form::token() }}
    </form>
@stop

1 个答案:

答案 0 :(得分:2)

您似乎没有在表单内的任何位置传递用户名。您是否尝试过使用{{ Form::open(...) }}{{ Form::close() }}(请参阅http://laravel.com/docs/html)?这些函数将为您处理参数传递,并在必要时包含隐藏变量。

祝你好运!

米甲