用户
 找回密码
 入住 CI 中国社区
搜索
查看: 10981|回复: 31
收起左侧

[已解决] 为什么我的ci一直访问的是默认控制器???

[复制链接]
发表于 2016-10-9 14:44:51 | 显示全部楼层 |阅读模式
新手,刚学习ci,好多不懂。一上来路由就把我搞得好郁闷!!!ci默认控制器是welcome,如:http://yong_hu.avazutracking.net/play_ci/index.php/welcome/index;我自定义了一个控制器User,在浏览器其中输入http://yong_hu.avazutracking.net/play_ci/index.php/user/index;结果一直还是访问的是默认的welcome控制器。百度了下,有人说你改一下config目录下的 routes.php中的$route['default_controller'] = 'welcome';这个是可以。但是不能我每次调试其他的控制器都去改一下吧
 楼主| 发表于 2016-10-11 22:40:25 | 显示全部楼层
Hex 发表于 2016-10-11 10:16
nginx 参考 https://www.nginx.com/resources/wiki/start/topics/recipes/codeigniter/ 设置一下


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    client_max_body_size 20M; #修改此处上传大小为自己实际需要的大小

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  www.yhuweb.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   D:/wnmp/www;
            index  index.html index.php;
#            if (-f $request_filename) {
#               expires max;
#               break;
#           }
#           if (!-e $request_filename) {
#               rewrite ^/(.*)$ /index.php/$1 last;
#           }
        }
               
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   D:/wnmp/www;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
                location /phpmyadmin {
                        alias D:/wnmp/www/phpmyadmin;
                        index index.php;
                }
               
        location ~ \.php$ {
            root           D:/wnmp/www;
            fastcgi_pass   127.0.0.1:9090;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  D:/wnmp/www$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
*********************************************************
这是我的nginx全部配置。
$config['base_url']        = 'http://'.$_SERVER['HTTP_HOST'].'/play_ci';
ci命名为play_ci,放在根目录下;
$config['uri_protocol']        = 'AUTO';
AUTO和REQUEST_UR设置I都试过了;
$route['default_controller'] = 'cismarty';routes.php下默认控制器的配置


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

class Cismarty extends CI_Controller {
  function __construct()
  {
   parent::__construct();
  }
  function index()
  {
     $this->test();
  }

  public function test()
  {
     $this->tpl->assign("content","恭喜你smarty安装成功!");
     $this->tpl->display('smarty_view.html');
  }
}
这是我自定义的控制器代码;
自己在浏览器中访问时
http://www.yhuweb.com/play_ci/index.php/cismarty/test
结果是404 Not Found
发表于 2016-10-12 10:45:33 | 显示全部楼层
本帖最后由 alben_001 于 2016-10-12 11:16 编辑

主要是因为缺少server环境变量引起的

    [HTTP_HOST] => work.mading.cn
    [SERVER_NAME] => work.mading.cn
    [DOCUMENT_ROOT] => D:/xampp/htdocs/workspace/
    [CONTEXT_DOCUMENT_ROOT] => D:/xampp/htdocs/workspace/
    [SCRIPT_FILENAME] => D:/xampp/htdocs/workspace/zuowen/CodeIgniter-3.1.0/index.php
    [REQUEST_URI] => /zuowen/CodeIgniter-3.1.0/
    [SCRIPT_NAME] => /zuowen/CodeIgniter-3.1.0/index.php
    [PHP_SELF] => /zuowen/CodeIgniter-3.1.0/index.php

看看你的 server 和这几个参数有什么区别,然后给自己的项目手动添加server,查看是否可以解决问题,如果可以再调整nginx配置
 楼主| 发表于 2016-10-12 09:56:29 | 显示全部楼层
燃雲 发表于 2016-10-12 04:58
除此以外,看看User有无index()。

LNMP还要看目录及文件的系统及Nginx访问权等等。

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

class User extends CI_Controller {

   public function __construct()  
   {  
      parent::__construct();  
   }  

        public function index()
        {
                echo "hello ,everyone!";
        }
}
******************
有的,文件目录我直接设成777了
 楼主| 发表于 2016-10-9 14:46:54 | 显示全部楼层
希望大神救我!!!!都快折腾死我了
发表于 2016-10-9 16:18:25 | 显示全部楼层
你的 CI 版本是什么?
 楼主| 发表于 2016-10-9 22:57:21 | 显示全部楼层
最新的3.1.0的
发表于 2016-10-10 10:53:23 | 显示全部楼层
path_info有开启吗
发表于 2016-10-10 12:11:01 | 显示全部楼层

可以尝试下更改 config.php 里的 URI 协议,不要使用 AUTO。
 楼主| 发表于 2016-10-10 19:19:33 | 显示全部楼层
原来是$config['uri_protocol']        = 'REQUEST_URI';,我改成AUTO,两个试过了都没用,还可以改成什么吗
发表于 2016-10-10 22:05:30 | 显示全部楼层
萧然 发表于 2016-10-10 19:19
原来是$config['uri_protocol']        = 'REQUEST_URI';,我改成AUTO,两个试过了都没用,还可以改成什么吗 ...

都没用。。。。

你这个问题太奇怪了,没有你的环境我也说不好了,很抱歉,建议你用 wamp 吧,直接配好的环境。
 楼主| 发表于 2016-10-10 22:32:25 | 显示全部楼层
我的是nginx1.8
 楼主| 发表于 2016-10-10 22:34:11 | 显示全部楼层
我用的是wnmp,公司的是lnmp

本版积分规则