无法访问控制器Codeigniter的其他功能

时间:2017-01-11 06:11:35

标签: php .htaccess codeigniter

在将index.php?添加到网址之前,我无法访问控制器中的其他功能。我可以直接访问任何控制器的index()功能,但不能访问其他功能。我检查了其他相关问题,但问题仍然存在。我也尝试将所有控制器添加到routes.php但仍然失败。

HTACESS

RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|include|style\.css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

CONFIG

$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';

$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';

博客控制器

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

class Blog extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->database();

    }

    public function index()
    {
        $data['page_name'] = 'blog';
        $this->load->view('pages/index', $data);
    }

    public function news()
    {
        $data['page_name'] = 'news';
        $this->load->view('pages/index', $data);
    }
}

URL

   http://localhost/tsb/blog // This works fine
    http://localhost/tsb/blog/news // only works when i add index.php? to the url

3 个答案:

答案 0 :(得分:1)

您需要正确检查您的css和js包含。确保使用绝对网址。根据您的问题,您似乎通过(页面)文件夹

中的一个索引文件加载所有视图

您的资产或资源链接应该是这样的

<link rel="stylesheet" type ="text/css" src="<?php echo base_url();?>css/style.css" />

对javascript或其他资源执行相同操作

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

$config['index_page'] = '';

 $config['uri_protocol'] = 'REQUEST_URI';

htaccess的

RewriteEngine on RewriteCond $1 !^(index\.php|images|css|js|include|style\.css|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]

这样您的所有页面都会正确加载,包括控制器中的所有功能

答案 1 :(得分:0)

打开config.php并执行以下替换

$config['index_page'] = "index.php"

$config['index_page'] = ""

只需替换

$config['uri_protocol'] ="AUTO"

$config['uri_protocol'] = "REQUEST_URI"

改变
$config['base_url'] = 'http://localhost/tsb/';

$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';

$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . preg_replace('@/+$@', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';

AND IN HTACCESS FILE下面的代码

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

答案 2 :(得分:0)

找到以下内容:

打开config.php并执行以下替换

$config['index_page'] = "index.php"

$config['index_page'] = ""

在某些情况下,uri_protocol的默认设置无法正常运行。只需替换

$config['uri_protocol'] ="AUTO"

$config['uri_protocol'] = "REQUEST_URI"

这将从您的URL中删除index.php,您可以访问没有index.php的页面,方法等