【问题标题】:Why the result looks strange when I send different parameters to the same function? (laravel 5.3)为什么当我向同一个函数发送不同的参数时结果看起来很奇怪? (laravel 5.3)
【发布时间】:2017-05-20 12:25:45
【问题描述】:

我的看法是这样的:

...
<a class="btn btn-primary pull-right" style="margin-top: -10px;margin-bottom: 5px" href="{!! route('users.create.year', [$year]) !!}">
    Add New
</a>
...

...
@foreach($testArray as $key)
...
<a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$key['display'], $year]) !!}">
    <i class="glyphicon glyphicon-plus"></i>
</a>
...
@endforeach
...

我的路线是这样的:

Route::get('users/create/{year}/{display?}', 'UserController@create')
     ->name('users.create.year');

我的控制器是这样的:

public function create($year, $display = null)
{
    echo $display.' : display <br>';
    echo $param_thang. ' : year';die();
    ...
}

当我像这样调用 url 时:http://localhost/mysystem/public/users/create/2016,它可以工作。

结果是这样的:

: display
2016 : year

当我这样调用 url 时:http://localhost/mysystem/public/users/create/14144499452111901/2016 结果是这样的:

2016 : display
14144499452111901 : year

看起来很奇怪,结果反了

为什么会这样?

【问题讨论】:

    标签: php laravel laravel-5.3


    【解决方案1】:

    因为你的路线是:

    Route::get('users/create/{year}/{display?}', 'UserController@create')
     ->name('users.create.year');
    

    所以第一个参数总是年份,第二个参数总是显示变量。

    【讨论】:

      【解决方案2】:

      您需要按照@farkie 的建议更改您的&lt;a&gt;

      <a class="btn btn-primary btn-xs" href="{!! route('users.create.year', [$year,$key['display']]) !!}">
          <i class="glyphicon glyphicon-plus"></i>
      </a>
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-31
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多