PHPZlc & Symfony PHPZlc & Symfony
  • 首页 
  • 文档 
  • 博客 
  • 联系我们 
  • 登录 注册
PHPZlc & Symfony PHPZlc & Symfony
登录 注册
PHPZlc & Symfony
  • 首页 
  • 文档 
  • 博客 
  • 联系我们 

类别:

  • Symfony新闻 (11)
  • Symfony博客 (8)
  • PHPZlc动态 (14)
  • PHPZlc博客 (2)
  • 技术分享 (7)

标签

  • symfony
  • route
  • 路由
  • routing

最新博客:

Symfony 6.3的新功能:调度程序组件,取代cron轻松实现定时任务【转载】
3天前
Symfony 6.3中的新功能:密码强度约束【转载】
4天前
Symfony 6.3 中的新功能:查询参数映射器【转载】
7天前
Symfony 6.3 中的新功能:序列化程序调试命令【转载】
10天前
Symfony 6.3 中的新功能:表情符号改进【转载】
11天前
loading
  1. 首页
  2. 博客
  3. Symfony博客
  4. 运用Symfony路由组件定制化开发
更新于 2022-10-26 17:25:03

类别:

  • Symfony新闻 (11)
  • Symfony博客 (8)
  • PHPZlc动态 (14)
  • PHPZlc博客 (2)
  • 技术分享 (7)

标签

  • symfony
  • route
  • 路由
  • routing

最新博客:

Symfony 6.3的新功能:调度程序组件,取代cron轻松实现定时任务【转载】
3天前
Symfony 6.3中的新功能:密码强度约束【转载】
4天前
Symfony 6.3 中的新功能:查询参数映射器【转载】
7天前
Symfony 6.3 中的新功能:序列化程序调试命令【转载】
10天前
Symfony 6.3 中的新功能:表情符号改进【转载】
11天前

运用Symfony路由组件定制化开发

CJayhe Symfony博客 114 views

如何获取路由类用于编程和写库

$routeCollection = $this->get('router')->getRouteCollection();
$routeCollection->all(); 

常用方法

/**
 * 生成url
 * 
 * @param string $name 路由名
 * @param array $parameters  路由参数
 * @param int $referenceType 返回类型 UrlGeneratorInterface::ABSOLUTE_PATH 相对路由 | UrlGeneratorInterface::ABSOLUTE_URL 绝对网址
 * @return string
 */
function generateUrl(string $route, array $parameters = [], int $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)

如何定义自己的路由类

定义

use use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$routeCollection = new RouteCollection();
$name = 'index';
$defaults = [];
$path = '/';
$route = new Route($path, $defaults, []);
$routeCollection->add($name, $route);

匹配

use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\RequestContext;
public function urlMatcher($path_info)
{
    $matcher = new UrlMatcher($routeCollection, new RequestContext());
    try {
        return $matcher->match($path_info);
    }catch (\Exception $exception){
        return  array();
    }
}

生成url

use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Generator\UrlGenerator;
public function generateUrl(string $name, array $parameters = [], int $referenceType = UrlGenerator::ABSOLUTE_PATH)
{
    $urlGenerator = new UrlGenerator($routeCollection, new RequestContext());
    $path = $urlGenerator->generate($name, $parameters, $referenceType);
    return CurSite::$baseUrl . $path;
}

帮助PHPZlc项目!

与任何开源项目一样, 贡献代码 或 文档 是最常见的帮助方式, 但我们也有广泛的 赞助机会。

评论
CONTENTS
Leaf Illustration

皖ICP备17026196号-3 34020302000197号

© All Rights Reserved by PHPZlc