将请求参数传递给View-Laravel [英] Passing request parameter to View - Laravel
本文介绍了将请求参数传递给View-Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以将route
参数传递给控制器,然后传递给laravel
中的视图?
Is it possible to pass a route
parameter to a controller to then pass to a view in laravel
?
示例;
我有下面的路线;
Route::get('post/{id}/{name}', 'BlogController@post')->name('blog-post');
我想将{id}
和{name}
传递给我的视图,以便在控制器中使用
I want to pass {id}
and {name}
to my view so in my controller
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'));
}
编辑:如果您需要将其返回为JSON,则只需执行以下操作:
EDIT If you need to return it as JSON you can simply do:
public function post ($id, $name)
{
return view('pages.blog.post', ['json' => json_encode(compact('name', 'id'))]);
}
这篇关于将请求参数传递给View-Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文