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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?