laravel - 最后一个 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');
}
解决方案
这可能是因为您传递的数据变量直接发送到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] );
推荐阅读
- ios - Xcode 9 Ios11 -> UITableViewController 不会显示任何东西
- kubernetes - Kubernetes 连接在部署期间被拒绝
- mysql - 无法将 AXIS2 Web 服务与 mysql DB 连接。收到以下问题
- python - Python(CPython 实现)函数可以容纳多少个局部变量?
- javascript - 使用 jquery 或 js 当光标在此 div 之外时如何滚动 div 的内容?
- uwp-xaml - 我的透视视图中的 UWP 添加窗格
- java - 带有 JFrame 和 JButton 的 Java ActionListener
- list - 在 Prolog 中将两个列表按组件相乘
- java - 用时间〜O(N)在数组中查找最大不可重复值
- ruby-on-rails - 发送后访问响应状态