Laravel 5.1,可选参数导致空白页面

时间:2018-04-07 03:58:08

标签: laravel optional-parameters

相关路线:

Route::patch('admin/track/practice/{practice_id}/close-practice-session/{session_id}/{new?}', array(
                'as' => 'close-practice-session',
                'uses' => 'AdminController@closePracticeSession'
));

new 是可选的路线参数。

Controller方法:

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());
        }
    }

我的视图中有两个表单,一个有可选参数,一个没有。

当我点击包含可选参数的表单上的按钮时,我将看到一个BLANK屏幕。

以下是一些奇怪的事情:

  1. 没有错误消息。检查了laravel.log
  2. 即使我从控制器方法中删除了所有逻辑并执行了操作 简单的var_dump,我仍然得到一个空白的屏幕
  3. 当我点击没有可选参数的按钮时,它 表现如预期
  4. 过去两天我一直试图解决这个问题而没有任何运气。我甚至试图强制使用{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() !!}
    

2 个答案:

答案 0 :(得分:0)

根据您的路线

Route::patch('admin/track/practice/{practice_id}/close-practice-session/{session_id}/{new?}', array(
                'as' => 'close-practice-session',
                'uses' => 'AdminController@closePracticeSession'
));

你的控制器功能应该是这样的

public function closePracticeSession(Request $request, $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());
        }
    }

答案 1 :(得分:0)

请查看this SO post。这给了我一个解决我问题的提示。我的routes.php文件中有一个相同的GET路由。一旦我将PATCH路由修改为以下内容,一切都按预期工作。

Route::patch('admin/close-practice-session/{practice_id}/{session_id}/{new?}', array(
    'as' => 'close-practice-session',
    'uses' => 'AdminController@closePracticeSession'
));