2015-12-30 83 views
0

我目前正在Laravel4上進行演示應用。演示應用程序在數據庫中有一些用戶。我想一一編輯它們。我有一個方法「postUpdate」,然而,編輯(http://localhost/testlaravell/users/5/edit)從列表中的用戶時,我看到發生錯誤 -Laravel 4 - 路由[users.postUpdate]未定義

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', '[email protected]'); 
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

您的路線不具名的路線。你必須在路由器中以爆炸性的方式命名它:Route :: post('users/update','UserController @ postUpdate') - > name('users.postUpdate'); – naneri

+0

建議:您可能想使用[REST全局資源](https://laravel.com/docs/4.2/controllers#restful-resource-controllers)控制器,而不是使用隱式控制器混合基本路由。 – Mirceac21

+0

@naneri在路由中實現代碼後,出現錯誤 - 調用未定義的方法Illuminate \ Routing \ Route :: name() –

回答

1

當使用Route:controller時,你將不會有任何命名的路線。

當你運行:

php artisan routes 

你會看到,你將有name爲空控制器的路線,所以你需要單獨創建路線,例如,您postUpdate路線:

Route::post('users/update', ['as' => 'users.postUpdate', 'uses' => '[email protected]']); 

編輯

示例路由列表(但來自Laravel 5 - 所以它可能有點不同)

Route list preview

你也應該閱讀carefeully @xAoc回答 - 因爲這可能是更好的解決方案(我幾乎從來不使用controller法)

+0

Thank!it works。但是,命令「php artisan route:list」不起作用。我可以使用「php artisan routes」命令嗎?另外,我怎麼能看到控制器路由的名稱(比如postUpdate方法)是空的。你能爲此發佈截圖嗎? –

+0

@Niladri請看我的編輯。我修正了這個問題。命令'php artisan route:list'可在Laravel 5 –

+0

再次感謝!我理解這個問題。但是在點擊「編輯用戶」頁面上的提交按鈕後,它會顯示以下錯誤: Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException 未找到控制器方法。 –

2

其實,你應該通過第三個參數,如果你想通過名字來稱呼航線 你的代碼將看起來像

Route::controller('users', 'UserController', ['postUpdate' => 'users.postUpdate']); 

第三個參數是數組,其中關鍵是你方法和價值它是你的路線名稱

+0

再次感謝!我理解了這個問題,但點擊'編輯用戶'頁面上的提交按鈕後,它顯示以下錯誤: Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException Controller method not found。 –