Slim 框架学习,第十六天 _Router(七)

来源:互联网 发布:帝国时代2蛮王崛起mac 编辑:程序博客网 时间:2024/05/21 08:49

导读:今天我们继续学习Route 类剩余的三个方法

先接着昨天的内容,relativePathFor方法

最后的三个方法 relativePathFor ,pathFor 和 urlFor 是调用关系。所以一起看下

urlFor 调用了 pathFor方法
/**     * Build the path for a named route.     *     * This method is deprecated. Use pathFor() from now on.     *     * @param string $name        Route name     * @param array  $data        Named argument replacement data     * @param array  $queryParams Optional query string parameters     *     * @return string     *     * @throws RuntimeException         If named route does not exist     * @throws InvalidArgumentException If required data not provided     */    public function urlFor($name, array $data = [], array $queryParams = [])    {        trigger_error('urlFor() is deprecated. Use pathFor() instead.', E_USER_DEPRECATED);        return $this->pathFor($name, $data, $queryParams);    }
pathFor 调用了 relativePathFor
 /**     * Build the path for a named route including the base path     *     * @param string $name        Route name     * @param array  $data        Named argument replacement data     * @param array  $queryParams Optional query string parameters     *     * @return string     *     * @throws RuntimeException         If named route does not exist     * @throws InvalidArgumentException If required data not provided     */    public function pathFor($name, array $data = [], array $queryParams = [])    {        $url = $this->relativePathFor($name, $data, $queryParams);        if ($this->basePath) {            $url = $this->basePath . $url;        }        return $url;    }
最后看下 relativePathFor
/**     * 为一个路由构建路径,除了基本的路径之外     * Build the path for a named route excluding the base path     *     * @param string $name        Route name     * @param array  $data        Named argument replacement data     * @param array  $queryParams Optional query string parameters     *     * @return string     *     * @throws RuntimeException         If named route does not exist     * @throws InvalidArgumentException If required data not provided     */    public function relativePathFor($name, array $data = [], array $queryParams = [])    {        //根据路由名字,得到路由对象        $route = $this->getNamedRoute($name);        //得到路由模式 如 hello/{}        $pattern = $route->getPattern();        //        $routeDatas = $this->routeParser->parse($pattern);        // $routeDatas is an array of all possible routes that can be made. There is        // one routedata for each optional parameter plus one for no optional parameters.        //        // The most specific is last, so we look for that first.        //array_reverse 以相反的元素顺序返回数组        $routeDatas = array_reverse($routeDatas);        $segments = [];        foreach ($routeDatas as $routeData) {            foreach ($routeData as $item) {                if (is_string($item)) {                    // this segment is a static string                    $segments[] = $item;                    continue;                }                // This segment has a parameter: first element is the name                if (!array_key_exists($item[0], $data)) {                    // we don't have a data element for this segment: cancel                    // testing this routeData item, so that we can try a less                    // specific routeData item.                    $segments = [];                    $segmentName = $item[0];                    break;                }                $segments[] = $data[$item[0]];            }            if (!empty($segments)) {                // we found all the parameters for this route data, no need to check                // less specific ones                break;            }        }        if (empty($segments)) {            throw new InvalidArgumentException('Missing data for URL segment: ' . $segmentName);        }        $url = implode('', $segments);        if ($queryParams) {            $url .= '?' . http_build_query($queryParams);        }        return $url;    }

最终返回了url地址

但是发现一个小问题,没有发现调用 该方法的地方。只在测试代码中看到了调用。至此Slim/Router.php router 就学完了。下面我们学习 Slim/Route.php 类 。注意前面是 Router 后面是 Route 。

原创粉丝点击