Laravel路由将变量传递给控制器

9 浏览
0 Comments

Laravel路由将变量传递给控制器

如何将一个硬编码的变量传递给控制器?

我的路由是:

Route::group(array('prefix' => $locale), function() {
    Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index'));
});

我想要做的是:

Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index(1)'));

但这样做不起作用。

怎么能做到呢?


如果我解释得不好,我很抱歉。

我希望简单地将type_id(由我固定的)硬编码到某些路由中,像这样:

Route::get('/milk', array('as' => 'milk', 'uses' => 'ProductsController@index(1)'));
Route::get('/cheese', array('as' => 'cheese', 'uses' => 'ProductsController@index(2)'));
...

供参考的我的ProductsController:

class ProductsController extends BaseController {
    public function index($type_id) {
        $Products = new Products;
        $products = $Products->where('type_id', $type_id)->get();
        return View::make('products.products', array('products' => $products));
    }
}

0