php - 在控制器中将变量从一个函数转移到另一个函数
问题描述
我此时正在使用 Laravel 来保护页面,当用户在打开之前在模式表单上输入密码时。我初始化了一个名为 的变量$crypt
,它隐藏在表单中,以使每个页面都是唯一的(以防止其他人使用 URL 打开页面)。
我想将$crypt
数据传递给 PDFView。我怎样才能做到这一点?我已经尝试了很多东西,但都没有奏效。
错误
未定义变量:crypts
路线:
Route::get('/pdfview/{id}/', 'HomeController@pdfview')->name('pdfview');
生成的密钥代码
<div style="display: none">{{ $crypt = str_random(10)}}
控制器
public function encryptslip(Request $request, $crypt)
{
$crypts = $crypt;
$id = $request->nip;
$pass = $request->password;
$nip = Auth::user()->nip;
if (Hash::check($pass, Auth::user()->password)) {
return redirect('/pdfview/' . $nip . '/', ['crypts' => $crypts])->with('crypt', $crypt);
} else {
return redirect('/home')->with('alert', 'Incorrect password');
}
}
public function pdfview(Request $request, $id)
{
$route = url()->current();
$month = Carbon::now()->month;
$periodeinput = DB::table('payrollinput')->where('nip', '=', $id)->orderBy('periode', 'desc')->pluck('periode')->implode('periode');
$periodehistory = DB::table('payrollhistory')->where('nip', '=', $id)->orderBy('periode', 'desc')->pluck('periode')->implode('periode');
// if ($periodeinput !== $month && $periodehistory !== $month) {
// return redirect('home')->with('alert', 'Slip gaji anda siap.');
// } else {
if (Auth::user()->nip == $id) {
$employees = MasterEmployee::where('nip', '=', $id)->first();
$payrollhistory = MasterPayrollHistory::where('nip', '=', $id)->where('periode', '=', $month)->first();
$payrollinput = MasterPayrollInput::where('nip', '=', $id)->where('periode', '=', $month)->first();
view()->share('employees', $employees);
view()->share('payrollhistory', $payrollhistory);
view()->share('payrollinput', $payrollinput);
view()->share('id', $id);
// calculation code
return view('pdfview', ['id' => $id])->with('id', $id)
->with('earningtotal', $earningtotal)
->with('deductiontotal', $deductiontotal)
->with('takehomepay', $takehomepay)
->with('total', $total);
} else {
return redirect('home')->with('alert', 'Sorry it is personally confidential, you are not able to see it.');
}
}
看法
<div><{{$crypts}}</div>
解决方案
当您使用方法时,该变量作为变量return redirect()
传递给视图,并且必须以形式调用session
blade
<div>{{session('crypts')}}</div>
session
将此变量转换为$request
{{Form:hidden('crypts', json_encode(session('crypts'))}}
推荐阅读
- angular - 使用 Angular 9 安装 Firebase 时出错:firebase/app
- java - C# 的 DateTime.ToFileTimeUTC 在 java 中是否可比?
- python-3.x - 训练后损失和准确率保持不变
- redis - 如何在 Redis 中设置/重置/更改流顶部项目 ID?
- flutter - Flutter - 如何在一行中显示文本和图标?
- html - 如何水平对齐伪元素与元素及其容器
- java - 我们如何在 Sailpoint IIQ 中设置日期范围
- unit-testing - NestJS Jest 测试期待然后最终捕获
- python - 如果行的内容包含特定的子字符串,如何排除 Daraframe 中的列?
- google-classroom - 课堂主题 API 给了我帐户受限错误