【问题标题】:Laravel 5.1, optional parameter causing blank pageLaravel 5.1,可选参数导致空白页
【发布时间】: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());
        }
    }

我认为有两种形式,一种有可选参数,一种没有。

当我单击具有可选参数的表单上的按钮时,我得到一个空白屏幕。

这里有一些奇怪的事情:

  1. 没有错误信息。检查 laravel.log
  2. 即使我从控制器方法中删除所有逻辑并执行 简单的 var_dump,我仍然得到一个空白屏幕
  3. 当我单击没有可选参数的按钮时,它 行为符合预期

过去两天我一直在尝试解决这个问题,但没有任何运气。我什至尝试将 {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


【解决方案1】:

根据你的路线

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

【讨论】:

  • 您只添加了一个请求对象。没有帮助:( ...同样的结果。我得到一个空白页,没有错误。
  • 您的请求是否进入控制器?
  • 它是......但后来我在 SO -->stackoverflow.com/questions/40451599/… 上看到了这篇文章......这是我的问题。我的路由文件中有一条具有相同路径的 GET 路由。
【解决方案2】:

请查看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'
)); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多