具有强制参数的Laravel路径控制器

azn*_*t81 1 php pattern-matching laravel laravel-4

所以我已经检查了 PHP - 在LaravelLaravel 4强制参数错误中使用参数进行路由

然而,使用所说的 - 我似乎无法使简单的路由成为可能,除非我不了解filter/get/parameters如何工作.

所以我想要做的是路由URL为/ display/2,其中display是一个动作,2是id,但我想将它限制为仅限数字.

我想

Route::get('displayproduct/(:num)','SiteController@display');
Route::get('/', 'SiteController@index');

class SiteController extends BaseController {

public function index()
{

    return "i'm with index";
}

public function display($id)
{
    return $id;
}
}
Run Code Online (Sandbox Code Playgroud)

问题是,如果我使用它会抛出404

Route::get('displayproduct/{id}','SiteController@display');
Run Code Online (Sandbox Code Playgroud)

它将传递参数,但URL可以显示/ ABC,它将传递参数.我想将其限制为仅限数字.

我也不希望它变得安静,因为索引I理想地希望将该控制器与不同的动作混合.

小智 8

假设您使用的是Laravel 4,则无法使用(:num),您需要使用正则表达式进行过滤.

Route::get('displayproduct/{id}','SiteController@display')->where('id', '[0-9]+');
Run Code Online (Sandbox Code Playgroud)