【问题标题】:How to remove controller name and function name in codeigniter如何在codeigniter中删除控制器名称和函数名称
【发布时间】:2018-04-11 11:49:43
【问题描述】:

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

这里 Site 是控制器名称,location 是其中的函数名称。

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

$route['location/(:any)'] = 'site/location';

但我也想从 url 中删除函数名称位置,并且 url 应该是 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->segment 捕获城市和类别名称,并将其用于过滤过程,这绝对没有问题。

但主要问题仍然是删除函数名称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');
  }
}

我认为我在索引或位置函数中犯了一些错误。位置功能只是显示索引视图。索引视图有代码来抓取 uri 段并相应地过滤数据。

我的代码将状态 id 传递给 jquery 函数:

<a onclick="get_cities(2)">Andaman &amp; 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['(:any)'] = '站点'; $route['(:any)/(:any)'] = '站点';

get_cities 函数返回索引页面的 html 代码的警报,并且索引页面也显示在打开的模型中。

如果我在上面两行评论,我会得到城市列表,它会返回城市的 url

<a href="http://example.com/visakhapatnam/">Visakhapatnam</a>

但是由于上面城市 url 上面注释的 2 条路线给出了 404 的错误。

【问题讨论】:

    标签: codeigniter


    【解决方案1】:

    希望对您有所帮助:

      $route['location'] = 'site/index'; /* redirect to index method */
      $route['location/(:any)'] = 'site/location'; /* redirect to location*/
      $route['location/(:any)/(:any)'] = 'site/location/$1';
    

    【讨论】:

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

    像这样添加

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-02
      • 1970-01-01
      • 2011-11-28
      • 2014-12-16
      • 2017-04-10
      • 1970-01-01
      • 2011-08-04
      • 1970-01-01
      相关资源
      最近更新 更多