douju5933 2014-10-12 08:35
浏览 19
已采纳

laravel两条路线非常相似,呼叫另一条路线

Hi there I have two routes that look identical except that one has an extra paramter called genre how do make them call their on routines and not have them mixed up.

Route::get('browse/{product_slug}', array(
    'as' => 'products.view_by_producttype', 
    function($slug)         
    {   
    }
))->where('product_slug', '[A-Za-z\-]+');

Route::get('browse/{producttype_slug}/{genre_slug}', array(
    'as' => 'products.view_by_producttype_genre', 
    function($productype_slug, $genre_slug) 
    {       
    }   
))->where('producttype_slug', '[A-Za-z\-]+')->where('genre_slug', '[A-Za-z\-]+');

Upated Code and order:

Route::get('browse/{producttype_slug}', array(
    'as' => 'products.view_by_producttype', 
    function($producttype_slug)         
    { 
        $producttype = ProductTypes::where('slug', '=', $producttype_slug)->firstOrFail();      

        $items = DB::table('products')->join('productvariations', function($join) {
            $join->on('productvariations.product_id', '=', 'products.id');
        })->where('producttype_id', '=', $producttype->id)->paginate(1);

        return View::make('products.view_by_producttype')->with(compact('items', 'producttype'));

    }
))->where('producttype_slug', '[A-Za-z\-]+');


Route::get('browse/{producttype_slug}/{genre_slug}', array(
    'as' => 'products.view_by_producttype_genre', 
    function($producttype_slug, $genre_slug) 
    { 
        $producttype = ProductTypes::where('slug', '=', $producttype_slug)->firstOrFail();

        $genre = GenreTypes::where('slug', '=', $genre_slug)->firstOrFail();

        $items = DB::table('products')->join('product_genretypes', function($join) {
            $join->on('product_genretypes.product_id', '=', 'products.id');             
        })->join('productvariations', function($join) {
            $join->on('productvariations.product_id', '=', 'products.id');
        })
        ->where('genretype_id', '=', $genre->id)
        ->where('producttype_id', '=', $producttype->id)
        ->paginate(1);

        return View::make('products.view_by_type_genre')->with(compact('items', 'producttype', 'genre'));

    }   
))->where('producttype_slug', '[A-Za-z\-]+')->where('genre_slug', '[A-Za-z\-]+');
  • 写回答

1条回答 默认 最新

  • dongqian7545 2014-10-12 08:37
    关注

    Just swap the order - have the Genre route defined first - like this:

    Route::get('browse/{producttype_slug}/{genre_slug}', array(
        'as' => 'products.view_by_producttype_genre', 
        function($productype_slug, $genre_slug) 
        {       
        }   
    ))->where('producttype_slug', '[A-Za-z\-]+')->where('genre_slug', '[A-Za-z\-]+');
    
    Route::get('browse/{product_slug}', array(
        'as' => 'products.view_by_producttype', 
        function($product_slug)         
        {   
        }
    ))->where('product_slug', '[A-Za-z\-]+');
    

    That way - if a route matches with a Genre route - it will be used first.

    Otherwise it will default back to the normal Product route.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大