为什么PHP框架Laravel从5.2版本开始弃用路由功能"控制器"?



我曾经使用框架Laravel 5.x路由如下:

Route::controller("demo", "AppHttpControllersDemoController");

但是当我在 Laravel 5.2 中读取路由代码时,我看到它自 5.2 版以来已被弃用,在文件 \Illuminate\Routing\Router 中

/**
 * Route a controller to a URI with wildcard routing.
 *
 * @param  string  $uri
 * @param  string  $controller
 * @param  array  $names
 * @return void
 *
 * @deprecated since version 5.2.
 */
public function controller($uri, $controller, $names = [])
{
    $prepended = $controller;
    // First, we will check to see if a controller prefix has been registered in
    // the route group. If it has, we will need to prefix it before trying to
    // reflect into the class instance and pull out the method for routing.
    if (! empty($this->groupStack)) {
        $prepended = $this->prependGroupUses($controller);
    }
    $routable = (new ControllerInspector)
                        ->getRoutable($prepended, $uri);
    // When a controller is routed using this method, we use Reflection to parse
    // out all of the routable methods for the controller, then register each
    // route explicitly for the developers, so reverse routing is possible.
    foreach ($routable as $method => $routes) {
        foreach ($routes as $route) {
            $this->registerInspected($route, $controller, $method, $names);
        }
    }
    $this->addFallthroughRoute($controller, $uri);
}

我认为这是一个很好的功能,但是为什么他们弃用它?

还有其他更好的解决方案吗?

=

更新 1 =

正如问题(https://github.com/laravel/framework/pull/10777)所说,他认为该功能使路线变得混乱,或者容易导致访问意外的路线定义。

根据Github问题(https://github.com/laravel/framework/pull/10777)

克雷普 说:

此方法有很多问题,并且由于路由文件中没有直接引用它们,因此使路由定义变得混乱。这有时可能会导致意外的路由定义(例如,如果将getFilesystem等方法添加到父级或trait - 开发人员错误,但可能未被注意到)。

当中间件

应用于特定方法时(例如,有人向控制器添加新方法而忘记添加中间件),这也可能导致安全错误,这在手动定义路由时很少出现。

最后 - 参数问题,由于分组或其他意外的路由组合,这很难解决(请参阅#10774)。

为了减少中断量,应在下一个 5.1.x 版本中将其标记为已弃用。

可能的替代方法:在 master 中将此方法标记为已弃用,并在下一个主要版本中删除。

最新更新