重定向进入空白页面而不重定向到路由 Laravel 5.8
Posted
技术标签:
【中文标题】重定向进入空白页面而不重定向到路由 Laravel 5.8【英文标题】:Redirect goes blank page without redirecting to route Laravel 5.8 【发布时间】:2019-10-26 12:42:35 【问题描述】:在保存数据并显示成功消息后,我试图从控制器重定向到上一页。它进入空白页面,如果我重新加载页面,则会出现会话消息。但直接它不会重定向到上一页。我使用了其他建议的重定向方法,但仍然遇到同样的问题。我使用了以下代码:
控制器
public function saveMachine(Request $request)
$number_of_lockers = $request->number_of_lockers;
if($number_of_lockers == '32')
$a_machine = A_Machine::all();
if($a_machine->isEmpty())
$this->saveFirstMachine(32, $request);
else
$this->saveNewMachine(32, $request);
else if($number_of_lockers == '64')
$a_machine = B_Machine::all();
if($a_machine->isEmpty())
$this->saveFirstMachine(64, $request);
else
$this->saveNewMachine(64, $request);
else if($number_of_lockers == '128')
$a_machine = C_Machine::all();
if($a_machine->isEmpty())
$this->saveFirstMachine(128, $request);
else
$this->saveNewMachine(128, $request);
private function saveNewMachine($number_of_lockers, $request)
$machine = null;
$machine_code = '';
$locker_start=0;
$locker_end=0;
if($number_of_lockers == 32)
$machine = new A_Machine;
$date = \Carbon\Carbon::now()->format('Y-m-dH:i:s');
$machine_code = 'm032'.str_replace(array('20', '-', ':'), array('', '', ''), $date);
$last_record = $machine->all()->last();
$last_locker_end = $last_record->locker_end;
$locker_start = $last_locker_end + 1;
$locker_end = $locker_start + 31;
if($number_of_lockers == 64)
$machine = new B_Machine;
$date = \Carbon\Carbon::now()->format('Y-m-dH:i:s');
$machine_code = 'm064'.str_replace(array('20', '-', ':'), array('', '', ''), $date);
$last_record = $machine->all()->last();
$last_locker_end = $last_record->locker_end;
$locker_start = $last_locker_end + 1;
$locker_end = $locker_start + 63;
if($number_of_lockers == 128)
$machine = new C_Machine;
$date = \Carbon\Carbon::now()->format('Y-m-dH:i:s');
$machine_code = 'm128'.str_replace(array('20', '-', ':'), array('', '', ''), $date);
$last_record = $machine->all()->last();
$last_locker_end = $last_record->locker_end;
$locker_start = $last_locker_end + 1;
$locker_end = $locker_start + 127;
$machine->vendor_id = $request->vendor_id;
$machine->locker_start = $locker_start;
$machine->locker_end = $locker_end;
$machine->machine_code = $machine_code;
$machine->qr_code = 'test code';
$machine->temperature = '123';
$machine->humidity = '123';
$machine->fan_status = 'off';
if($machine->save())
$saved = $this->saveLocker($number_of_lockers, $machine->id);
if($saved)
return redirect()->route('create_machine')->with('success','A New Machine with '.$number_of_lockers.' Lockers Added Successfully');
private function saveLocker($number_of_lockers, $machine_id)
$lockers = [];
if($number_of_lockers == 32)
for ($i=0; $i < 32 ; $i++)
$lockers[$i]['machine_id'] = $machine_id;
$lockers[$i]['product_id'] = 0;
$lockers[$i]['created_at'] = now();
$lockers[$i]['updated_at'] = now();
return DB::table('a_machine_lockers')->insert($lockers);
if($number_of_lockers == 64)
for ($i=0; $i < 64 ; $i++)
$lockers[$i]['machine_id'] = $machine_id;
$lockers[$i]['product_id'] = 0;
$lockers[$i]['created_at'] = now();
$lockers[$i]['updated_at'] = now();
return DB::table('b_machine_lockers')->insert($lockers);
if($number_of_lockers == 128)
for ($i=0; $i < 128 ; $i++)
$lockers[$i]['machine_id'] = $machine_id;
$lockers[$i]['product_id'] = 0;
$lockers[$i]['created_at'] = now();
$lockers[$i]['updated_at'] = now();
return DB::table('c_machine_lockers')->insert($lockers);
刀片
@if(session('success'))
<div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<i class="fa fa-check mx-2"></i>
session('success')
</div>
@endif
基本上上一页路由是:
Route::get('create_machine', 'AdminController@createMachine')->name('create_machine');
有控制器方法:
public function createMachine()
$vendors = \App\Vendor::all();
return view('admin.create_machine', compact('vendors'));
N:B:我在我的项目中使用多重身份验证。
【问题讨论】:
你能发布发生重定向的完整方法吗 我已经通过给出完整的方法进行了编辑。该方法工作正常。但只是它没有重定向到上一页。 重定向到哪里 表单从这个路由提交。保存表单后,它应该回到这个 route.Route::get('create_machine', 'AdminController@createMachine')->name('create_machine'); 但是该页面现在在哪里 【参考方案1】:尝试路由重定向:
return redirect()->route('create_machine')->with('success','The First Machine with '.$number_of_lockers.' Lockers Added Successfully');
在视图中更改消息显示如下:
@if(session()->has('success'))
<div class="alert alert-success">
session()->get('success')
</div>
@endif
@if(session()->has('error'))
<div class="alert alert-danger">
session()->get('error')
</div>
@endif
此外,您似乎还没有处理 else case
的保存方法。所以也加一下。
喜欢:
private function saveFirstMachine($number_of_lockers, $request)
...
if($machine->save())
$saved = $this->saveLocker($number_of_lockers, $machine->id);
if($saved)
return redirect()->route('create_machine')->with('success','The First Machine with '.$number_of_lockers.' Lockers Added Successfully');
return redirect()->route('create_machine')->with('error','There is something wrong! Please try again.');
已编辑:
把saveMachine
改成return
点赞
public function saveMachine(Request $request)
$number_of_lockers = $request->number_of_lockers;
if($number_of_lockers == '32')
$a_machine = A_Machine::all();
if($a_machine->isEmpty())
return $this->saveFirstMachine(32, $request);
else
return $this->saveNewMachine(32, $request);
else if($number_of_lockers == '64')
$a_machine = B_Machine::all();
if($a_machine->isEmpty())
return $this->saveFirstMachine(64, $request);
else
return $this->saveNewMachine(64, $request);
else if($number_of_lockers == '128')
$a_machine = C_Machine::all();
if($a_machine->isEmpty())
return $this->saveFirstMachine(128, $request);
else
return $this->saveNewMachine(128, $request);
return redirect()->route('create_machine')->with('error','There is something wrong! Please try again.');
【讨论】:
@WahidSherief 还添加了代码以在视图中显示消息...请检查。 @WahidSherief 看来您还没有处理保存方法的 else 情况。所以也加一下。检查我更新的解决方案。 首先它将模型保存在数据库中。所以如果是真的。但我也跟着你说的。但没有运气。我认为这不是逻辑错误。我几乎一直尝试重定向但没有工作。 @WahidSherief 只是为了验证......如果你在函数的最开始添加重定向......这行得通吗? 请看我添加了完整版的代码。是的,如果在它重定向的 saveMachine() 方法的第一行使用重定向。【参考方案2】:试试这个
return redirect()->back()->with('success','your message');
【讨论】:
【参考方案3】: @if(Session::has('message'))
@if( 'danger' == Session::get('message_type') )
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<b>ERROR!</b> Session::get('message')
</div>
@elseif( 'success' == Session::get('message_type') )
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<b>SUCCESSFUL!</b> Session::get('message')
</div>
@elseif( 'warning' == Session::get('message_type') )
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<b>ALERT!</b> Session::get('message')
</div>
@endif
@endif
控制器
public function __construct(Machine $machine)
$this->machine = $machine;
public function store (CreateSaveMachineRequest $request) //CreateSaveMachineRequest Request for validation
$machine = $this->machine->create($request->except('_token'));
try
if($machine)
return \Redirect::back()->with('message', 'machine Created successfully. ')
->with('message_type', 'success');
catch (\Exception $exception)
return \Redirect::back()->with('message','machine Not Created')
->with('message_type','danger')
->withInput();
创建像 alert.blade.php 这样的刀片文件 @include(alert) 到你所有的刀片文件
【讨论】:
以上是关于重定向进入空白页面而不重定向到路由 Laravel 5.8的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 ajax 提交带有 3 个提交按钮的表单而不重定向到 Laravel 中的表单操作 url?
使用 .htaccess 重定向 404 而不重定向尾部斜杠