【问题标题】:Create group route with Closure in PHP在 PHP 中使用 Closure 创建组路由
【发布时间】:2016-03-18 11:29:31
【问题描述】:

如何在 PHP 中使用 Closure 创建组路由?我正在使用 PHP 从头开始​​创建自己的 REST API 以进行练习和学习。

在引导文件中我调用 App 类:

$app = new App/App();
$app->import('routes.php');

我有 routes.php 文件:

$app->group('/api/v1', function() use ($app)
{
    $app->group('/users', function() use ($app)
    {
        $app->get('/', 'User', 'index');
        $app->post('/', 'User', 'post');
        $app->get('/{id}', 'User', 'get');
        $app->put('/{id}', 'User', 'put');
        $app->delete('/{id}', 'User', 'delete');
    });
});

它需要像这样创建路由:

  • /api/v1/users/
  • /api/v1/users/
  • /api/v1/users/{id}
  • /api/v1/users/{id}
  • /api/v1/users/{id}

应用类:

class App
{
    public function group($link, Closure $closure)
    {
        $closure();
    }
}

它设置这样的路线:

  • /
  • /
  • /{id}
  • /{id}
  • /{id}

我应该怎么做前缀 urls ?我怎样才能“foreach”这些其他 $app->get()、$app->post() 方法调用?

【问题讨论】:

    标签: php routes closures


    【解决方案1】:

    想通了!将 DI 容器添加到处理 Router、Route 和 RouteGroup 类的 App 类。 PHP SLIM 框架是我的灵感来源 - https://github.com/slimphp/Slim/tree/3.x/Slim

    首先我从 App 类调用 group() 方法,并从 Router 类调用 pushGroup() 方法。然后我用 $group(); 调用 RouteGroup 类。之后我调用 popGroup() 只返回最后一个路由组。

    在Route中添加groups url时,只需在Router类中运行processGroups()方法添加前缀链接。

    应用

    /**
     * Route groups
     * 
     * @param string $link
     * @param Closure $closure
     * @return void
     */
    public function group($link, Closure $closure)
    {
        $group = $this->container->get('Router')->pushGroup($link, $closure);
        $group();
        $this->container->get('Router')->popGroup();
    }
    

    路由器

    /**
     * Process route groups
     * 
     * @return string
     */
    private function processGroups()
    {
        $link = '';
        foreach ($this->route_groups as $group) {
            $link .= $group->getUrl();
        }
        return $link;
    }
    
    
    /**
     * Add a route group to the array
     * @param string $link
     * @param Closure $closure
     * @return RouteGroup
     */
    public function pushGroup($link, Closure $closure)
    {
        $group = new RouteGroup($link, $closure);
        array_push($this->route_groups, $group);
        return $group;
    }
    
    
    /**
     * Removes the last route group from the array
     *
     * @return RouteGroup|bool The RouteGroup if successful, else False
     */
    public function popGroup()
    {
        $group = array_pop($this->route_groups);
        return ($group instanceof RouteGroup ? $group : false);
    }
    

    Route类是基础类,有路由参数——method、url、controller、action和附加参数,这里就不复制了。

    RouteGroup

    /**
     * Create a new RouteGroup
     *
     * @param string $url
     * @param Closure $closure
     */
    public function __construct($url, $closure)
    {
        $this->url = $url;
        $this->closure = $closure;
    }
    
    /**
     * Invoke the group to register any Routable objects within it.
     *
     * @param Slinky $app The App to bind the callable to.
     */
    public function __invoke()
    {
        $closure = $this->closure;
        $closure();
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-09
      • 2022-01-17
      • 2021-04-09
      • 2020-06-09
      • 2020-02-11
      • 1970-01-01
      • 2019-06-01
      • 2020-10-25
      • 2015-10-28
      相关资源
      最近更新 更多