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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?