【问题标题】:Laravel 5.5 optional parameter in routing is not workingLaravel 5.5 路由中的可选参数不起作用
【发布时间】:2017-12-02 20:00:12
【问题描述】:

路线:

$router->get('vehicle/{year}/{make}/{model}/{rating?}','VehicleController@vehicle');

控制器动作

public function vehicle($year, $make, $model, $rating = false)
{
    // Implementation
}

网址 http://localhost:8080/vehicle/2010/Wapal/S2

错误

(1/1) NotFoundHttpException

事情很明显,肯定是laravel核心的bug吧?为什么会出现这个错误?

【问题讨论】:

  • 尝试将您的路线更改为 Route::get('vehicle/{year}/{make}/{model}/{rating?}','VehicleController@vehicle');

标签: routing lumen


【解决方案1】:

终于设法使它与多个可选参数一起工作,希望它会有所帮助。 适用于 Lumen 5.6。

例子:

$app->get(
    'vehicle[/{optional_year}[/{optional_make}[/{optional_model[/{optional_rating}]]]]',
    ['middleware' => 'auth' , 'uses' => 'VehicleController@vehicle']
);

如果你不使用任何中间件,类似的东西应该可以工作:

$app->get(
    'vehicle[/{optional_year}[/{optional_make}[/{optional_model[/{optional_rating}]]]]',
    'VehicleController@vehicle'
);

【讨论】:

    【解决方案2】:

    尝试一下,应该可以的

    $router->get('vehicle/{year}/{make}/{model}[/{rating}]','VehicleController@vehicle');
    

    【讨论】:

      猜你喜欢
      • 2016-10-06
      • 2018-03-17
      • 2019-12-10
      • 2018-05-26
      • 1970-01-01
      • 2016-01-26
      • 2015-02-05
      • 1970-01-01
      相关资源
      最近更新 更多