Laravel - 对所有路线使用 (:any?) 通配符?
我的路由有点问题.
我正在开发 CMS,我需要两条主要路线./admin
和 /(:any)
.admin
控制器用于路由 /admin
,而 view
控制器应该用于除 /admin之外的任何东西代码>.从
view
控制器,然后我将解析 url 并显示正确的内容.
I'm working on a CMS, and I need two primary routes. /admin
and /(:any)
. The admin
controller is used for the route /admin
, and the view
controller should be used for anything else than /admin
. From the view
controller, I will then parse the url and show the correct content.
这就是我所拥有的:
Route::get(array('admin', 'admin/dashboard'), array('as' => 'admin', 'uses' =>'admin.dashboard@index'));
Route::any('(:any)', 'view@index');
第一条路线有效,但第二条路线无效.我玩了一下,似乎如果我使用不带问号的 (:any)
,它只有在 /
后面放一些东西才有效.如果我做把问号放在那里,它根本不起作用.
The first route works, but the second one doesn't. I played around with it a little bit, and it seems if I use (:any)
without the question mark, it only works if I put something after /
. If i do put the question mark there, it doesn't work at all.
我希望以下所有路线都转到 view@index:
I want all of the following routes to go to view@index:
/
/something
/something/something
/something/something/something
/something/something/something/something
...etc...
如果不硬编码一堆 (:any?)/(:any?)/(:any?)/(:any?)
(我什至不知道有效),这是否可能)?
Is this possible without hardcoding a bunch of (:any?)/(:any?)/(:any?)/(:any?)
(which I don't even know works)?
最好的方法是什么?
推荐答案
自 Laravel 4 发布以来,关于这个话题一直存在一些混淆,这个答案是针对 Laravel 3 的.
There has been some confusion since the release of Laravel 4 regarding this topic, this answer was targeting Laravel 3.
有几种方法可以解决这个问题.
There are a few ways to approach this.
第一种方法是匹配(:any)/(:all?)
:
Route::any('(:any)/(:all?)', function($first, $rest=''){
$page = $rest ? "{$first}/{$rest}" : $first;
dd($page);
});
不是最好的解决方案,因为它被分解为多个参数,并且由于某种原因 (:all) 无法自行工作(错误?)
Not the best solution because it gets broken into multiple parameters, and for some reason (:all) doesn't work by itself (bug?)
第二种解决方案是使用正则表达式,我认为这是比上面更好的方法.
The second solution is to use a regular expression, this is a better way then above in my opinion.
Route::any( '(.*)', function( $page ){
dd($page);
});
还有一种方法,即使路由可能匹配其他模式,也可以让您检查是否有 cms 页面,前提是这些路由返回 404.此方法修改 routes.php 中定义的事件侦听器
:
There is one more method, which would let you check if there are cms pages even when the route may have matched other patterns, provided those routes returned a 404. This method modifies the event listener defined in routes.php
:
Event::listen('404', function() {
$page = URI::current();
// custom logic, else
return Response::error('404');
});
但是,我的首选方法是 #2.我希望这有帮助.不管你做什么,确保你在这些之上定义了所有其他路由,捕获所有路由,之后定义的任何路由都不会触发.
However, my preferred method is #2. I hope this helps. Whatever you do, make sure you define all your other routes above these catch all routes, any routes defined after will never trigger.
相关文章