2017-03-09 229 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

感謝您的回答。但是,除了配置文件路由之外,我的所有路由均可用這是唯一不行的路線。默認路線和列出的所有其他路線一樣。 –