我正在尝试进入Laravel 4并且遇到了编辑我创建的用户的问题。到目前为止,我有控制器,视图和路径来显示用户,然后通过单击“编辑”按钮编辑用户,但是当我单击提交按钮时,我不断收到NotFoundHttpException和“Crash / Error”橙色和白色Laravel屏幕。但是,我注意到URL显示用户名(ex - public / users / av1 / edit - av1是用户名)只改为{username}(ex -public / users / {username} / edit) 。我仍然是Laravel的新手,但我的想法是我没有正确传递用户名,但我知道它也可能是控制器或路由。我已经尝试删除和更改代码部分,并发现如果我从控制器中删除代码我仍然得到一个带有{username}的URL,但我至少没有得到“崩溃/错误”屏幕。如果有人能帮我解释我哪里出错了,我们将非常感激!
以下是我的观点:
@extends('layout.main')
@section('content')
{{ Form::model($user, array('route'=>'user-edit-post')) }}
<div>
{{ Form::label('username', 'Username:')}}
{{ Form::text('username') }}
@if($errors->has('username'))
{{ $errors->first('username') }}
@endif
</div>
<div>
{{ Form::label('password', 'Password:')}}
{{ Form::password('password') }}
@if($errors->has('password'))
{{ $errors->first('password') }}
@endif
</div>
<div>
{{ Form::label('password_confirmation', 'Confirm Password:')}}
{{ Form::password('password_confirmation') }}
@if($errors->has('password_confirmation'))
{{ $errors->first('password_confirmation') }}
@endif
</div>
<div>
{{ Form::submit('Edit User') }}
</div>
@stop
与编辑有关的我的用户控制器功能:
public function getEdit($username){
$user = User::where('username', '=', $username);
if($user->count()) {
$user = $user->first();
return View::make('users.edit')
->with('user', $user);
} else {
return App::abort(404);
}
}
public function postEdit($username){
$validator = Validator::make(Input::all(),
array(
'first_name' => 'required|max:20',
'last_name' => 'required|max:20',
'email' => 'required|max:50|email',
'username' => 'required|max:20|min:3',
'password' => 'required|min:6',
'password_confirmation' => 'required|same:password'
)
);
if($validator->fails()){
return Redirect::route('user-edit')
->withErrors($validator);
} else {
/*Edit User*/
$user = User::whereUsername($username)->first();
$password = Input::get('password');
$user->password = Hash::make($password);
$user->first_name = Input::get('first_name');
$user->last_name = Input::get('last_name');
$user->email = Input::get('email');
$user->username = Input::get('username');
/*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('home')
->with('global', 'The password could not be changed.');
}
}
最后我的路线:
/*Edit users (GET)*/
Route::get('users/{username}/edit', array(
'as' => 'user-edit',
'uses' => 'UserController@getEdit'
));
/*Edit Order (POST)*/
Route::post('/orders/{orders}/edit', array(
'as' => 'order-edit-post',
'uses' => 'OrderController@postEdit'
));
答案 0 :(得分:2)
更改
{{ Form::model($user, array('route'=>'user-edit-post')) }}
要
{{ Form::model($user, array('route'=>array('user-edit-post', $user->username))) }}
您的路线需要额外的参数,因此在将模型绑定到表格时,您需要提供带有路线名称的参数。
是的,看完评论后。我发现你遇到麻烦的原因是postEdit
功能中的重定向:
变化:
if($validator->fails()){
return Redirect::route('user-edit')
->withErrors($validator);
}
向
if($validator->fails()){
return Redirect::route('user-edit', $username)
->withErrors($validator);
}
同样,您的路线需要参数。验证失败后,您已被重定向到错误的URL。