2015-04-06 247 views
1

我正在開發電子商務應用程序。我需要根據url_key(url的一列)動態產品/類別url。Laravel動態路由

我試圖做這樣的事情:

if (Route::has('route.name')) { 
     // go where ever it is intended 
    } 
    else { 

     $productHelper = new ProductHelper(); 

     $product = $productHelper->getProductByUrl($url_key); 
     if ($product) 
      return Redirect::to('product')->with('id', $product->id); 
     else { 
      $categoryHelper = new CategoryHelper(); 

      $category = $categoryHelper->getCategoryByUrl($url_key); 

      if ($category) 
       return Redirect::route('category')->with('id', $category->id); 
      else 
       return View::make('static.page_not_found'); 
     } 
    } 

讓我們說我們有像URL:

/約 - 我們 /接觸我們

/索尼BRAVIA -b32(產品的url密鑰) /電視主導(一個類別的URL鍵)

現在我想檢查,如果一個url存在於「routes.ph p「文件,那麼它應該去那個網址(與數據一樣,如果提交表格)

否則,它會檢查url是否匹配產品或類別的url key並相應地移動。

Magento行爲的排序。我卡在if部分。

回答

2

您可以使用routes.php來實現。

這是你怎麼做的例子。在實際的應用程序中,您可能需要爲每個路徑創建單獨的控制器來處理應用程序的不同行爲。

routes.php文件

Route::get('/about-us', function(){ 
    echo 'About Us'; 
}); 

Route::get('/contact-us', function(){ 
    echo 'Contact Us'; 
}); 


Route::get('/{product}/{category}', function($product, $category){ 
    dd($product,$category); // remove this line and put your business logic here 
}); 

確保你把靜態URL上方的動態之一。

+0

感謝mininoz,把動態路線放在最後解決了它。 – Ashutosh