首页 > 解决方案 > 在控制器中将变量从一个函数转移到另一个函数

问题描述

我此时正在使用 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>

标签: phplaravel

解决方案


当您使用方法时,该变量作为变量return redirect()传递给视图,并且必须以形式调用sessionblade

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

session将此变量转换为$request

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


推荐阅读