在Laravel 4中捕获失败的路线

时间:2013-11-26 17:13:08

标签: laravel laravel-4 laravel-routing

我有

Route::get('/layouttest/{category}/{brand}', array('as' => 'main', 'uses' => 'MainController@showUrls')) ->where(array('category' => '[0-9]+', 'brand' => '[0-9]+'));

哪个工作正常。但我想要一条路线来捕获/layouttest/的其他获取请求。例如{category}{brand}->where条件失败时,或者当它们都没有提供时。我甚至不能让这个工作,(我已经把它放在routes.php中的上述路线之后):

Route::get('/layouttest/', 
    array('as' => 'index', 'uses' => 'MainController@index'));

当我请求/layouttest/时,我会收到NotFoundHttpException

1 个答案:

答案 0 :(得分:1)

您就是这样做的:

Route::get('/layouttest/{category?}/{brand?}', array('as' => 'main', 'uses' => 'MainController@showUrls'))->where(array('category' => '[0-9]+', 'brand' => '[0-9]+'));

您需要创建两个路由以回退到另一个控制器:

Route::get('/layouttest/{category}/{brand}', array('as' => 'main', 'uses' => 'MainController@showUrls'))->where(array('category' => '[0-9]+', 'brand' => '[0-9]+'));

Route::get('/layouttest/{category?}/{brand?}', array('as' => 'index', 'uses' => 'AnotherController@showUrls'));

只要你把这个放在第二位,你就会很好。