Php Laravel-将变量从一个函数传递到另一个函数

Php Laravel-将变量从一个函数传递到另一个函数,php,laravel,Php,Laravel,这是将id从一个函数传递到另一个函数的正确方法吗 return Redirect::to('uploads/show', $id); 或者我应该使用 return Redirect::to('uploads/show')->with($id); 我试图将id传递给的函数 public function getShow($id) { $entryid = $this->upload->findOrFail($id); $this->layout->con

这是将id从一个函数传递到另一个函数的正确方法吗

return Redirect::to('uploads/show', $id);
或者我应该使用

return Redirect::to('uploads/show')->with($id);
我试图将id传递给的函数

public function getShow($id)
{
  $entryid = $this->upload->findOrFail($id);

  $this->layout->content = View::make('uploads.show', compact('entryid'));
}
我得到的错误是

Missing argument 1 for UploadsController::getShow()

使用
重定向时
应执行以下操作:

return Redirect::to('uploads/show/'.$id);

要通过路由器将变量传递给控制器

若要尽可能正确地传递变量,需要正确地创建URL。前面使用字符串连接的答案没有错误,但更好的方法可能是:

return Redirect::to(URL::to('uploads/show', [$id]));
但您会发现,当您获得更多路由时,尤其是第一次决定修改路由约定时,使用命名路由(或至少使用基于控制器的路由和
Route::controller
)可能会更好。在这种情况下,情况类似:

// routes.php
Route::get('uploads/show/{id}', ['as' => showUpload', 'uses' => 'Controller@action']);

// redirect:
return Redirect::route('showUpload', [$id]);
还要注意如何在这里直接传递参数(而不是使用
URL::to()
),因为使用命名路由不是上面提到的简单字符串路由

类似地,如果使用基于控制器的路由:

// routes.php
Route::Controller('uploads', 'Controller'); // I don't use this, syntax is like this

// redirect:
return Redirect::action('Controller@getShow', [$id]); // again I don't use this, but it's similar

谢谢你的回答,暂时来说,上面的答案就可以了。非常感谢你的回答,效果非常好。