mercredi 30 décembre 2015

Laravel 4 - Controller method not found

I want to modify an user from the list. I have the codes in the routes.php:

Route::get('users/{all}/edit', 'UserController@getEdit');
Route::post('users/update', ['as' => 'users.postUpdate', 'uses' => 'UserController@postUpdate']);
Route::controller('users', 'UserController');

In the UserController.php, I wrote the following script for edit and update:

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.');
}

The code under edit.blade.php as below:

@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

The edit screen is opening well. However, when modify the values and submit the form, the URL shows as http://localhost/testlaravell/users/update?5 and the error occurs that -

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

Please help me how can I solve this issue.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1QZ25Zw
via IFTTT

Aucun commentaire:

Enregistrer un commentaire