【问题标题】:URI not working and just adding to URLURI 不起作用,只是添加到 URL
【发布时间】:2016-11-22 08:55:20
【问题描述】:

我正在尝试了解自定义 URI。我希望我的view_county 有网址:

http://localhost/chipadvisor2/controller_ca/all_county/1

和我的view_all 获得网址:

http://localhost/chipadvisor2/controller_ca/all_chippers

如果有人可以帮助我,我将不胜感激。该链接适用于view_county,但是当我点击view_all 链接时,它会显示:

{
    "status": false,
    "error": "Unknown method"
}

如果我先点击view_county,然后点击view_all 链接,它只会不断将controller_ca/all_chippers 添加到URL 中,什么也不做。任何想法为什么?

model_ca

<?php
class Model_ca extends CI_Model {
    function __construct() {
        parent::__construct();
    }

    function get_chipper() {
        $query = $this->db->get('chipper_reviews');
        return $query->result();
    }

    function get_county($id) {
        $this->db->where('location', 'Westmeath');
        $query = $this->db->get('chipper_reviews');
        return $query->result();
    }

}
?>

controller_ca

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH.'/libraries/REST_Controller.php';

class Controller_ca extends REST_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->helper('url');
        $this->load->model('model_ca');
    }

    public function index_get() {  
        $this->all_chipper_get();
    }

    public function all_chipper_get() {
        $data['chipper'] = $this->model_ca->get_chipper();
        $this->load->view('view_all', $data);
    }    

    public function all_county_get() {
        $id = $this->uri->rsegment(3);
        $data['location'] = $this->model_ca->get_county('location',$id);
        $this->load->view('view_county', $data);
    }
}
?> 

view_county

​​>

查看全部

<html>
    <head> </head>
    <body>
        <div class="container">
            <a href="controller_ca/all_chippers">View All Chippers</a>
            <br/>
            <a href="controller_ca/all_county/1">View County</a>

            <?php echo "<br/><br/>";
            foreach ($chipper as $chipperdata) {
                echo "<b>Name: </b>".$chipperdata->name." "."<br/>"."<b>County: </b>".$chipperdata->location." "."<br/>"."<b>Description: </b>".$chipperdata->description." "."<br/>"."<br/>";
            } ?>
        </div>
    </body>
</html> 

我也已将此添加到 route.php 文件中,但不知道如何使其生效或如何将值“westmeath”添加到末尾。理想情况下,我想将其作为变量传递,而不是对其进行硬编码:

$route['default_controller'] = 'Controller_ca';
$route['404_override'] = '';
$route['translate_uri_dashes'] = false;

$route['all_chipper'] = 'controller_ca/all_chippers';

$route['all_county'] = 'controller_ca/all_county/$1';

【问题讨论】:

  • 我猜问题出在你的路由上。您没有在路由规则中的函数名称之前提到控制器名称,例如 $route['controller_ca/all_county'] = 'controller_ca/all_county/$1';$route['controller_ca/all_chipper'] = 'controller_ca/all_chippers';...
  • 我试过了,它阻止了两个链接的工作。
  • 有一件事我不明白,为什么第二个链接&lt;a&gt;controller 而不是controller_ca?甚至这也在路由中。
  • 哎呀,现在已经改变了,但仍然是同样的问题,每次我点击一个链接时将controller_ca/all_chipperscontroller_ca/all_county/1 添加到我的网址,而不是简单地反复点击。跨度>
  • 我没有得到你。现在是什么错误?

标签: php codeigniter uri


【解决方案1】:

尝试给控制器方法名称与 url 相同。将all_chipper_get() 更改为all_chipper()。其他方法相同并检查。

【讨论】:

  • 不,还是不行。 _get 在那里,因为我使用的是 RESTful。
【解决方案2】:

关于问题:

如果我先点击 view_county 然后 view_all 链接它只是 不断将 controller_ca/all_chippers 添加到 URL 并且什么都不做。 任何想法为什么?

请在锚标记的 URL 开头添加base_url()。它将解决 URL 中的append 问题。示例:

<a href="<?php echo base_url(); ?>/controller_ca/all_chippers">View All Chippers</a>
<br/>
<a href="<?php echo base_url(); ?>/controller_ca/all_county/1">View County</a>

另外,将您的路由更改为:

$route['controller_ca/all_county'] = 'controller_ca/all_county/$1';
$route['controller_ca/all_chipper'] = 'controller_ca/all_chippers';

【讨论】:

  • 嗯...现在这将我带到 404 页面?
  • 发生了什么是它没有添加我的基本网址,这很奇怪吗?它只是将 url 更改为 localhost/controller_ca/all_county/$1 而不是 localhost/folder/controller_ca/all_county/$1
  • 您是否在config.php 中定义了基本路径?
  • 我没有,因为我打开了 rewrite mod,但是当我这样做时,我看到了这个消息:` {"status":false,"error":"Unknown method"}`
  • 你能告诉你在配置文件的base_url行中到底定义了什么吗?
猜你喜欢
  • 2017-04-30
  • 1970-01-01
  • 2014-06-15
  • 2019-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多