如何删除codeigniter中的控制器名和函数名

如何删除codeigniter中的控制器名和函数名,codeigniter,Codeigniter,我正在codeigniter中创建一个目录站点。在登录页加载时,它会提示选择州,然后选择城市。然后它从www.example.com重定向到www.example.com/location/city name/ 此处Site是控制器名称,location是其中的函数名称 我已经使用route.php从url中删除了控制器名称。代码如下: $route['location/(:any)'] = 'site/location'; <?php defined('BASEPATH') OR ex

我正在codeigniter中创建一个目录站点。在登录页加载时,它会提示选择州,然后选择城市。然后它从www.example.com重定向到www.example.com/location/city name/

此处Site是控制器名称,location是其中的函数名称

我已经使用route.php从url中删除了控制器名称。代码如下:

$route['location/(:any)'] = 'site/location';
<?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');
  }
}
        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");
                }
            }
        });
    }
但是我想从url中删除函数名位置,url应该是www.example.com/city name/

此外,如果在选择城市后选择了类别,则应将其重定向到www.example.com/city name/category name/,但我只能使用route.php中的以下行重定向到www.example.com/location/city-name/category-name/:

$route['location/(:any)/(:any)'] = 'site/location';
我可以使用$this->uri->segment捕获城市和类别名称,并将其用于过滤过程,这绝对没有问题

但主要问题仍然是删除函数名位置

在发布此问题之前,我搜索了建议的问题,但找不到任何合适的解决方案。有人能帮我实现这个目标吗

我的站点控制器如下:

$route['location/(:any)'] = 'site/location';
<?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');
  }
}
        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['location'] = 'site/index'; /* redirect to index method */
  $route['location/(:any)'] = 'site/location'; /* redirect to location*/
  $route['location/(:any)/(:any)'] = 'site/location/$1';
像这样加上

在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');
    }   
}

我已将编辑更改为“
$route['location/(:any)”='site/$1'
”。在登陆www.example.com选择state和city后,它指向url www.example.com/palasa kasibugga/。它应该显示根据所选城市过滤的结果。但它显示404页未找到。我无法理解的原因。这取决于您如何重定向页面和参数passedI将页面重定向到www.example.com/palasa kasibugga/。base_url().$city->slug;。如果它是错误的,你能给我一个我的url应该是什么的例子吗?位置字符串u r在哪里重定向所有站点url以通过位置,还有你的默认控制器是什么?现在我添加了www.example.com/location/north lakhimpur/。仍然显示404页未找到。我正在编辑问题以添加我的控制器和位置功能。