在控制器中将变量从一个功能转移到另一个功能

时间:2019-03-29 19:40:17

标签: php laravel

当用户在打开页面之前以模态形式输入密码时,我现在正在使用Laravel保护页面。我初始化了一个名为$crypt的变量,该变量隐藏在表单中,以使每个页面都是唯一的(以防止其他人使用URL打开页面)。

我想将$crypt数据传递给PDFView。我怎样才能做到这一点?我已经尝试了很多东西,但是都没用。

错误

  

未定义变量:地穴

路线:

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>

1 个答案:

答案 0 :(得分:1)

当您使用return redirect()方法时,该变量将作为session变量传递到视图,并且在blade中,它必须称为form

<div>{{session('crypts')}}</div>

session上转换此$request变量

{{Form:hidden('crypts', json_encode(session('crypts'))}}