簡體   English   中英

嘗試訪問CodeIgniter中的控制器時出現錯誤404

[英]Error 404 when trying to access a controller in CodeIgniter

我正在嘗試訪問CodeIgniter控制器,但出現404錯誤。 在我的計算機上,我可以訪問.../index.php/apadrinhamentoController/padrinhosPossiveis

但是當上傳到服務器時,我只能訪問site.com/apadrinhamento/index.php的索引文件,而不是site.com/apadrinhamento/index.php/apadrinhamentoController的索引文件(獲取404錯誤)。

apadrinhamentoController.php文件

<?php
class apadrinhamentoController extends CI_Controller{
public function __construct() {
    parent::__construct();
    $this->load->helper('url');
}
public function index() {
    $this->load->view('apadrinhamento');
}
}

CodeIgniter默認提供index.php文件

我不在/apadrinhamento上使用.htaccess

首先,您應該了解Codeigniter != CakePHP 意味着您無需定義controller_name + Controller word。

class apadrinhamento extends CI_Controller
{
    public function __construct() 
    {
        parent::__construct();
        $this->load->helper('url');
    }
    public function index() 
    {
        //$this->load->view('apadrinhamento');
        echo 'Im index of your function';
    }

    public function my_method() 
    {
        echo 'I jsut access my_Method';
    }

}

並在config/routes.php

$route['default_controller'] = "";//Default Controller Name
$route['404_override'] = '';

訪問您的控制器(以上)

site.com/apadrinhamento/ //this will access index() function
//or
site.com/index.php/apadrinhamento/

如果要訪問其中的方法(URL應該是)

site.com/apadrinhamento/my_method
//or
site.com/index.php/apadrinhamento/my_method

請注意,如果您未放置.htaccess則網址應為site.com/index.php/apadrinhamento/

問題是在CodeIgniter v3.0中,我們需要編寫以大寫字母開頭的控制器文件名。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM