我正试图了解自定义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
并且什么都不做。有什么想法吗?
<?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();
}
}
?>
<?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);
}
}
?>
和
<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';
答案 0 :(得分:0)
关于这个问题:
如果我先点击进入view_county,那么view_all就会链接它 继续将controller_ca / all_chippers添加到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';
答案 1 :(得分:0)
尝试将控制器方法名称与url相同。将all_chipper_get()
更改为all_chipper()
。其他方法与检查相同。