【发布时间】: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
【问题讨论】: