【问题标题】:Laravel 4 - username in URL changes to {Username} after pressing submitLaravel 4 - 按提交后 URL 中的用户名更改为 {Username}
【发布时间】:2014-03-14 16:57:57
【问题描述】:

我是 Laravel 新手,遇到语法问题。我正在尝试创建一个视图,使管理员能够更改用户的密码,但是当我单击提交时,页面刷新并且 URL 已替换用户名(例如:public/users/alex/edit 为 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'
                ));

和视图/刀片:

@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

【问题讨论】:

    标签: url laravel laravel-4


    【解决方案1】:

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

    祝你好运!

    迈克尔

    【讨论】:

    • 哦!好吧,我不知道使用 {{Form::open}} 会做到这一切!我马上试试,谢谢!
    • 很抱歉我还没来得及尝试一下。一个朋友试图“帮助我”,结果弄得一团糟,我不得不从头开始哈哈。但我一定会牢记这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    相关资源
    最近更新 更多