我是octoberCMS的新手!考虑到我一直在开发其他类型的CMS,它们通常会自动处理url。因此,让我们考虑一下,我们有一个包含国家及其相对州和城市的网站。当然,所有这些都使用模型中的关系。
如果我使用网址创建此页面
/:country-slug
我可以通过查询国家信息来轻松访问内容
/美国
如果我想进入费城(Philadelphia),我会遇到类似这样的事情
/:country-slug /:state-slug /:city-slug
这样就行了
/美国/宾夕法尼亚州/费城
但要使其真正起作用和是唯一的,您需要检查“城市”模型是否具有与其连接的确切国家和州。否则,如果仅检查最后一个参数:city-slug,则会创建虚假或更糟糕的无限重复内容,其中每个州/国家/地区的组合都可以与费城一起使用,因此该URL也可以使用
/意大利/加利福尼亚州/费城
这是正确的方法吗?这样每个带有多个参数的URL都需要在后端手动检查吗?还是我在这里想念什么?对于非常长的URL,此方法似乎非常复杂,而对于实际的动态内容而言却不太实用。
创建具有以下定义的页面:
url = "/:country-slug/:state-slug?/:city-slug?"
[countryComponent]
country = ":country-slug"
state = ":state-slug"
city = ":city-slug"
在component内检查external property values是否为空,并相应地过滤/搜索模型。如果找不到模型,则返回404。那不切实际吗?
它看起来很复杂,但确实很简单,您只需要一次检查,只需要在需要时重定向到正确的位置。
只需使用此子弹
/:country-slug?/:state-slug?/:city-slug?
您可以看到我们将这3个都设为可选。
通过这种方式,您实际上可以记录请求,并知道用户正在搜索哪些项目以及哪些鼻涕虫穿着。
现在您可以在组件或模板中执行以下操作。
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 %}
这应该对您没有任何问题
[如有疑问请发表评论。