【发布时间】: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';... -
我试过了,它阻止了两个链接的工作。
-
有一件事我不明白,为什么第二个链接
<a>有controller而不是controller_ca?甚至这也在路由中。 -
哎呀,现在已经改变了,但仍然是同样的问题,每次我点击一个链接时将
controller_ca/all_chippers和controller_ca/all_county/1添加到我的网址,而不是简单地反复点击。跨度> -
我没有得到你。现在是什么错误?
标签: php codeigniter uri