如何为多个变量提供 Code Igniter URI 段?

发布于 2024-08-21 16:22:01 字数 509 浏览 3 评论 0原文

我正在编写一个应用程序,允许您根据位置和类别过滤数据库结果。

如果有人要在 Golf 类别下搜索 Liverpool,则 URI 将是 /index.php/search/Liverpool/Golf

如果有人想按位置而不是类别进行搜索,他们将被发送到 /index.php/search/Liverpool

但是,如果有人只想按类别进行过滤,他们将无法使用 /index.php/search/Golf 因为这会被位置搜索捕获。

是否有最佳实践方法可以让 /index.php/search/Golf 被识别?关于还可以添加到 URI 中以使这两个查询不同的一些最佳实践?也许/index.php/search/category/Golf

尽管这开始显示出 /index.php?search&category=Golf 的特征,但这正是我想要避免的。

I'm writing an app that allows you to filter database results based on Location and Category.

If someone was to search for Liverpool under the Golf category the URI would be /index.php/search/Liverpool/Golf.

Should someone want to search by Location but not category, they would be sent to /index.php/search/Liverpool

However, should someone want to filter only by category they would be unable to use /index.php/search/Golf because that would be caught by the location search.

Is there a best practice way to have /index.php/search/Golf be recognised? Some best practice as to what else to add to the URI to make these two queries distinct? /index.php/search/category/Golf perhaps?

Though that is beginning to show characteristics of /index.php?search&category=Golf which is exactly what I'm trying to avoid.

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

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

发布评论

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

评论(4

疏忽 2024-08-28 16:22:01

尝试使用 $this->uri->uri_to_assoc(n)
此处描述 http://codeigniter.com/user_guide/libraries/uri.html (页面的一半)

基本上你会像这样构建你的网址:

mysite.com/index.php/search/location/liverpool/category/golf

注意:参数是可选的,因此您不必始终将两者都放在其中。你也可以这样做

mysite.com/index.php/search/location/liverpool/

mysite.com/index.php/search/category/golf

这样,如果您要查找的元素不存在,它将返回 FALSE

Try using $this->uri->uri_to_assoc(n)
described here http://codeigniter.com/user_guide/libraries/uri.html (half way down on page)

basically you will structure your url like this:

mysite.com/index.php/search/location/liverpool/category/golf

NOTE: the parameters are optional so you dont have to have both in there all the time. you can just as well do

mysite.com/index.php/search/location/liverpool/
and

mysite.com/index.php/search/category/golf

this way it will return FALSE if the element you are looking for does not exist

灼疼热情 2024-08-28 16:22:01

无论他们在搜索什么,最好保持 URI 段的相关性。

index.php/LOCATION/CATEGORY

如果他们对某个位置不感兴趣,则将填充符传递给系统:

index.php/anywhere/golf

然后在您的代码中,您只需检查 ANYWHERE 的特定字符串即可确定他们是否只想查看该活动。我假设您将使用链接或论坛重定向它们(并且它们本身不会键入 URI 字符串),因此您应该安全地仅传递您期望的信息并针对该信息进行测试。

It would probably be best to keep your URI segments relavent no matter what they are searching for.

index.php/LOCATION/CATEGORY

If they are not interested in a location then pass a filler to the system:

index.php/anywhere/golf

Then in your code you just check for that specific string of ANYWHERE to determine if they only want to see the activity. I assume that you are going to be redirecting them with either links or forums (and that they aren't typing the URI string themselves) so you should be safe in just passing information that you expect and testing against that.

空心↖ 2024-08-28 16:22:01

我使用上面 Tom 建议的格式,然后按照下面的操作确定参数的值。

$segment_array = $this->uri->segment_array();        
$is_location_searched = array_search('location', $segment_array);
if($is_location_searched && $this->uri->segment($is_location_searched +1))
{
    $location = $this->uri->segment($is_sorted+1);
}

I use the format suggested by Tom above and then do something along the lines of below to determine the value of the parameters.

$segment_array = $this->uri->segment_array();        
$is_location_searched = array_search('location', $segment_array);
if($is_location_searched && $this->uri->segment($is_location_searched +1))
{
    $location = $this->uri->segment($is_sorted+1);
}
不打扰别人 2024-08-28 16:22:01

看看 http://lucenebook.com/#/p:solr/s: wiki 并单击左侧导航栏上的一些位置。执行此操作时,请密切注意 url 中发生的情况。我真的很喜欢这个计划,原因有很多。

  1. 它对 SEO 友好。
  2. “好奇”的人可以混合/匹配网址,但它仍然可以解析为正确的搜索。
  3. 看起来不错!

当然,诀窍实际上在于代码中,在于如何构建该东西。我花了几周的时间来整理它,但我终于有了我自己的该网站版本。只是不是基于ajax,因为我比ajax更喜欢搜索引擎。阿贾克斯不支付账单。

Have a look at http://lucenebook.com/#/p:solr/s:wiki and click around a bit on the left-hand navigation. Pay close attention to what happens in the url when you do. I really like this scheme for many reasons.

  1. It's SEO-friendly.
  2. "Curious" people can mix/match the urls and it still resolves to a proper search.
  3. It just looks good!

Of course, the trick is really in the code, in how you build the thing. It took me a few weeks to sort it out, but I finally have my own version of that site. Just not ajax based, because I like search engines better than ajax. Ajax don't pay the bills.

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