简体   繁体   中英

Laravel 5.1 - Return to edit page

i'm writing a resource controller, and i have a problem with edit method.

i inserted a validator form, and if there is a error return to edit page with messages, but RETURN doesnt work good!

public function update(Request $request, $id)
    {
       $rules = [
            'title' => 'required',
            'content' => 'required',
            'image' => 'required',    
        ];

        $messages = [        
            'title.required' => 'Campo titolo richiesto',
            'content.required' => 'Contenuto richiesto',
            'image.required' => 'Campo immagine richiesto',
        ];

        $validator = Validator::make($request->all(), $rules, $messages);

        if ($validator->fails()){

         return redirect('admin/article/edit' , $id)->withErrors($validator);   

        }else {


            $s = new Article;



                $visible = (isset($_POST['visible']) == '1' ? '1' : '0');

                $data = array(

                'title'          => $request->get('title'),
                'slug'          => $request->get('title'),
                'content'    => $request->get('content'),
                'image'            => $request->get('image'),
                'user_id'        => $request->get('user_id'),
                'category_id'    => $request->get('category_id'),
                'visible'        => $visible,
                );



             $s->where('id', '=', $id)->update($data);
             return redirect('admin/article')->with('message', 'Articolo aggiornato con successo!');


         }
    }

It return to:

admin/article/edit/5

NOT to

admin/article/5/edit

How can i fix this issue? thank you for your help!

PS: $id work well, return my id edited

So you're saying the redirect on failure doesn't redirect to the right URL? Have you tried doing return redirect('admin/article/' . $id . '/edit')->withErrors($validator); ?

I haven't tested this approach, but perhaps return redirect()->back()->withErrors($validator); could also work.

Here is the redirect helper. As you can see below, it takes status as its' second parameter.

function redirect($to = null, $status = 302, $headers = [], $secure = null)

What you do with passing the $id as the second parameter is actually setting the $status .

You need to pass the $to parameter as the full path like below.

return redirect('admin/article/' . $id . '/edit')->withErrors($validator);

I guess that you want to generate the url with route, which can be implemented like below.

return redirect(route('admin.article.edit', compact($id)))->withErrors($validator);

Here you have

return redirect('admin/article/edit' , $id)->withErrors($validator); 

means the link/route is admin/article/edit/$id (5 or 2 or ...)

better check

return redirect('admin/article/' . $id . '/edit')->withErrors($validator);

One way to do this, as others have suggested, is like following:

return redirect("admin/article/{$id}/edit")->withErrors($validator);

Or if you've a "Route Name" defined, like this..

return redirect()->route('route.name',[$id])->withErrors($validator);

it all depends on how you prefer, I prefer the later one, looks clean to me.

The redirect go to the passed url:

return redirect('admin/article/' . $id . '/edit'); #admin/article/id/edit
return redirect('admin/article/edit', $id); #admin/article/edit/5

And you can use methods to get this url:

return redirect(action('Controller@update', compact($id)));

Easiest solution:

Laravel 5.1 has a back() helper, that returns to the previous page:

return back()->withErrors($validator);

More thorough explanation:

If you want to be more verbose, a generally more robust way to redirect to a route is to first define it as a named route in your routes.php :

Route::get('admin/article/{article_id}/edit', ['as' => 'articles.edit', 'uses' => 'ArticlesController@edit']);
Route::bind('article_id', function($id, $route) {
    return App\Article::whereId($id)->findOrFail();
}

If you are using Route::resource instead, then this is already done automatically for you. To find the name of the route, run the command-line php artisan route:list . Then, in your controller method, you call it like this:

return redirect()->route('articles.edit', ['article_id' => $id])->withErrors($validator);

Using that kind of call, Laravel will automatically build the correct URL for you. This is more robust because if you ever want to change that URL to something else or change what controller method it calls, you only need to change it in one place, the routes.php , and not everywhere in your code (as long as every reference to that route in your code is referring to it by name).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM