Laravel在除路线之外的任何地址返回页面

时间:2016-09-01 15:07:05

标签: php laravel laravel-5 laravel-routing

我有一个Laravel应用程序的路由列表。现在,我想返回/为所有未列出的路由返回的页面。即。

/hello/world <-> binded to "Hello world" page
/blah/blah <-> binded to "Blah Blah" p

现在,无论用户请求(GET)和请求中有多少斜杠(/asdfda/sadfasdf/asdfasdfeafsadfsdaasdfadsfasd/adsfsdaf),我都想返回同一页面。我不希望它是404,因为我想保持这是合法的路线。

我试过

Route::get('{all?}', [
    'as' => 'spa.index',
    'uses' => 'SPAController@index',
]);

但只有在没有斜杠的情况下才有效。

有人知道怎么做吗?

2 个答案:

答案 0 :(得分:0)

好吧,我自己找到了解决办法。我会为子孙后代保留这个问题。无论用户请求什么路由

,这都将返回相同的页面
Route::get('{all?}', [
    'as' => 'spa.index',
    'uses' => 'SPAController@index',
])->where('all', '([A-z\d-\/_.]+)?');

答案 1 :(得分:-1)

Route::get('/{foo}/{bar}', [
   'as' => 'spa.index',
   'uses' => 'SPAController@index',
]);

并在SPAController和action index中添加:

public function index($foo,$bar){
     return view($foo.' '.$bar);
}