是否可以将route
参数传递给控制器,然后传递给laravel
中的视图?
示例;
我有下面的路线;
Route::get('post/{id}/{name}', 'BlogController@post')->name('blog-post');
我想把{id}
和{name}
传给我的视图,所以在我的控制器中
class BlogController extends Controller
{
//
public function post () {
//get id and name and pass it to the view
return view('pages.blog.post');
}
}
您可以使用:
public function post ($id, $name)
{
return view('pages.blog.post', ['name' => $name, 'id' => $id]);
}
甚至更短:
public function post ($id, $name)
{
return view('pages.blog.post', compact('name', 'id'));
}
EDIT如果您需要将其作为JSON返回,您可以简单地执行:
public function post ($id, $name)
{
return view('pages.blog.post', ['json' => json_encode(compact('name', 'id'))]);
}
这样的东西行吗?
class BlogController extends Controller
{
//
public function post ($id, $name) {
//get id and name and pass it to the view
return view('pages.blog.post', ['name' => $name, 'id' => $id]);
}
}