指定控制器&在路由参数的匿名函数中使用的方法?

时间:2015-07-18 05:55:46

标签: laravel laravel-5 laravel-routing

我正在检查Laravel。尝试使用通配符路由。第二个参数是匿名函数。理想情况下,我想做一些验证,以确定它是否是一个有效的通配符选项&然后指定控制器&使用方法。

Route::get('lodging/{entrance}', function($entrance){
    // validate if entrance is 'north','south','east','west'
    // send to controller & specific method
    return "entrance is $entrance";
});

这是一个合适的地方吗?

或者是否应该在控制器中进行此验证并将此格式用于路线:

Route::get('lodging/{entrance}', 'Lodging@chooseEntrance');

2 个答案:

答案 0 :(得分:0)

您可以为路线添加简单验证

Route::get('lodging/{entrance}', function(){ ... })->where('entrance', 'north|south|east|west');

请参阅Routing#parameters-regular-expression-constraints

答案 1 :(得分:-2)

Route::get('lodging/{entrance}', function($entrance){ 
    $app = app();
      $controller = $app->make('ExampleController');
    if($entrance=="north"){
      return $controller->callAction('index', $parameters = array());
    }else{
      return $controller->callAction2('index', $parameters = array());
    }

})->name('lodging');

希望它有助于帮助某人。