Php 注册控制器路由时防止编号参数

Php 注册控制器路由时防止编号参数,php,laravel-4,Php,Laravel 4,拉威尔有一个非常棒的路由系统,但我特别需要解决一点 我想删除在为管线定义控制器时创建的额外可选管线 在Laravel3中,您显然可以执行以下操作,但从未使用过v3。我不知道这是否是我所寻找的,无论如何,它在v4中不起作用 Router::$segments = 0; 例如,当我这样定义路线时: Route::group(array('prefix' => 'monitoring'), function() { // Handle hosts Route::controll

拉威尔有一个非常棒的路由系统,但我特别需要解决一点

我想删除在为管线定义控制器时创建的额外可选管线

在Laravel3中,您显然可以执行以下操作,但从未使用过v3。我不知道这是否是我所寻找的,无论如何,它在v4中不起作用

Router::$segments = 0;
例如,当我这样定义路线时:

Route::group(array('prefix' => 'monitoring'), function() {
    // Handle hosts
    Route::controller('hosts/', 'Apm\Controllers\Monitoring\Hosts\Index');
    Route::controller('hosts/{host}', 'Apm\Controllers\Monitoring\Hosts\Host');
    Route::controller('hosts/{host}/{service}', 'Apm\Controllers\Monitoring\Hosts\Service');
});
我的路线将在
artisan routes
命令中显示如下:

+--------+------------------------------------------------------------------------------------------------+------+---------------------------------------------------------+----------------+---------------+
| Domain | URI                                                                                            | Name | Action                                                  | Before Filters | After Filters |
+--------+------------------------------------------------------------------------------------------------+------+---------------------------------------------------------+----------------+---------------+
|        | GET|HEAD /                                                                                     |      | Closure                                                 |                |               |
|        | GET|HEAD monitoring/hosts/status/{one?}/{two?}/{three?}/{four?}/{five?}                        |      | Apm\Controllers\Monitoring\Hosts\Index@getStatus        |                |               |
|        | GET|HEAD monitoring/hosts/details/{one?}/{two?}/{three?}/{four?}/{five?}                       |      | Apm\Controllers\Monitoring\Hosts\Index@getDetails       |                |               |
|        | GET|HEAD monitoring/hosts/report/{one?}/{two?}/{three?}/{four?}/{five?}                        |      | Apm\Controllers\Monitoring\Hosts\Index@getReport        |                |               |
|        | GET|HEAD monitoring/hosts/thresholds/{one?}/{two?}/{three?}/{four?}/{five?}                    |      | Apm\Controllers\Monitoring\Hosts\Index@getThresholds    |                |               |
|        | GET|HEAD monitoring/hosts/timeline/{one?}/{two?}/{three?}/{four?}/{five?}                      |      | Apm\Controllers\Monitoring\Hosts\Index@getTimeline      |                |               |
|        | GET|HEAD monitoring/hosts/index/{one?}/{two?}/{three?}/{four?}/{five?}                         |      | Apm\Controllers\Monitoring\Hosts\Index@getIndex         |                |               |
|        | GET|HEAD monitoring/hosts                                                                      |      | Apm\Controllers\Monitoring\Hosts\Index@getIndex         |                |               |
|        | GET|HEAD monitoring/hosts/{host}/status/{one?}/{two?}/{three?}/{four?}/{five?}                 |      | Apm\Controllers\Monitoring\Hosts\Host@getStatus         |                |               |
|        | GET|HEAD monitoring/hosts/{host}/details/{one?}/{two?}/{three?}/{four?}/{five?}                |      | Apm\Controllers\Monitoring\Hosts\Host@getDetails        |                |               |
|        | GET|HEAD monitoring/hosts/{host}/report/{one?}/{two?}/{three?}/{four?}/{five?}                 |      | Apm\Controllers\Monitoring\Hosts\Host@getReport         |                |               |
|        | GET|HEAD monitoring/hosts/{host}/thresholds/{one?}/{two?}/{three?}/{four?}/{five?}             |      | Apm\Controllers\Monitoring\Hosts\Host@getThresholds     |                |               |
|        | GET|HEAD monitoring/hosts/{host}/timeline/{one?}/{two?}/{three?}/{four?}/{five?}               |      | Apm\Controllers\Monitoring\Hosts\Host@getTimeline       |                |               |
|        | GET|HEAD monitoring/hosts/{host}/index/{one?}/{two?}/{three?}/{four?}/{five?}                  |      | Apm\Controllers\Monitoring\Hosts\Host@getIndex          |                |               |
|        | GET|HEAD monitoring/hosts/{host}                                                               |      | Apm\Controllers\Monitoring\Hosts\Host@getIndex          |                |               |
|        | GET|HEAD monitoring/hosts/{host}/{service}/status/{one?}/{two?}/{three?}/{four?}/{five?}       |      | Apm\Controllers\Monitoring\Hosts\Service@getStatus      |                |               |
|        | GET|HEAD monitoring/hosts/{host}/{service}/details/{one?}/{two?}/{three?}/{four?}/{five?}      |      | Apm\Controllers\Monitoring\Hosts\Service@getDetails     |                |               |
|        | GET|HEAD monitoring/hosts/{host}/{service}/report/{one?}/{two?}/{three?}/{four?}/{five?}       |      | Apm\Controllers\Monitoring\Hosts\Service@getReport      |                |               |
|        | GET|HEAD monitoring/hosts/{host}/{service}/thresholds/{one?}/{two?}/{three?}/{four?}/{five?}   |      | Apm\Controllers\Monitoring\Hosts\Service@getThresholds  |                |               |
|        | GET|HEAD monitoring/hosts/{host}/{service}/timeline/{one?}/{two?}/{three?}/{four?}/{five?}     |      | Apm\Controllers\Monitoring\Hosts\Service@getTimeline    |                |               |
|        | GET|HEAD monitoring/hosts/{host}/{service}/self-healing/{one?}/{two?}/{three?}/{four?}/{five?} |      | Apm\Controllers\Monitoring\Hosts\Service@getSelfHealing |                |               |
|        | GET|HEAD monitoring/hosts/{host}/{service}/index/{one?}/{two?}/{three?}/{four?}/{five?}        |      | Apm\Controllers\Monitoring\Hosts\Service@getIndex       |                |               |
|        | GET|HEAD monitoring/hosts/{host}/{service}                                                     |      | Apm\Controllers\Monitoring\Hosts\Service@getIndex       |                |               |
简单地说,我想去掉
{one?}/{two?}/{three?}/{four?}/{five?}
部分,因为我总是可以预测我的参数,而且通常我想指定必须使用的控制器并命名它们

更新 我们决定不隐式地列出所有路由,因为这会使我们的routes.php极度膨胀(如果不是数千条,也会有数百条)。除了路由器解析的额外参数外,像我们现在这样的控制器设置工作得非常好,这在使用
route::get
隐式定义路由时是不存在的


我可能应该提到,我们稍微修改了
路由器
类,以便能够使子资源成为可能。

我不知道如何摆脱这些额外参数,但使用隐式路由被认为不是最佳做法,可能会导致许多问题。查看更多信息。如果你能预测,正如你所说,我认为最好用
Route::get
等显式定义你的路由。

为了回答我自己的问题,我们已经做的是覆盖
Router
类。我现在明白,这也是解决这个问题的关键

Route::controller
基本上允许您将其中的所有方法隐式注册为路由的端点。如前所述,这不是你通常想做的事情,但我们在这样做之前做了一个明智的决定。为了解决无法正确说出哪些参数可以传递给函数的问题,他们添加了这5个“通配符”参数。在这个问题中,我们使用类的方式是不需要这些参数

我想指出的是,这可能可以通过使用
ReflectionMethod::getParameters
使用更多的反射魔法来解决。不过,也有很多人反对使用这样的东西

因此,正如您可能猜到的,简言之,Laravel使用传递给
Route::controller
的类的反射类,并以这种方式从代码中提取路由端点

参数添加在类
illumb\Routing中\ControllerInspector@addUriWildcards
因此,为了防止添加这些参数,我们需要重写此类。由于该类是实例化的,然后在
illighte\Routing\Router
类中使用,因此我们还需要覆盖
路由器

在Laravel中,您需要创建一个覆盖来创建核心类。对于
路由器
来说,这没有什么不同。尽管标准文档(目前)并不特别清楚如何做到这一点

(我们实现了psr-4,并将名称空间
Apm
设置为
app/
以自动加载我的类)

在app/Lib/Routing/RoutingServiceProvider.php中

<?php namespace Apm\Lib\Routing;
use Illuminate\Routing\RoutingServiceProvider as LaravelRoutingServiceProvider;

class RoutingServiceProvider extends LaravelRoutingServiceProvider
{
    protected function registerRouter()
    {
        $this->app['router'] = $this->app->share(function($app)
        {
            $router = new Router($app['events'], $app);

            // If the current application environment is "testing", we will disable the
            // routing filters, since they can be tested independently of the routes
            // and just get in the way of our typical controller testing concerns.
            if ($app['env'] == 'testing')
            {
                $router->disableFilters();
            }

            return $router;
        });
    }
}
它将控制器检查器设置为
Apm\Lib\Routing\ControllerInspector
,而不是
light\Routing\ControllerInspector

最后在
app/Lib/Routing/ControllerInspector.php中,我们有:

<?php namespace Apm\Lib\Routing;
use Illuminate\Routing\ControllerInspector as LaravelControllerInspector;

class ControllerInspector extends LaravelControllerInspector
{
    /**
     * Add wildcards to the given URI.
     *
     * @param  string  $uri
     * @return string
     */
    public function addUriWildcards($uri)
    {
        return $uri;//.'/{one?}/{two?}/{three?}/{four?}/{five?}';
    }
}
当然,以某种方式能够显式地传递用于每个端点/函数方法的参数要好得多,但这会破坏隐式设置端点的意义

如果仍然使用此机制,您可能希望更进一步,根据该方法接受的参数获取/使用路由参数。而不添加已在被检查控制器的基本路由模式中设置的参数


Enfin,这为我们解决了问题,因为我们不需要在任何地方的路线末端添加参数。

控制器的方法是什么样子的?在我们决定采取这种方法之前,已经对这个问题进行了内部评估。我已经阅读了您链接的文章,除了一些技术错误(子资源是可能的,端点完全与artisan routes一起布置)外,它提出了有效的观点,但对于该项目的规模,我们决定不隐式命名所有路由,因为这会干扰该文章中的最后一点,也就是说,routes.php包含成百上千行代码。此外,这不是一个答案,应该是一个评论。
<?php namespace Apm\Lib\Routing;
use Illuminate\Routing\ControllerInspector as LaravelControllerInspector;

class ControllerInspector extends LaravelControllerInspector
{
    /**
     * Add wildcards to the given URI.
     *
     * @param  string  $uri
     * @return string
     */
    public function addUriWildcards($uri)
    {
        return $uri;//.'/{one?}/{two?}/{three?}/{four?}/{five?}';
    }
}
'providers' => array(
        'Apm\Lib\Routing\RoutingServiceProvider',
),