将值从 url 传递到 Laravel 中的控制器
Passing value from url to controller in Laravel
我在 x.blade.pdp 文件中创建了一个标签
<a href="{{ URL::to('/certificate/pdf/'.$year) }}" class= "text-center">Print Certificate</a>
在web.php
Route::get('/certificate/pdf/{$year}','PDFController@export_pdf');
我的控制器
public function export_pdf( $year)
但是当我点击link时页面无法显示。我想在 where 子句中使用 $year .
拜托,我需要一些帮助
控制器函数应该是这样的
public function export_pdf( Request $request){
$year = $request->year;
}
路线应该是这样
Route::get('/certificate/pdf/{year}','PDFController@export_pdf');
如果年份是可选的那么路线是
Route::get('/certificate/pdf/{year?}','PDFController@export_pdf');
您必须删除 $
并使用:Route::get('/certificate/pdf/{year}','PDFController@export_pdf');
在您的 web.php 文件中。
在 routes/web.php
中,像您所做的那样定义您的路线(我通常也将名称放在末尾)。
Route::get('/someroute/route-url/{param1}', 'Directory\ControllerName@controller_function')->name('route-url-name');
然后你可以在你的 view/blade 中使用它的名字来描述那条路线,并传递参数:
{{ route('route-url-name', $param) }}
我在 x.blade.pdp 文件中创建了一个标签
<a href="{{ URL::to('/certificate/pdf/'.$year) }}" class= "text-center">Print Certificate</a>
在web.php
Route::get('/certificate/pdf/{$year}','PDFController@export_pdf');
我的控制器
public function export_pdf( $year)
但是当我点击link时页面无法显示。我想在 where 子句中使用 $year .
拜托,我需要一些帮助
控制器函数应该是这样的
public function export_pdf( Request $request){
$year = $request->year;
}
路线应该是这样
Route::get('/certificate/pdf/{year}','PDFController@export_pdf');
如果年份是可选的那么路线是
Route::get('/certificate/pdf/{year?}','PDFController@export_pdf');
您必须删除 $
并使用:Route::get('/certificate/pdf/{year}','PDFController@export_pdf');
在您的 web.php 文件中。
在 routes/web.php
中,像您所做的那样定义您的路线(我通常也将名称放在末尾)。
Route::get('/someroute/route-url/{param1}', 'Directory\ControllerName@controller_function')->name('route-url-name');
然后你可以在你的 view/blade 中使用它的名字来描述那条路线,并传递参数:
{{ route('route-url-name', $param) }}