2017-01-28 40 views
3

我是codeigniter的初學者,我試圖呈現視圖文件,但Codeigniter沒有獲取視圖文件。Codeigniter找不到視圖文件

我的控制器溫控功能

public function index() 
{ 
    $data['news'] = $this->news_model->getNews(); 
    $data['title'] = 'Todas as noticias'; 

    $this->load->view('template/header', $data); 
    $this->load->view('news/index', $data); 
    $this->load->view('template/footer', $data); 
} 

我的路由

$route['news/(:any)'] = 'news/view/$1'; 
$route['default_controller'] = 'news'; 
$route['404_override'] = ''; 
$route['translate_uri_dashes'] = FALSE; 

任何人都可以找出什麼是錯了,我已經在這裏做了什麼?

+0

你救了裏面查看/新聞文件夾的index.php文件 –

+0

是被這個消息文件夾 –

+0

對不起我的錯,它的內部工作權現在 –

回答

1

在您的routes.php文件中刪除$route['news/(:any)'] = 'news/view/$1';。 CodeIgniter具有自動路由系統。你不需要指定它。這就像example.com/class/function/ID

對於您的情況,您可以通過鍵入http://localhost/yourprojectname(因爲它是您的默認控制器)或鍵入http://localhost/yourprojectname/index.php/news來訪問新聞控制器的索引功能。

請記住,您也可以使用.htaccess文件從您的url中刪除index.php。 在.htaccess文件複製和粘貼以下行並將其保存在您的項目目錄

<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA] </IfModule>

1

伊夫tryed,但仍然沒有找到!

控制器是確定的,但認爲負載是問題,因爲這表明請求的頁面沒有找到

1

試試這個

class news extends CI_Controller() { 
public function __construct() { 
    parent::__construct(); 
    $data['news'] = $this->news_model->getNews(); 
    $data['title'] = 'Todas as noticias'; 
} 

public function index() { 
    $this->load->view('template/header', $data); 
    $this->load->view('news/index', $data); 
    $this->load->view('template/footer', $data); 
} 
} 

添加BASE_URL中的配置文件:

$config['base_url'] = 'http://localhost/folder_name/'; 

要刪除index.php文件,請在根文件夾中添加文件.htaccess,並將以下代碼複製到該文件中:

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css) 
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

從你的routes.php文件刪除代碼:

$route['news/(:any)'] = 'news/view/$1';