将变量从按钮传递到控制器 Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40258599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing variable from button to controller Laravel
提问by Anna Jeanine
I am having a little routing problem in Laravel 5.2. I have a result page which shows detailed information about personnel. I would like a button, which when enabled, generates a PDF page. Passing the variables has been a problem but I am very close now! I will public my code to elaborate.
我在 Laravel 5.2 中遇到了一个小路由问题。我有一个结果页面,其中显示了有关人员的详细信息。我想要一个按钮,启用后会生成一个 PDF 页面。传递变量一直是个问题,但我现在已经很接近了!我将公开我的代码来详细说明。
result page
结果页面
<form action="generatePDFpage" method="get">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</form>
routes.php
路由文件
Route::get('/dashboard/result/generatePDFpage', 'resultController@GeneratePDFc');
GeneratePDFc controller
生成PDFc控制器
public function GeneratePDFc(){
$id_array_implode = "HALLO";
$pdf= PDF::loadView('GeneratePDF', ["test"=>$id_array_implode])->setPaper('a4', 'landscape');
return $pdf->stream('invoice.pdf');
}
So, on the result page I am using a array ($id_array) to search the database for the matching records. I need to pass this variable onto the GeneratePDFc controller, so that I can pass that again to the loadView function!
因此,在结果页面上,我使用数组 ($id_array) 在数据库中搜索匹配的记录。我需要将此变量传递给 GeneratePDFc 控制器,以便我可以再次将其传递给 loadView 函数!
Could someone please help me out? :-)
有人可以帮我吗?:-)
回答by Alexey Mezenin
When you're using get
method, you can do just this:
当您使用get
方法时,您可以这样做:
<a href="{{ route('route.name', $parameter) }}">
<button type="submit" class="btn btn-default">Generate PDF!</button>
</a>
For other methods you can use something like this (this one is for DELETE
method):
对于其他方法,您可以使用类似的DELETE
方法(此方法用于方法):
<form method="POST" action="{{ route('route.name', $parameter) }}" accept-charset="UTF-8">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<button type="submit" class="btn btn-sm btn-default">Generate PDF!</button>
<input type="hidden" value="someVariable" />
</form>
To get variable, use something like this:
要获取变量,请使用以下内容:
public function generatePDF(Request $request)
{
$someVariable = $request->someVariable;
回答by MounirOnGithub
I don't know Laravel but I think when in your action=""
of the form you can put your route with its parameters no ?
我不知道 Laravel,但我认为当你action=""
以你的形式时,你可以用它的参数来放置你的路线吗?
I've found it here : https://laravel.com/docs/4.2/html#opening-a-form
我在这里找到了:https: //laravel.com/docs/4.2/html#opening-a-form
And access the variable in your controller using the $request
var
并使用$request
var访问控制器中的变量