首页 > 解决方案 > 最后一个 Laravel 控制器功能丢失数据

问题描述

我正在将参数从一个发送到另一个而不通过路由传递它们以另一种方式传递它们以确保安全,但最后一个函数的问题是它丢失了参数甚至dd()不起作用,它只返回未定义的视图变量$data

控制器

// Pass id to test.auth
public function show_auth($id)
{
    return view('test.auth', compact('id'));
}

// Here it gets id and dd($data) works
public function post_auth(Request $request)
{
    $id = $request->id;

    $data = DB::table('kuponai')->where('id', $id)->get();
    //dd($data);
    
    return view('test.view', compact('data'));
}

// Function from before dump doesn't work and returns view
public function view($data)
{
    dump($data);

    return view('test.view');
}

为此更改也不起作用...

public function check(Request $request)
    {
      $id = $request->id;

      //
      
      return view('test.auth',compact('id'));
      
    }
    public function show_auth($id)
    {
        return view('test.auth',compact('id'));
        
    }
    public function post_auth(Request $request)
    {
      $id = $request->id;
      $pin = $request->pin;
      $mail = $request->elpastas;

      $data = DB::table('kuponai')
      ->where('id', $id)
      ->get();
      //dd($data);

      return route('view.show',compact('data'));
    }
    public function view($data)
    {
      $d = $this->post_auth($data);
      //dd($data);
        return view('test.view');
    }

标签: laravelmodel-view-controllerrequestlaravel-8

解决方案


这可能是因为您传递的数据变量直接发送到test.view视图。所以永远不要传递你做的视图函数($data)dump($data)

($ 数据不是通过路由发送,而是直接发送到视图中)!!

我似乎知道解决方案,但请告诉我你的 routes.php 片段

路线:

Route::group(['domain' => 'coupons.domain.com'], function()
{

        Route::get('/', 'App\Http\Controllers\IsoreController@index')->name('view.klientas');
        Route::post('/check', 'App\Http\Controllers\IsoreController@check')->name('check')->middleware('checkid');
        // view authenticated coupon
        Route::get('/view', 'App\Http\Controllers\IsoreController@view')->name('view.show')->middleware('checkpin');
        // show coupon authenticate form
        Route::get('/auth','App\Http\Controllers\IsoreController@show_auth')->name('show.auth')->middleware('checkid');
        // handle user input, authenticate coupon
        Route::post('/authenticate','App\Http\Controllers\IsoreController@post_auth')->name('post.auth')->middleware('checkpin');
});

尝试更换return view('test.auth', compact('data'));

和: return redirect('/view')->with(compact('data'));

我的中间件:

检查ID:

class KuponoId {

  public function handle($request, Closure $next)
  {
        $exists =Kuponas::where('id', $request->id)->exists();
        if (!$exists) {
            return \Redirect::to(url('/'))->with('pin','Duomenys neteisingi, patikrinkite ');
        }
        return $next($request);
  }
}

检查针:

class CheckPin {

  public function handle($request, Closure $next)
  {
        $exists = Kuponas::where('patvirtinimo_kodas', $request->pin)->where('elpastas', $request->elpastas)->where('id', $request->id)->exists();
        //dd($exists);
        if (!$exists) {
          //return \Redirect::to(url('/'))->with('pin','Duomenys neteisingi, patikrinkite ');
          return \Redirect::to(url('/'))->with('pin','Duomenys neteisingi! ');
        }
        return $next($request);
  }
}

我只需要在用户正确验证的那一次显示信息视图

好的,这个怎么样?

return redirect()->action( 'TheController@view', ['id' => 1] );


推荐阅读