Laravel > route, filter URL against database

时间:2015-10-29 15:41:56

标签: laravel routes laravel-5

I'm trying to build the new version of our site with the Laravel 5 framework.

The problem is, we have now the following site structure (and this may not be changed):

  • domain.com/[productname] <- dynamic
  • domain.com/[categoryname] <- dynamic
  • domain.com/some-static-page1
  • domain.com/some-static-page2
  • ....

In our database, we have a list of different product names and different categories.

I want to route the [productname] to a productController and the [categoryname] to the categoryController. But therefor, i need query our database with each request to see if the URL parameter is either a product or a category.

Any ideas to get me on right direction?

Thanks in advance!

1 个答案:

答案 0 :(得分:1)

我认为这是最好的方法:

路线:

Route::get('/cat={name}', 'CatagoryController@cataFind');
Route::get('/prod={name}', 'ProductController@prodfind');

控制器:

public function cataFind($name){
 //get all the rows with that cata
 $catas = Product::where('cata', $name)->get();
 return view('cata')->with('catas', $catas);
}

public function prodFind($name){
 //get all the rows with that prod name
 $prod = Product::where('name', $name)->get();
 return view('prod')->with('prod', $prod);
}

如果你需要任何关于正在发生的事情的解释,那么只需评论我将更新帖子。但这是你应该考虑的道路我应该想到的!