CI new default controller not working

試著忘記壹切 提交于 2020-04-30 04:59:47

问题


Have problem with CI 3.0 if i leave default controller in routes.php file "welcome" everything is working perfectly. BUT if I change it i.e. "main" CI starts to throw 404 error main controller for first steps is the same as welcome. I just copied files. renamed, changed class name (ofcourse), and in index() loading view. any suggestions?

also I forgot to tell on wamp localhost everything is working.. but in server NOT.. :/

And one more thing... i.e. if I try go to mydomain.com/welcome - working, if I try go to mydomain.com/main - NOT. even if i change routes default controller back to welcome

My main.php file:

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

class Main extends CI_Controller {
    function index(){
        $this->load->view('welcome_message');
    }
}

my routes.php file:

<?php

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

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

回答1:


As said in the comments : Your controller's file name must start with an uppercase. In your case, Main.php. See http://codeigniter.com/userguide3/changelog.html

«changed filenaming convention (class file names now must be Ucfirst and everything else in lowercase). »




回答2:


You just need to set your default controller in application/config/routes.php like

$route['default_controller'] = $this->set_directory('front/home/').'home/index';

for Ci 3.x




回答3:


i stuck with this problem. i fixed it just replacing the path. here "site" folder is the default if you want to use CI, you have to store your all code into "site" folder. if you have not used site folder for installation it ll not work with multiple controller setting. hence you get default controller as a workable and rest of others appeared 404.

change .htacess file in your root directory.

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /

        # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
       # RewriteRule ^(.*)$ /site/index.php/$1 [L]
        RewriteRule ^(.*)$ /codeig/index.php/$1 [L]
</IfModule>

above u can see there is
RewriteRule ^(.*)$ /codeig/index.php/$1 [L] changed line for "codeig" root folder.




回答4:


Try to change the function index() to Public. If that doesnt work, then try to add in the URL: domain.com/index.php/main and see what happens. Some times u need .htaccess in the other server to remove the index.php.




回答5:


In Codeigniter 3 they are adding extra code to force the names of the files to start with an upper case letter. Here's a fix for this one.

In system/core/Router.php change the line (about 303)

if ( ! file_exists(APPPATH.'controllers/'.$this->directory.ucfirst($class).'.php'))

to

if ( ! file_exists(APPPATH.'controllers/'.$class.'.php'))

I had to remove stuff in CodeIgniter.php and Loader.php as well.



来源:https://stackoverflow.com/questions/28301742/ci-new-default-controller-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!