Laravel-对所有路由都使用(:any?)通配符吗? [英] Laravel - Using (:any?) wildcard for ALL routes?

查看:110
本文介绍了Laravel-对所有路由都使用(:any?)通配符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在路由方面遇到了一些麻烦.

I am having a bit of trouble with the routing.

我正在使用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)?

解决此问题的最佳方法是什么?

What's the best way to go about this?

推荐答案

自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);
});

还有另一种方法,即使这些路由返回了404,也可以让您检查是否有cms页(即使这些路由可能与其他模式匹配).该方法修改了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.

这篇关于Laravel-对所有路由都使用(:any?)通配符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆