2017-03-09 126 views
0

我正在建立一个CodeIgniter项目,到目前为止一切进展顺利。除了一个之外,我的所有路线和控制器都能正常工作。虽然我是CodeIgniter的新手,但我已经使用PHP进行了近五年的编程,所以我有一些经验。如果有人可以看看下面的代码示例,并让我知道我做错了什么,那会很好。Codeigniter 3路由不工作

很明显,该应用程序确实识别该路由,但它一直抱怨无法找到该URI。

控制器:

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

class Profile extends CI_Controller { 
    function __construct() { 
     parent::__construct(); 

     $this->load->helper('url'); 
    } 

    public function Profile($page = 'profile-page') 
    { 
     if (! file_exists(APPPATH.'/views/'.$page.'.php')) 
     { 
      // Load error page 
      show_404(); 
     } 

     // Capitalize the first letter 
     $data['title'] = ucfirst($page); 
     $data['pageType'] = "profile-page"; 

     // Load pages 
     $this->load->view('templates/header', $data); 
     $this->load->view('templates/topbar', $data); 
     $this->load->view('profile'); 
     $this->load->view('templates/footer', $data); 
     // 
    } 
} 

接下来是我的路线:

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

// All custom routes go below this line. 

$route['contact'] = 'pages/contact'; 
$route['signup'] = 'auth/signup'; 
$route['login'] = 'auth/login'; 
$route['profile'] = 'profile/profile'; 

我已经验证了所有的视图文件是在正确的文件夹,并没有错别字。然而配置文件路线仍然失败。所有其他路径正常工作,除了配置文件路由。

我在Windows 10上用XAMMP,Mac OS X Sierra与MAMP和CentOS 7与Apache/PHP/MySQL一起尝试了这一点。

谢谢。

回答

1

的问题是你的代码进入这个条件&它无法找到该路径下的配置文件,page.php文件的文件并显示error.As曲线函数时没有正确的调用为我检查Ÿ问题

if (! file_exists(APPPATH.'/views/'.$page.'.php')) 
     { 
      echo "test";die; 
      // Load error page 
      show_404(); 
     } 
+0

虽然不是我的问题的答案,但您的问题确实指向了正确的方向。我得到它的工作原来是一个丢失的文件。 –

0

自从我写了CodeIgniter路由之后,有一段时间了,但我似乎记得方法名称区分大小写,所以具有大写'P'的Profile可能会导致问题?

我通常会去调试这个方法,但尝试重命名方法,重命名控制器;想法是你可以找出问题是在路由中,还是在代码中有一些看不见的错字或其他问题,或者它的路由本身

不知道这是否有帮助?祝你好运,让它解决了!

+1

对不起 - 对拒绝,我的意思是写路由内笨,没有试图声称我写的笨路由! – JohnoTheCoder

0

Mac OSX区分大小写,请确保控制器中的文件名与您尝试在路径中访问的情况相匹配。如果您也发布了输出,它也会有所帮助。我有一个类似的问题,我通过根据定义的路由重命名我的文件名解决了问题。请参阅我的回答 HMVC codeigniter works on local server but not on web server 这里。

+0

我在Windows 10上遇到了同样的问题,而且Microsoft不区分大小写。但我会尝试解决您的问题并让您知道。 –

0

如果您正在使用CI 3,除非你已经修改MY_Router.php

应用/核心/ MY_Router.php

这是原来的问题here

您不能使用子文件夹的默认控制器
<?php 

class MY_Router extends CI_Router { 

    protected function _set_default_controller() { 

     if (empty($this->default_controller)) { 

      show_error('Unable to determine what should be displayed. A default route has not been specified in the routing file.'); 
     } 

     // Is the method being specified? 

     if (sscanf($this->default_controller, '%[^/]/%s', $class, $method) !== 2) { 
      $method = 'index'; 
     } 

     // This is what I added, checks if the class is a directory 

     if(is_dir(APPPATH.'controllers/'.$class)) { 

      // Set the class as the directory 

      $this->set_directory($class); 

      // $method is the class 

      $class = $method; 

      // Re check for slash if method has been set 

      if (sscanf($method, '%[^/]/%s', $class, $method) !== 2) { 
       $method = 'index'; 
      } 
     } 


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

      // This will trigger 404 later 

      return; 
     } 

     $this->set_class($class); 
     $this->set_method($method); 

     // Assign routed segments, index starting from 1 

     $this->uri->rsegments = array(
      1 => $class, 
      2 => $method 
     ); 

     log_message('debug', 'No URI present. Default controller set.'); 
    } 
} 
+0

感谢您的回答。但是,除了配置文件路由之外,我的所有路由均可用这是唯一不行的路线。默认路线和列出的所有其他路线一样。 –