首页 文章

路由在localhost中工作,但不在codeigniter项目的在线主机中工作

提问于
浏览
0

我是CI的新手,请帮我解决 .

路由在localhost中工作,但不在codeigniter项目的在线主机中工作

config.php

$root=(isset($_SERVER['HTTPS']) ? "https://" : "http://").$_SERVER['HTTP_HOST'];
$root.= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $root;

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

routes.php

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['test'] = 'test';

Controller - test.php

class test extends CI_Controller {
    public function index()
    {
        echo "Hi! This is at test";
    }
}

Problem is

http://localhost/CodeIgniter-3.0.0/index.php/test -----有效

http://codeignitertests.site50.net/index.php/test ------不要工作

1 回答

  • 1

    当你创建一个控制器时,类和文件名必须有首字母大写

    Test.php

    <?php
    
    class Test extends CI_Controller {
        public function index() {
           echo "Working";
        }
    }
    

    与以前的版本没有问题,但现在在CI3版本中,您必须具有所有类和文件名大写的首字母 .

    例如,在调用模型时也是如此 .

    Test_model.php

    <?php
    
    class Test_model extends CI_Model {
        public function get() {
           // some db stuff here.
        }
    }
    

    将模型加载到控制器上

    <?php
    
    class Test extends CI_Controller {
    
        public function __construct() {
           parent::__construct();
           $this->load->model('test_model');
        }
    
        public function index() {
    
           $data['test_results'] = $this->test_model->get();
           $this->load->view('test_page', $data);
        }
    }
    

    使用base_url,如果需要,您可以留空,并且应该自动获取您的URL

    $config['base_url'] = '';
    

相关问题