CodeIgniter语言 - 更多情况下的 URL 路由



这是我想要实现的目标:

https://www.example.com/properties
https://www.example.com/properties/properties-in-newyork
https://www.example.com/properties/properties-in-DC/property-for-rent
https://www.example.com/properties/all-cities/property-for-rent
https://www.example.com/properties/all-cities/property-for-sale

以上所有内容仅供搜索。现在我想获取详细信息页面,例如:

https://www.example.com/properties/2br-apartment-for-sale-100

我想区分搜索和详细信息页面链接。这是我尝试过的:

$route['properties/index']  = 'properties';
$route['properties(/:any)'] = 'properties/property_details$1';

如何区分哪个 URL 用于属性/property_details函数,哪个 URL 用于属性/索引函数? 在此处输入图像描述

像这样设置你的route.php

$route['properties/index']  = 'properties';
$route['properties'] = 'properties/property_details';
$route['properties/(:any)'] = 'properties/property_details/$1';

访问网址 :

这种直接的索引方法

https://www.example.com/properties/index

这将指导您property_details方法

https://www.example.com/properties/

控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Properties extends CI_Controller {
public function __construtct()
{
parent::__construtct();
$this->load->helper('url');
}
public function index()
{
echo 'index';
}
public function property_details($component = NULL)
{
echo 'property_details';
echo $component;
}
}

如果我是对的,根据您对区分路由的解释,您遇到的问题是,无论您的 URL 在properties之后有什么,它始终运行路由index

您可以通过像这样更改路线的顺序来尝试;

$route['properties(/:any)'] = 'properties/property_details/$1';
$route['properties/index']  = 'properties';

它始终根据您放置的路线顺序工作。如果有可接受的参数,对于程序来说,properties/index也类似于properties(/:any).因此,为了区分这两者,我们必须像这样更改路由的顺序。

最新更新