2017-10-27 03:52 阅读 41

Laravel 5.2重定向使用WildCard ID无法正常工作

I got an error with redirecting I dont know why, i read the documentation of laravel 5.2 about redirecting and still not working pls help me thanks, here is my route in the routes.php

Route::get('/orders/view/{id}', 'OrderController@view');

here is my code in the controller for the redirecting

return redirect()->route('/orders/view/', ['id' => 5]);

and still it gives me this error

InvalidArgumentException in UrlGenerator.php line 314: Route [/orders/view/] not defined.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongpu4141 dongpu4141 2017-10-27 03:54

    The Problem

    You're trying to redirect to a route by it's name, but you're passing an URL to the method.

    return redirect()       # Initialize a redirect...
           ->route(...);    # to a route by it's name

    The solutions

    There are multiple ways to solve this. You can either redirect by path (that's how you're trying to do at the moment), or by using route names.

    Method 1: Redirect by path

    You can redirect by path in two ways: - concat the path by hand - use the URL facade.

    Concat the path by hand

    $url = '/orders/view/' . $order->id;
    return redirect($url);

    Use the URL facade

    # Remember to remove the trailing slash.
    # Appending a trailing slash would lead to
    $url = URL::to('/orders/view', [$order->id]);
    return redirect($url);

    You're done!

    Method 2: Redirect by route name

    To use route names, you need to prepare your routes and give them a name.
    I recommend this way, because sometimes it's more clear but it also makes maintaining your application easier when you need to change some paths, because the names will be still the same. Read more about it here:

    This also allows you to use the easy-to-use and useful route() helper method.

    Prepare your routes file
    To assign your routes a name, you need to pass them to your router in your app/Http/routes.php.

    # Example 1
    Route::get('/orders/view/{id}', [
        'as' => 'orders.view', 'uses' => 'OrderController@view'
    # Example 2
    Route::get('/orders/view/{id}', 'OrderController@view')->name('orders.view');

    For myself, I recommend to use the second syntax. It makes your code more readable.

    Redirect by route name
    Now it's time to redirect! If you're using the method of route naming, you don't even have to change much in your code.

    return redirect()->route('orders.view', ['id' => $order->id]);

    A note from my side

    Have a look on route-model-binding in the documentation:

    This allows you to pass a model instance directly to the route() method and it helps you fetching models by passing the proper model to your controller method like this:

    # Generate route URL
    route('orders.view', $order);
    # Controller
    public function view(Order $order) {
        return $order;

    And in your app/Http/routes.php you would have {order} instead of {id}:

    Route::get('/orders/view/{order}', 'OrderController@view');
    点赞 评论 复制链接分享
  • dskm94301 dskm94301 2017-10-27 04:01

    You can use

    return redirect()->action('OrderController@view', ['id' => 5]);


    // $id = 5
    return redirect()->to('/orders/view/'.$id);
    点赞 评论 复制链接分享