doupike2351 2015-12-12 16:48
浏览 30
已采纳

使用PHP中的Closure创建组路由

How can I create group routing in PHP with Closure? I'm creating my own REST API from scratch in PHP for practice and learning.

In bootstrap file i call App class:

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

I have routes.php file with:

$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');
    });
});

It needs to create routes like this:

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

App class:

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

And it sets routes like this:

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

What should I do to prefix urls ? How can I "foreach" these other $app->get(), $app->post() method callings ?

  • 写回答

1条回答 默认 最新

  • douyin7416 2015-12-12 20:59
    关注

    Figured it out! Added DI container to App class which handles Router, Route and RouteGroup classes. PHP SLIM framework was my inspiration - https://github.com/slimphp/Slim/tree/3.x/Slim

    First I call group() method from App class with calls pushGroup() method from Router class. Then I invoke RouteGroup class with $group();. After that I cal popGroup() to return only last route group.

    When adding groups url to Route, just run processGroups() method in Router class to add prefix links.

    App class

    /**
     * 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();
    }
    

    Router class

    /**
     * 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 class is basic class with routing parameters - method, url, controller, action and additional parameters so I won't copy it here.

    RouteGroup class

    /**
     * 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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 感应式传感器制作的感应式讯响器
  • ¥15 如何使用SC92F8003固件库解析私有协议数据?
  • ¥15 如何在音频中嵌入字符串(水印)信息进行传递
  • ¥30 plc怎么以设计说明书申请软著
  • ¥15 硬盘识别不了,需要初始化,可我的数据怎么办
  • ¥15 lvm2被mask了,怎么unmask都没用(标签-ubuntu|关键词-apt)
  • ¥15 交叉注意力机制的残差问题
  • ¥15 微信小程序:渲染收货地址时页面不显示
  • ¥20 win7 64位DirectShow提示初始化失败如何解决?
  • ¥15 关于Java对接海康威视车牌识别一体机SDK是否需要固定外网的IP?