[英]Passing request parameter to View - Laravel
Is it possible to pass a route
parameter to a controller to then pass to a view in laravel
? 是否可以将route
参数传递给控制器,然后传递给laravel
的视图?
Example; 例;
I have the route below; 我有以下路线;
Route::get('post/{id}/{name}', 'BlogController@post')->name('blog-post');
I want to pass {id}
and {name}
to my view so in my controller 我想在我的控制器中将{id}
和{name}
传递给我的视图
class BlogController extends Controller
{
//
public function post () {
//get id and name and pass it to the view
return view('pages.blog.post');
}
}
Would something like this work? 会这样的吗?
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]);
}
}
You can use: 您可以使用:
public function post ($id, $name)
{
return view('pages.blog.post', ['name' => $name, 'id' => $id]);
}
or even shorter: 甚至更短:
public function post ($id, $name)
{
return view('pages.blog.post', compact('name', 'id'));
}
EDIT If you need to return it as JSON you can simply do: 编辑如果您需要将其作为JSON返回,您可以简单地执行:
public function post ($id, $name)
{
return view('pages.blog.post', ['json' => json_encode(compact('name', 'id'))]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.