【问题标题】:codeigniter 3 search button error 404codeigniter 3 搜索按钮错误 404
【发布时间】: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


【解决方案1】:

@Terchilă Marian,你的代码绝对没问题,你必须做什么:

用'index.php'检查是否


1 - 'http://localhost/teste/index.php/mycontroller' 有效吗?

2 - 'http://localhost/teste/index.php/mycontroller/search_actor' 有效吗?

如果上面的 url 工作正常并且你的代码执行没有任何错误 然后就好了。

检查是否没有'index.php'


1 - 'http://localhost/teste/mycontroller' 有效吗?

2 - 'http://localhost/teste/mycontroller/search_actor' 有效吗?

如果上面的 url 工作正常并且你的代码执行没有任何错误 那你就不用担心任何事情了。代码点火器 一切准备就绪!

如果不是那么:

在存在index.php 的项目根文件夹中创建一个.htaccess文件(只需创建一个空白文本文件并在文件夹/teste 中重命名.htaccess.。

将以下代码放入 .htaccess 文件中以启用 re_write 模式

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond $1 !^(index\.php|resources|robots\.txt)
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
</IfModule>

打开./teste/index.php 并在顶部将以下代码为CodeIginter 设置动态基本网址,而不是在config 文件中手动执行。

/*
|--------------------------------------------------------------------------
| Dynamic Base Url
|--------------------------------------------------------------------------
| Put below code in './index.php' on top
|
*/
    define('APP_URL', ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . "://{$_SERVER['SERVER_NAME']}".str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']));

打开./teste/application/config/config.php 并更新设置

/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
|   http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = APP_URL;

/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = '';

/*
|--------------------------------------------------------------------------
| Cross Site Request Forgery
|--------------------------------------------------------------------------
| Enables a CSRF cookie token to be set. When set to TRUE, token will be
| checked on a submitted form. If you are accepting user data, it is strongly
| recommended CSRF protection be enabled.
|
| 'csrf_token_name' = The token name
| 'csrf_cookie_name' = The cookie name
| 'csrf_expire' = The number in seconds the token should expire.
*/
$config['csrf_protection'] = FALSE;

不建议设置$config['csrf_protection'] = FALSE;,但如果您不使用CodeIgniter CSRF 保护安全,请设置FALSE。因为当您设置此 TRUE 并使用 Form Submission 或 Ajax Submission 时,您必须将 CSRF 代码放入您的 Form 中或在 Ajax 数据参数中传递 例如

表单发布:

<form action='<?=base_url("mycontroller/search_actor")?>' method='post'>
<input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash();?>" />
</form>

或者,更改./teste/application/config/autoload.php 配置文件以自动加载表单助手,如下所示:

/*
| -------------------------------------------------------------------
|  Auto-load Helper Files
| -------------------------------------------------------------------
| Prototype:
|
|   $autoload['helper'] = array('url', 'file');
*/
$autoload['helper'] = array('form');

内部视图,

<?php echo form_open('mycontroller/search_actor'); ?>
// other form elements
<?php echo form_close(); ?>

Ajax 请求:

<!-- CSRF Hack , Put this code inside head section, so entire body can access-->
<script type="text/javascript">
//<![CDATA[
window.CSRF = {};
window.CSRF['<?php echo $this->security->get_csrf_token_name(); ?>'] = '<?php echo $this->security->get_csrf_hash(); ?>';
$(function() {
     // Attach csfr data token within each ajax request
     $.ajaxSetup({
        data: window.CSRF
     });
 });
 //]]>
</script>

用于测试目的的 Mycontroller 控制器:

<?php
// No direct Script allowed
defined('BASEPATH') or exit('No direct script access allowed');

/**
 * My Test Controller
 */
class Mycontroller extends CI_Controller
{
    /**
     * [index description]
     * @return [type] [description]
     */
    public function index()
    {
        echo "Yes! CodeIgniter Controller working";
    }

    /**
     * [search_actor description]
     * @return [type] [description]
     */
    public function search_actor()
    {
        echo "Yes! CodeIgniter Method working";
    }
}
/* End of file Mycontroller.php */
/* Location: ./application/controllers/Mycontroller.php */

【讨论】:

    【解决方案2】:

    你能把 NAME mycontroller.php 改成 Mycontroller.php 并尝试一次吗?

    【讨论】:

    • 1.i.stack.imgur.com/UPYhr.png 删除此图片链接;2。 mycontroller.php 到 Mycontroller.php 3. 控制器 -> 索引函数第一行 -> echo 'hello';死();检查您的控制器是否正常工作并逐步移动
    • 回声“你好”;死();当我放入 search_actor 时,仅在公共函数 index() 中有效,它不会显示你好
    • 'localhost/mycontroller/search_actor' 你是不是用这种类型的url来打函数和这个函数的第一行“echo 'hello'; die();”正确的?但你不打招呼;
    • 这样检查 /mycontroller/search_actor' ;比你确定你的功能没问题
    • 我不明白,我必须把那条线放在哪里?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-09
    • 2018-06-09
    • 2019-07-21
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    相关资源
    最近更新 更多