我在codeigniter中创建一个目录站点。在登陆页面加载时,它会提示选择州和城市。然后,它会从 www.example.com 重定向到 www.example.com/location/city-name / 。
此处Site是控制器名称,location是其中的函数名称。
我已经使用route.php从网址中删除了控制器名称。代码如下:
$route['location/(:any)'] = 'site/location';
但我想从网址中删除功能名称位置,网址应为 www.example.com/city-name / 。
此外,在选择城市后选择类别时,应将其重定向到 www.example.com/city-name/category-name / ,但我可以重定向到 www。 example.com/location/city-name/category-name / 仅在route.php中使用以下行:
$route['location/(:any)/(:any)'] = 'site/location';
我可以使用$ this-> uri->段捕获城市和类别名称,并使用相同的过滤器进程,绝对没有问题。
但主要问题仍然是删除功能名称位置。
我在发布此问题之前已搜索了建议的问题,但无法找到任何合适的解决方案。任何人都可以帮我实现目标吗?
我的网站控制器如下:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('file');
$this->load->model('query_model');
$this->load->library("pagination");
error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
}
public function index(){
$this->load->view('website/index');
}
public function location(){
$this->load->view('website/index');
}
}
我认为我在索引或位置功能方面犯了一些错误。位置功能不仅仅是显示索引的视图。索引视图具有抓取uri段并相应地过滤数据的代码。
我的代码将状态id传递给jquery函数:
<a onclick="get_cities(2)">Andaman & Nicobar Islands</a>
我的jquery代码get_cities(id)如下:
function get_cities(id){
var aj_url="<?= base_url();?>ajax_post_controller/show_cities";
jQuery.ajax({
type: "POST",
url: aj_url,
data: {id: id,},
success: function(res, status) {
if (res)
{
alert(res);
$("#loc-head").html("Select City");
$("#loc-data").html(res);
$(".modal-footer").css("display","block");
}
}
});
}
定义以下代码后: $ route [&#39;(:any)&#39;] =&#39; site&#39 ;; $ route [&#39;(:any)/(:any)&#39;] =&#39; site&#39;;
来自get_cities函数的警报返回索引页面和索引页面的html代码也会在打开的模型中显示。
如果我在上面两行评论,我会得到城市列表,它会返回城市的网址
<a href="http://example.com/visakhapatnam/">Visakhapatnam</a>
但是由于以上2条路线注释了城市网址,因此错误为404。
答案 0 :(得分:1)
希望这会对您有所帮助:
$route['location'] = 'site/index'; /* redirect to index method */
$route['location/(:any)'] = 'site/location'; /* redirect to location*/
$route['location/(:any)/(:any)'] = 'site/location/$1';
答案 1 :(得分:0)
像这样添加
在routes.php中
$route['location/(.*)'] = 'site/index';
在控制器中
public function index(){
if($this->uri->segment(1) && $this->uri->segment(2)){
$this->load->view('website/category');
}elseif($this->uri->segment(1)){
$this->load->view('website/city');
}else{
$this->load->view('website/index');
}
}