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

Unable to access other functions of controller Codeigniter

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

HTACESS

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

配置

$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

打开 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'])) . '/';

并在 HTACCESS 文件中放入以下代码

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

找到以下内容:

打开 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

的情况下访问页面、方法等

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

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

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

<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  !^(index\.php|images|css|js|include|style\.css|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/ [L]

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