我目前正在Laravel4上做一个演示应用程序。演示应用程序在数据库中有一些用户。我想逐个编辑它们。我有一个方法“postUpdate”,但是,在编辑(http://localhost/testlaravell/users/5/edit)列表中的用户期间,我看到错误发生 -
ErrorException (E_ERROR)
Route [users.postUpdate] not defined. (View: D:\wamp\www\testlaravell\local\app\views\users\edit.blade.php).
我在routes.php中有代码:
Route::get('/', function()
{
return View::make('hello');
});
Route::get('users/{all}/edit', 'UserController@getEdit');
Route::controller('users', 'UserController');
在UserController.php中,我编写了以下脚本进行编辑和更新:
public function getEdit($id)
{
//
$user = User::find($id);
if (is_null($user))
{
return Redirect::to('users/all');
}
return View::make('users.edit', compact('user'));
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function postUpdate($id)
{
//
$input = Input::all();
$validation = Validator::make($input, User::$rules);
if ($validation->passes())
{
//$user = User::find($id);
$user = User::find($id);
$user->username = Input::get('username');
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->phone = Input::get('phone');
$user->password = Hash::make(Input::get('password'));
$user->save();
return Redirect::route('users.getIndex', $id);
}
return Redirect::route('users.getEdit', $id)
->withInput()
->withErrors($validation)
->with('message', 'There were validation errors.');
}
edit.blade.php下的代码如下:
@extends('users.user')
@section('main')
<h1>Edit User</h1>
{{ Form::model($user, array('method' => 'PATCH', 'route' => array('users.postUpdate', $user->id))) }}
<ul>
<li>
{{ Form::label('username', 'Username:') }}
{{ Form::text('username') }}
</li>
<li>
{{ Form::label('password', 'Password:') }}
{{ Form::text('password') }}
</li>
<li>
{{ Form::label('email', 'Email:') }}
{{ Form::text('email') }}
</li>
<li>
{{ Form::label('phone', 'Phone:') }}
{{ Form::text('phone') }}
</li>
<li>
{{ Form::label('name', 'Name:') }}
{{ Form::text('name') }}
</li>
<li>
{{ Form::submit('Update', array('class' => 'btn btn-info')) }}
{{ link_to_route('users.getAll', 'Cancel', $user->id, array('class' => 'btn')) }}
</li>
</ul>
{{ Form::close() }}
@if ($errors->any())
<ul>
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
</ul>
@endif
@stop
我错过了什么,我不知道。有人能帮助我吗?
答案 0 :(得分:2)
实际上,如果要按名称调用路由,则应传递第三个参数 您的代码看起来像
Route::controller('users', 'UserController', ['postUpdate' => 'users.postUpdate']);
第三个参数是数组,其中键是你的方法,并且它是路由名称
答案 1 :(得分:1)