简体   繁体   English

ToroPHP路由 - 使用子节点重写URL

[英]ToroPHP routing - URL rewrite with childs

I'm using PHP and ToroPHP for routing. 我正在使用PHP和ToroPHP进行路由。

Unknown number of child pages 未知数量的子页面

It works fine, but in my case I can add pages with childs and parents, where a parent can have an unknown number of have childpages. 它工作正常,但在我的情况下,我可以添加有孩子和父母的页面,其中父母可以有未知数量的儿童页面。

In ToroPHP it might look like this: 在ToroPHP中它可能看起来像这样:

// My Path: http://www.test.com/product/house/room/table/leg/color/

Toro::serve(array(
    "/" => "Home",
    "/:string/:string/:string/:string/:string/:string/" => "Page"
));

class Page {
    function get($slug, $slug2, $slug3, $slug4, $slug5, $slug6) {
        echo "First slug: $slug";
    }
}

Problem 问题

  1. I can figure out what the maximum depth can be and then loop and append out a string containing the "/:string" parameters but it don't look that nice. 我可以弄清楚最大深度可以是什么,然后循环并追加包含“/:string”参数的字符串,但它看起来不太好。

  2. The get-function in the Page-class takes an unknown number of in parameters. Page-class中的get-function接受未知数量的in参数。 I can calculate the max depth from outside the function, but I need the function to know how many values to take. 我可以从函数外部计算最大深度,但我需要函数来知道要采用多少个值。

Question

  1. Is there an alternative way the the problem 1? 问题有另一种方法吗? Some regular expression maybe? 一些正则表达可能吗?
  2. How can I make a function take an unkown number of in parameters? 如何使函数采用未知数量的in参数?
  3. Maybe I try to solve this the wrong way and the first two questions are not relevant? 也许我试图以错误的方式解决这个问题,前两个问题是不相关的? Correct me if that is. 纠正我,如果是的话。

In order for your action to receive all parameters, you need to capture them in your regex. 为了让您的操作接收所有参数,您需要在正则表达式中捕获它们。 You capture a value in regular expressions using parentheses. 您可以使用括号在正则表达式中捕获值。 :string is just an alias for ([a-zA-Z]+) . :string只是([a-zA-Z]+)的别名。 You could apply a wildcard after the first segment, like this: 您可以在第一个段之后应用通配符,如下所示:

"/product/(.*?)" => "Page"

However, this means that you need to parse the URL by yourself in your action, which is not very clean either. 但是,这意味着您需要在您的操作中自己解析URL,这也不是很干净。

If you want to make this particular case more clean, an option would be to use str_repeat : 如果你想让这个特殊情况更干净,可以选择使用str_repeat

Toro::serve(array(
    "/" => "Home",
    "/" . str_repeat(":string/", 6) => "Page"
));

ToroPHP is a very simple library, it should not be that hard to fork it and bend it to your will. ToroPHP是一个非常简单的库,它不应该很难分叉并根据你的意愿弯曲它。 Ideally, how would you like to define routes like this? 理想情况下,您想如何定义这样的路线? Maybe a route like /:string*6 ? 也许像/:string*6这样的路线?


You can always pass more or fewer parameters than defined to a PHP function. 您总是可以传递比PHP函数定义的更多或更少的参数。 Use func_get_args to get all passed parameters and func_num_args to get the number of passed parameters. 使用func_get_args获取所有传递的参数,使用func_num_args获取传递的参数数量。

In response to question 2, can you possibly format the GET parameters into an array and then pass in arrays rather than individual values? 在回答问题2时,您是否可以将GET参数格式化为数组,然后传入数组而不是单个值?

Maybe like: 也许喜欢:

$allSlugs = array($slug, $slug2, $slug3, $slug4, $slug5, $slug6);

// Pass $allSlugs into your instance of Page::get($allSlugs);

class Page {
    function get($getValues) {
        echo isset($getValues[0]) ? "First slug: ".$getValues[0] : '';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM