【发布时间】:2017-11-24 07:39:07
【问题描述】:
我在我的网站上使用 codeigniter3,我尝试为用户创建一个搜索按钮,但我总是得到 找不到对象!
在此服务器上找不到请求的 URL。引用页面上的链接似乎是错误的或过时的。请将该错误告知该页面的作者。
如果您认为这是服务器错误,请联系网站管理员。 错误 404 本地主机 Apache/2.4.25 (Win32) OpenSSL/1.0.2j PHP/5.6.30
代码如下:
mycontroller.php
<?php
defined ('BASEPATH') OR exit('No direct script access allowed');
class Mycontroller extends CI_Controller {
public function index()
{
$this->load->library('pagination');
$this->load->model('mymodel');
$config = array();
$config['base_url'] = base_url(). 'mycontroller/index';
$config['total_rows'] = $this->mymodel->count_actor();
$config['per_page'] = 10;
$config['num_links'] = 3;
//$config['uri_segment'] = 3;
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$this->pagination->initialize($config);
$page = $this->uri->segment(3);
$data['actor'] = $this->mymodel->fetch_actor($config['per_page'], $page);
$data['links'] = $this->pagination->create_links();
$this->load->view('actor_lists',$data);
}
public function search_actor() {
$this->load->model('mymodel');
$firstname = $this->input->post('search');
if(isset($firstname) and !empty($firstname)){
$data['actor'] = $this->mymodel->search_actor($firstname);
$data['links'] = '';
$this->load->view('actor_lists', $data);
}
else
{
redirect($this->index());
}
}
}
?>
mymodel.php
<?php
defined ('BASEPATH') OR exit('No direct script access allowed');
class Mymodel extends CI_Model {
public function count_actor() {
return $this->db->count_all('actor');
}
public function fetch_actor($limit, $offset) {
$this->db->limit($limit, $offset);
$query = $this->db->get('actor');
if($query->num_rows() > 0 ) {
return $query->result();
}
else {
return $query->result();
}
}
public function search_actor($firstname) {
$this->db->select('*');
$this->db->from('actor');
$this->db->like('firstname', $firstname);
$query = $this->db->get();
if($query->num_rows > 0 )
{
return $query->result();
}
else
{
return $query->result();
}
}
}
?>
actor_lists.php
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="http://bootswatch.com/readable/bootstrap.css">
</head>
<div align="center" id="container">
<h1>Actor Lists</h1>
<form class="form-inline" role="form" action="<?php echo base_url().'mycontroller/search_actor'; ?>" method="post">
<div class="form-group">
<input type="text" class="form-control" name="search" id="search" placeholder="Cauta dupa nume">
</div>
<button type="submit" class="btn btn-info" name="submit">Submit</button>
</form><br/>
<table style="width: 978px"; class="table table-striped">
<thead style="background-color: #2aabd2;">
<tr>
<th>Actor ID</th>
<th>Last Name</th>
<th>First Name</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach ($actor as $row) { ?>
<tr>
<td><?php echo $row->actorid; ?></td>
<td><?php echo $row->lastname; ?></td>
<td><?php echo $row->firstname; ?></td>
<td><?php echo $row->date; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php echo $links ?>
</div>
【问题讨论】:
-
首先,检查 mycontroller/search_actor 是否可以从 url 访问。尝试做一个简单的回显并检查方法是否从浏览器 url 回显消息。如果是,请确保在配置文件中 csrf 安全必须关闭。因为您正在发布一个带有 POST 且没有 CSRF 令牌的表单,如果在 Config 中启用了 CSRF,Codeigniter 将不会调用方法。如果 CSRF 关闭,则在 else 条件下删除重定向代码,因为重定向将字符串作为参数,并且您在其中调用类方法。祝你好运:)
-
仍然没有工作
-
尝试从控制器名称和模型名称中删除“我的”字样,使用其他名称并试一试...因为 Codeigniter 使用“MY_”表示自定义核心模型和控制器。还要确保使用 .htaccess 代码删除 index.php 代码,否则请使用“http://www.website.com/index.php/controller/method”。
标签: php html codeigniter button search