Codeigniter:调用控制器时出现 404

Posted

技术标签:

【中文标题】Codeigniter:调用控制器时出现 404【英文标题】:Codeigniter: 404 on calling controller 【发布时间】:2016-10-31 19:17:21 【问题描述】:

访问网址-http://localhost:82/admin/login 然后- 我收到 404“找不到页面错误” 即-

找不到对象!

在此服务器上找不到请求的 URL。如果您输入了 URL 请手动检查您的拼写,然后重试。

如果您认为这是服务器错误,请联系网站管理员。

错误 404

本地主机 Apache/2.4.9 (Win32) OpenSSL/0.9.8y php/5.4.27

我的 config.php

$config['base_url'] = 'http://localhost:82/admin';

/*
|--------------------------------------------------------------------------
| 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'] = '';

/*
|--------------------------------------------------------------------------
| URI PROTOCOL
|--------------------------------------------------------------------------
|
| This item determines which server global should be used to retrieve the
| URI string.  The default setting of 'REQUEST_URI' works for most servers.
| If your links do not seem to work, try one of the other delicious flavors:
|
| 'REQUEST_URI'    Uses $_SERVER['REQUEST_URI']
| 'QUERY_STRING'   Uses $_SERVER['QUERY_STRING']
| 'PATH_INFO'      Uses $_SERVER['PATH_INFO']
|
| WARNING: If you set this to 'PATH_INFO', URIs will always be URL-decoded!
*/
$config['uri_protocol']	= 'REQUEST_URI';

我的路线.php

$route['default_controller'] = 'admin/index';
$route['404_override'] = '';
//$route['translate_uri_dashes'] = FALSE;


/*admin*/
//$route['admin'] = 'user/index';
//$route['admin/signup'] = 'user/signup';
//$route['admin/create_member'] = 'user/create_member';
$route['admin'] = 'admin';
//$route['admin/logout'] = 'admin/logout';


$route['package'] = 'package';

我的控制器 Admin.php

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

class Admin extends CI_Controller 

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/admin
	 *	- or -
	 * 		http://example.com/index.php/admin/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see https://codeigniter.com/user_guide/general/urls.html
	 */
       function __construct() 
        parent::__construct();
        $this->load->helper('url');
         $this->load->helper('form');
        $this->load->model('LoginModel');
       $this->load->library('session');
    
      
      
    
	public function index()
	
            if($this->session->userdata('is_logged_in'))
			redirect('package/packages');
        else
        	$this->load->view('admin/admin');	
        
		
	
          function __encrip_password($password) 
        return md5($password);
    	
         /**
    * check the username and the password with the database
    * @return void
    */
        public function login()
		
      echo "hi"; exit;
		

		$user_name = $this->input->post('username');
		$password = $this->__encrip_password($this->input->post('password'));

		$is_valid = $this->LoginModel->validate($user_name, $password);
		
		if($is_valid)
		
			$data = array(
				'user_name' => $user_name,
				'is_logged_in' => true
			);
			$this->session->set_userdata($data);
			redirect('package/package');
		
		else // incorrect username or password
		
			$data['message_error'] = TRUE;
			$this->load->view('admin/admin', $data);	
		
	
        
       

   
	
		

    /**
    * The method just loads the signup view
    * @return void
    */

【问题讨论】:

显示您尝试访问的控制器代码和 URL 我添加了有问题的网址 什么版本的 CI 你的admin文件名Admin.php首字母大写 在您的基本网址中以正斜杠 / 结束它,例如 $config['base_url'] = 'http://localhost:82/yourprojectname/'; 【参考方案1】:

也许你有 .htaccess,因为你的控制器看起来不错,但你仍然得到这个答案。请尝试在您的根目录中添加 .htaccess。

【讨论】:

【参考方案2】:

routes.php

$route['default_controller'] = 'admin/index';

你猜应该是?

$route['default_controller'] = 'admin';

【讨论】:

以上是关于Codeigniter:调用控制器时出现 404的主要内容,如果未能解决你的问题,请参考以下文章

调用未定义函数 codeigniter\locale_set_default() 时出现 codeigniter 错误

当我需要 CodeIgniter 错误时出现 PHP 错误

调用 CodeIgniter 控制器方法返回 404

尝试使用 mysqli 连接到数据库时出现 Codeigniter 错误

Codeigniter:当我插入数据时出现重复键错误,我该如何处理这个错误?

在 PHP,Codeigniter 中提交带有空字段的表单时出现一些错误