如何在OctoberCMS中处理复杂的URL?

问题描述 投票:0回答:2

我是octoberCMS的新手!考虑到我一直在开发其他类型的CMS,它们通常会自动处理url。因此,让我们考虑一下,我们有一个包含国家及其相对州和城市的网站。当然,所有这些都使用模型中的关系。

如果我使用网址创建此页面

/:country-slug

我可以通过查询国家信息来轻松访问内容

/美国

如果我想进入费城(Philadelphia),我会遇到类似这样的事情

/:country-slug /:state-slug /:city-slug

这样就行了

/美国/宾夕法尼亚州/费城

但要使其真正起作用是唯一的,您需要检查“城市”模型是否具有与其连接的确切国家和州。否则,如果仅检查最后一个参数:city-slug,则会创建虚假或更糟糕的无限重复内容,其中每个州/国家/地区的组合都可以与费城一起使用,因此该URL也可以使用

/意大利/加利福尼亚州/费城

这是正确的方法吗?这样每个带有多个参数的URL都需要在后端手动检查吗?还是我在这里想念什么?对于非常长的URL,此方法似乎非常复杂,而对于实际的动态内容而言却不太实用。

octobercms
2个回答
0
投票

创建具有以下定义的页面:

url = "/:country-slug/:state-slug?/:city-slug?"

[countryComponent]
country = ":country-slug"
state = ":state-slug"
city = ":city-slug"

component内检查external property values是否为空,并相应地过滤/搜索模型。如果找不到模型,则返回404。那不切实际吗?


0
投票

它看起来很复杂,但确实很简单,您只需要一次检查,只需要在需要时重定向到正确的位置。

只需使用此子弹 /:country-slug?/:state-slug?/:city-slug?

您可以看到我们将这3个都设为可选。

通过这种方式,您实际上可以记录请求,并知道用户正在搜索哪些项目以及哪些鼻涕虫穿着。

  1. 如果没有正确的国家/地区重定向到错误页面,或者没有帮助说明您需要提供正确的国家/地区名称
  2. 如果国家允许,请显示其内容。
  3. 如果国家/地区正常,则不能将其重定向到国家/地区页面,以避免出现很多重复的内容和错误消息。
  4. 如果国家和州可以,请显示城市内容。
  5. 同样适用于城市...

现在您可以在组件或模板中执行以下操作。

use Country;
use State;
use City;

function onStart()
{
    $whatToShow = 'country';
    $country = Country::where('slug', $this->param('country-slug'))->first();
    if(!$country) {
        // if we cant find correct country then no go
        return Redirect::to('/not-found-page');
    } 
    $this['country'] = $country;
    $this['state'] = null;
    $this['city'] = null;

    $stateSlug = $this->param('state-slug');
    // check if state slug is there
    if($stateSlug) {
        $state = Country::where('slug', $stateSlug)->first();     
        if(!$state) {
            // if we do not have state then just redirect to country URL
            $countryPageLink = $this->controller->pageUrl(
                null, 
                ['country-slug' => $country->slug]
            );
            return Redirect::to($countryPageLink);
        }
        $this['state'] = $state;
        $whatToShow = 'state';
    }

    $citySlug = $this->param('city-slug');
    // check if city slug is there
    if($citySlug) {
        $city = City::where('slug', $citySlug)->first();     
        if(!$city) {
            // if we do not have city then just redirect to country/state URL
            $statePageLink = $this->controller->pageUrl(
                null, 
                ['country-slug' => $country->slug, 'state-slug' => $state->slug]
            );
            return Redirect::to($statePageLink);
        }
        $whatToShow = 'city';
        $this['city'] = $city;
    }

    $this['whatToShow'] = $whatToShow;
}

在标记中

{% if whatToShow == 'country' %}
    show country specific data - or better country partial
    {% partial 'country' item=country %}
{% endif %}

{% if whatToShow == 'state' %}
    {% partial 'country' item=country %}
    show country specific data - or better state partial
    {% partial 'state' item=state %}
{% endif %}

{% if whatToShow == 'city' %}
    {% partial 'country' item=country %}
    {% partial 'state' item=country %}
    show country specific data - or better city partial
    {% partial 'city' item=city %}
{% endif %}

这应该对您没有任何问题

[如有疑问请发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.