根据斜杠分隔字符串执行页面 url 路由

发布于 2024-10-28 02:18:43 字数 914 浏览 1 评论 0原文

我正在使用 PHP 爆炸功能。该函数用于检测页面URL。

在我们的网站设置中,页面 URL 为:

  1. /app/home/ (主页)
  2. /app/answers/list (搜索列表页面)
  3. /app/answers/detail /a_id/309 (答案详细信息页面)
  4. /app/answers/list/session/L3NpZC9WVmpBQWlxaw%3D%3D

目前,我正在使用爆炸函数来拆分字符串并将它们存储在数组中。

$currentLocation  =explode("/",$_SERVER["REQUEST_URI"]);

Array ( [0] => [1] => app [2] => answers [3] => list [4] => session [5] => L3NpZC9WVmpBQWlxaw%3D%3D ) 

然后有很多 IF Else 条件语句:

if ($currentLocation [2]=='home')
        {
            $this->data['pageURL']=$currentLocation [2]; //Home directory
        }
        else if($currentLocation [2]=='answers')
        {
            $this->data['pageURL']= $currentLocation [4];
        }

我想知道是否有一个聪明的方法来实现这一点。谢谢。 URL 的 /app/ 部分始终是固定的,其余部分会发生变化。

干杯, 清

I am working with the PHP explode function. This function is used to detect page URL.

In our website settings, the page URLs are:

  1. /app/home/ (Home page)
  2. /app/answers/list (Search list page)
  3. /app/answers/detail/a_id/309 (Answer detail page)
  4. /app/answers/list/session/L3NpZC9WVmpBQWlxaw%3D%3D

Currently, I am using the explode function to split up the strings and store them in an array.

$currentLocation  =explode("/",$_SERVER["REQUEST_URI"]);

Array ( [0] => [1] => app [2] => answers [3] => list [4] => session [5] => L3NpZC9WVmpBQWlxaw%3D%3D ) 

And then there are a lot of IF Else condition statement:

if ($currentLocation [2]=='home')
        {
            $this->data['pageURL']=$currentLocation [2]; //Home directory
        }
        else if($currentLocation [2]=='answers')
        {
            $this->data['pageURL']= $currentLocation [4];
        }

I am wondering whether there is a smart way to achieve this. Thank you. the /app/ part of URL is always fixed, the rest will change.

Cheers,
Qing

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧 2024-11-04 02:18:43

一般来说,当您有很多这样的 if/else 条件时,更常见的是使用 switch 语句:

switch $currentLocation[2] {
    case 'home' : $this->data['pageURL']=$currentLocation[2]; break;
    case 'answers' : $this->data['pageURL']= $currentLocation[4]; break;
    default: $this->data['pageURL']= $currentLocation[4]; break;
}

它比一堆 elseif 块更简洁,尽管代码并没有少多少。

但是在这种情况下,我想说您可能可以使用一组重写规则获得更好的结果。您尚未指定它,但鉴于您所描述的 URL,您可能在服务器配置中使用 mod_rewrite (或类似的)将“友好”URL 重定向到实际的 PHP 页面?

这是我建议工作的地方。通过使用一组 mod_rewrite 规则,您可以让 PHP 页面接收正常的 $_GET 变量。

mod_rewrite 允许您拥有类似 /app/answers/detail/a_id/309 的 URL,但 PHP 程序会接收它,就像用户输入 app.php?page=answers& 一样。部分=详细信息&a_id=309。重写规则基于正则表达式,您可以使它们变得非常复杂,以允许各种不同的 URL 格式。

如果你实现了这一点,你可能会抛弃你在程序中使用 explode() 所做的所有工作,然后挑选出 URL 的相关位,并将其全部替换为大约五行您的服务器配置(.htaccess)。

希望有帮助。

Generally when you've got a lot of if/else conditions like that, it would be more common to use a switch statement instead:

switch $currentLocation[2] {
    case 'home' : $this->data['pageURL']=$currentLocation[2]; break;
    case 'answers' : $this->data['pageURL']= $currentLocation[4]; break;
    default: $this->data['pageURL']= $currentLocation[4]; break;
}

It's neater than a bunch of elseif blocks, though it's not really much less code.

However in this case, I would say that you could probably achieve better results using a set of rewrite rules. You haven't specified it, but given the URLs you've described, it's possible that you're using mod_rewrite (or similar) in your server config to redirect the 'friendly' URL to the actual PHP page?

This is where I would suggest working. By using a set of mod_rewrite rules, you can have the PHP page receive normal $_GET variables.

mod_rewrite allows you to have URLs like /app/answers/detail/a_id/309, but the PHP program would receive it as if the user had typed in app.php?page=answers§ion=detail&a_id=309. The rewrite rules are based on regular expressions, and you can make them quite complex, to allow for a variety of different URL formats.

If you implement that, you could probably throw away all the work you're doing in the program with explode() and then picking out the relevant bits of URL, and replace it all with about five lines in your server config (.htaccess).

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文