简体   繁体   English

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

[英]Unable to access other functions of controller Codeigniter

I am unable to access other functions in my controllers until i add index.php? 在添加index.php?之前,我无法访问控制器中的其他功能index.php? to the url. 到网址。 I can access the index() functions of any controller directly but not the other functions. 我可以直接访问任何控制器的index()函数,但不能访问其他函数。 i have checked other related questions but problem persists. 我检查了其他相关问题,但问题仍然存在。 i have also tried adding all controllers to routes.php but still fails. 我也尝试过将所有控制器添加到routes.php但仍然失败。

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

BLOG CONTROLLER 博客控制器

<?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

You need to check your css and js includes properly. 您需要检查您的CSS和js是否正确包含。 Make sure to use absolute url. 确保使用绝对网址。 From your question you seem to be loading all your views through one index file in (pages) folder 从您的问题来看,您似乎正在通过(页面)文件夹中的一个索引文件加载所有视图

Your assets or resource links should be like this 您的资产或资源链接应如下所示

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

Do same for javascript or other assets 对javascript或其他资产执行相同的操作

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

$config['index_page'] = '';

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

HTACCESS 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]

This way all your pages will load properly including all the functions in your controller 这样,您的所有页面都将正确加载,包括控制器中的所有功能

Open config.php and do following replaces 打开config.php并执行以下替换

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

to

$config['index_page'] = ""

Just replace 只需更换

$config['uri_protocol'] ="AUTO"

to

$config['uri_protocol'] = "REQUEST_URI"

change from

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

to

$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 put following code 并且在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]

Find the following: 查找以下内容:

Open config.php and do following replaces 打开config.php并执行以下替换

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

to

$config['index_page'] = ""

In some cases the default setting for uri_protocol does not work properly. 在某些情况下,uri_protocol的默认设置无法正常工作。 Just replace 只需更换

$config['uri_protocol'] ="AUTO"

to

$config['uri_protocol'] = "REQUEST_URI"

This will remove index.php from your URL and you can access pages, methods etc without index.php 这将从您的URL中删除index.php,您无需使用index.php就可以访问页面,方法等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM