通过控制器从路径到视图传递参数不起作用

时间:2014-05-12 13:40:35

标签: php laravel laravel-4

我正在创建一个Laravel 4 webapp并获得以下路线:

Route::get('products/{whateverId}', 'ProductController@index');

这是我在ProductController中的索引函数:

    public function index($whateverId)
{
    $products = Product::all();
    $data['whateverId'] = $whateverId;
    return View::make('products', compact('products'), $data);
}

在我看来,这会返回以下错误:

<p>Product: {{ $data['product'] }}</p>
  

ErrorException   未定义的变量:data(查看:/Users/myuser/webapp/app/views/products.blade.php)

2 个答案:

答案 0 :(得分:3)

return View::make('products', compact('products'), "data"=>$data);

(或compact('data')

答案 1 :(得分:1)

尝试将其传递为:

$data['whateverId'] = $whateverId;

$data['products'] = Product::all();;

return View::make('products', $data);

你可以访问

{{ foreach($products as ...) }}

{{ $whateverId }}

或者你可以

$products = Product::all();

$data['whateverId'] = $whateverId;

return View::make('products')
         ->with('products', $products)
         ->with('whateverId', $whateverId);