【发布时间】:2018-04-07 03:58:08
【问题描述】:
相关路线:
Route::patch('admin/track/practice/{practice_id}/close-practice-session/{session_id}/{new?}', array(
'as' => 'close-practice-session',
'uses' => 'AdminController@closePracticeSession'
));
new 是一个可选的路由参数。
控制器方法:
public function closePracticeSession($club, $practice_id, $session_id, $new = null)
{
$clubDetails = new ClubDetails();
$club_id = $clubDetails->getClubID($club);
date_default_timezone_set(config('app.timezone'));
$CurrentTime = date("Y-m-d H:i:s");
try
{
DB::table('practice_sessions')
->where('id', $session_id)
->where('club_id', $club_id)
->update(['is_current' => 0, 'updated_at' => $CurrentTime]);
if ($new == 'Y')
{
return redirect()->action('AdminController@getTrackPracticeSession', [$club, $practice_id]);
}
else
{
return redirect()->action('AdminController@getTrackPracticeSession', [$club, $practice_id, $session_id])
->with(array('success'=>'Practice was successfully closed.'));
}
}
catch(\Exception $e)
{
return view('errors.500')->with(self::getRequiredData($club))->with('error', $e->getMessage());
}
}
我认为有两种形式,一种有可选参数,一种没有。
当我单击具有可选参数的表单上的按钮时,我得到一个空白屏幕。
这里有一些奇怪的事情:
- 没有错误信息。检查 laravel.log
- 即使我从控制器方法中删除所有逻辑并执行 简单的 var_dump,我仍然得到一个空白屏幕
- 当我单击没有可选参数的按钮时,它 行为符合预期
过去两天我一直在尝试解决这个问题,但没有任何运气。我什至尝试将 {new} 参数设为强制性。每当我传递最后一个参数时,我都会得到一个空白屏幕。
有什么想法吗?我确信我在做一些愚蠢的事情。就是看不出来。
更新(视图上的两种形式)- csrf 标记在标题中。
{!! Form::open([
'method' => 'PATCH',
'route' => ['close-practice-session', $alias, $practiceDetails[0]->practice_id, $practiceDetails[0]->id]
]) !!}
{!! Form::submit('Close Session', ['class' => 'btn btn-primary btn-sm', 'style' => 'width: 160px;margin-left: 0px!important']) !!}
{!! Form::close() !!}
<!-- #2 -->
{!! Form::open([
'method' => 'PATCH',
'route' => ['close-practice-session', $alias, $practiceDetails[0]->practice_id, $practiceDetails[0]->id, "Y"]
]) !!}
{!! Form::submit('Close + Create New', ['class' => 'btn btn-secondary btn-sm', 'style' => 'width: 160px;margin-left: 0px!important']) !!}
{!! Form::close() !!}
【问题讨论】:
-
什么是$club?因为您的路线有第一个动态变量是 {practice_id}
-
是子域的变量。我需要将它传递给每条路线。
-
$club 没有任何价值。 $club 未初始化
-
不,就像我说的,是子域名。总是被传递给路由。问题已解决。请在下面查看我的答案。
标签: laravel optional-parameters