doutangdan3588 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 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 example.com/orders/view//1
    $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:
    https://laravel.com/docs/5.2/routing#named-routes

    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:
    https://laravel.com/docs/5.2/routing#route-model-binding

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求一个解密绿盾的代码或者程序,有偿!
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到