【问题标题】:Laravel 4 - Controller method not foundLaravel 4 - 找不到控制器方法
【发布时间】:2016-04-04 08:09:57
【问题描述】:

我想修改列表中的用户。我在 routes.php 中有代码:

Route::get('users/{all}/edit', 'UserController@getEdit');
Route::post('users/update', ['as' => 'users.postUpdate', 'uses' => 'UserController@postUpdate']);
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

编辑屏幕打开良好。但是,当修改值并提交表单时,URL显示为http://localhost/testlaravell/users/update?5,并出现错误 -

Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException
Controller method not found.

请帮助我如何解决这个问题。

【问题讨论】:

    标签: laravel laravel-4 laravel-5 laravel-routing


    【解决方案1】:

    这里的问题是您使用PATCH 方法,但在控制器中您有方法postUpdateroutes.php 也是如此)。

    你应该做的是:

    • 将控制器方法 postUpdate 更改为 putUpdate
    • 改变

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

      进入

      Route::put('users/update/{id}', ['as' => 'users.putUpdate', 'uses' => 'UserController@putUpdate']);
      
    • 改变你的形式

      {{ Form::model($user, array('method' => 'PATCH', 'route' => array('users.postUpdate', $user->id))) }
      

      进入

      {{ Form::model($user, array('method' => 'PUT', 'route' => array('users.putUpdate', $user->id))) }
      

    【讨论】:

    • 另一个正确答案@Marcin。非常感谢。你是 Laravel 大师。
    【解决方案2】:

    这样定义你的路线:

    Route::resource('users', 'UsersController');
    

    改变它:

    {{ Form::model($user, array('method' => 'PATCH', 'route' => array('users.postUpdate', $user->id))) }}
    

    {!! Form::model($user, array('method' => 'PATCH', 'route' => array('users.update', $user->id))) !!}
    

    这样写你的控制器:

    public function edit($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 update($id)
    {
        $input = Input::all();
        $validation = Validator::make($input, User::$rules);
        if ($validation->passes())
        {
    
                $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.');
    }
    

    希望,它会起作用。

    【讨论】:

      猜你喜欢
      • 2014-03-09
      • 2014-05-12
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 2014-01-04
      • 1970-01-01
      相关资源
      最近更新 更多