Question

I just started trying Laravel 4.1 today, and I've had to use a tutorial for Laravel 4.0, so I've had to troubleshoot certain parts of the code. There's one part I couldn't troubleshoot, and i need some help with it.

These are the routes involved:

Route::get('authors/{id}/edit', array('as'=>'edit_author', 'uses'=>'AuthorsController@get_edit'));

Route::put('authors/update', array('uses'=>'AuthorsController@put_update'));

and these are the actions in the controller:

public function get_edit($id){
   return View::make('authors.edit')->with('title', 'Edit Author')->with('author', Author::find($id));
}

public function put_update(){
    $id = Input::get('id');
    $author = array(
            'name' => Input::get('name'),
            'bio'  => Input::get('bio'),
            );
    $validation = Author::validate($author);
    if ($validation->fails()){
        return Redirect::route('edit_author', $id);
    }else{
        Author::update($id, $author);
        return Redirect::route('view_author', $id);
    }
}

Note that in the routes i'm using {id} instead of (:any), because the latter didn't work for me.

On my browser the get_edit function runs ok at first, but then, when i click the submit button and it's supposed to execute put_update, whether it's supposed to redirect me to view_author or back to edit_author, it just gives me a NoFoundHttpException.

Just as additional information, i use the default .htacces which is this one:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Was it helpful?

Solution

Since you are using 4.1 so it should be {id} not (:any) and make sure you are using the right way to generate form, like this:

Form::open(array('action' => array('AuthorsController@put_update', $author->id), 'method' => 'put'))

Also close the form using Form::close(). Since you are not using a RESTful controller so you can use a method name as update instead of put_update and for RESTful method use putUpdate not put_update. So, you may use a route like:

Route::put('authors/update', array('uses'=>'AuthorsController@update'));

Then the method should be:

public function update($id)
{
    // ...
    if ($validation->fails()){
        return Redirect::back()->withInput()->withErrors($validation);
    }
    else{
        Author::update($id, $author);
        return Redirect::route('view_author', $id);
    }
}

So the form should be like:

Form::open(array('action' => array('AuthorsController@update', $author->id), 'method' => 'put'))

Also change your edit route to this:

Route::get('authors/edit/{id}', array('as'=>'edit_author', 'uses'=>'AuthorsController@edit'));

Make the change in the method as well:

public function edit($id)
{
    //...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top