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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里