Php CodeIgniter-更多情况下的URL路由
以下是我想要实现的目标:Php CodeIgniter-更多情况下的URL路由,php,codeigniter,url,routes,codeigniter-3,Php,Codeigniter,Url,Routes,Codeigniter 3,以下是我想要实现的目标: 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
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用于属性/属性\详细信息函数,哪个URL用于属性/索引函数?
如下设置您的route.php
:
访问url:
这是一种直接索引方法
https://www.example.com/properties/index
这将指导您使用属性详细信息方法
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在属性之后有什么
你可以这样改变路线的顺序来尝试
$route['properties(/:any)'] = 'properties/property_details/$1';
$route['properties/index'] = 'properties';
它始终根据您放置的路线的顺序工作。如果有可接受的参数,对于程序,properties/index
也类似于properties(/:any)
。所以,为了区分这两者,我们必须改变这样的路线顺序 尝试使用$route['properties/(:any)]='properties/$1'$路线['properties/(:any)/(:any)]='properties/$1/$2'代码>$route['properties(/:any')用于属性\u详细信息功能。我还需要处理property_details函数如何处理property/property_details请求?我是否还需要添加此行?$route['properties(/:any)]=“properties/property_details$1”;。。。在您的代码中没有对财产详细信息的讨论方法$1
将处理您对财产详细信息的请求和$2
将处理2br-PLANTION-for-sale-100
在我的控制器(财产)中有两种功能/方法(索引和财产详细信息)。我怀疑你所有的代码将只使用索引函数,而不会使用属性_细节函数。这是两个不同的函数,我想从url中隐藏属性_的详细信息,并使其对用户友好
$route['properties(/:any)'] = 'properties/property_details/$1';
$route['properties/index'] = 'properties';