webman自动映射路由能否让url支持中划线

aphper

想让url中controller、action部分兼容兼容中划线,中划线更加符合SEO标准也更美观
例如 /security/update-password 将访问 securityController 控制器的 updatePassword 方法
每条都注册路由麻烦

阅读了一下代码,在
框架核心App.php 类 getControllerAction方法添加如下代码

$controller_class = str_replace(' ', '', ucwords(str_replace('-', ' ', $controller_class)));
$action = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));

实测可行 也不会对框架现有用户产生影响,github发了PR,但是是第一次发 不知道是不是操作有问题群主看不到,望群主能通过下这段改动

中划线更加符合SEO标准也更美观

877 1 0
1个回答

walkor

重新发pr吧,你的pr里带了其它文件代码,src/Auth/AuthManger.php 什么的不要提交。

$controller_class = str_replace(' ', '', ucwords(str_replace('-', ' ', $controller_class)));
$action = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));

这个写得太繁琐了,改成

$controller_class = str_replace('-', ' ', $controller_class);
$action = str_replace('-', ' ', $action);

看下是否ok

  • aphper 2022-05-09

    我从Yii2抄过来的代码 好像是最简单的实现了 你写的这段会报错 method名有空格

  • aphper 2022-05-09

    重新PR了

  • walkor 2022-05-09

    发错了,替换成

    $controller_class = str_replace('-', '', $controller_class);
    $action = str_replace('-', '', $action);
  • aphper 2022-05-10

    这样 不管是i-ndex还in-dex或者ind-ex都能访问到index去了

  • walkor 2022-05-10

    除了不自动转大写字母,其它和你的没区别吧

  • aphper 2022-05-10

    ....区别很大啊 不管你访问 index/i-index、index/in-dex、index/ind-ex都会跑到index控制器的index方法去

  • walkor 2022-05-11

    你pr也是一样的效果,index/i-index、index/in-dex、index/ind-ex都会跑到index控制器的index方法去。
    这样好像确实不好

  • aphper 2022-05-11

    不会啊 我测试过 都是从Yii直接copy过来的 很可靠

  • nitron 2022-05-11
    php方法命名不能重复也不区分大小写,所以walkor那个跟你的是一样的效果
    所以不管你是function Log/LOg/LOG/lOg/loG ... 最后->log都能调用
    
    打个比方,你要Request-For-Something, 你要精确匹配,那么方法名只能是request_for_something, - 转 _
年代过于久远,无法发表回答
🔝