2014-10-28 82 views
1

我目前正在學習CI,我遇到了一個我無法解決的問題。CI路由問題

我在驅動器中建立了我的wamp服務器,並在www(root)文件夾中提取了codeigniter文件。 [示例] [1] [1]:http://i.stack.imgur.com/7RKqG.png

然後,我創建我的PHP文件的視圖/模型和控制器等,現在當我去設置默認路由在config/routes.php文件

到我的瀏覽器並輸入localhost我得到的post.php沒有任何發佈。

但我無法從這裏訪問任何視圖。例如我有一個new_post.php視圖,當我輸入地址欄localhost/new_post.php我得到一個「未找到

在此服務器上找不到請求的URL/new_post.php。」錯誤。

我在做什麼錯?下面我已經發布了我寫在post.php控制器中的代碼以及我擁有的文件結構/名稱的圖像。

posts.php - 控制器

<?php 

class Posts extends CI_Controller 
{ 
    function __construct() 
    { 
     parent::__construct(); 
     $this->load->model('post'); //loads the post model u created in the models folder 
    } 


    function index() //goes to this function 1st when u access the controller 
    { 
     $data['posts']=$this->post->get_posts(); // load all the data from the get_posts function in post model to the data array posts 
     $this->load->view('post_index', $data); //loads the view 

    } 


    function post($postID) 
    { 
     $data['post']=$this->post->get_post($postID); 
     $this->load->view('post', $data); 
    } 

    function new_post() 
    { 
     if($_POST) 
     { 
      $data=array(
       'title'=> $_POST['title'], 
       'post'=> $_POST['post'], 
       'active' =>1 
      ); 
      $this->post->insert_post($data); 
      redirect(base_url(). 'posts/'); 
     } 
     else 
     { 
      $this->load->view('new_post'); 
     } 


    } 


    function editpost($postID) 
    { 
     $data['success']=0; 
     if($_POST) 
     { 
      $data_post=array(
       'title'=> $_POST['title'], 
       'post'=> $_POST['post'], 
       'active' => 1 
      ); 
      $this->post->update_post($postID,$data); 
      $data['success'] =1; 
     } 
     $data['post']=$this->post->get_post($postID); 
     $this->load->view('edit_post',$data); 

    } 

    function deletepost($postID) 
    { 
     $this->post->delete_post($postID); 
     redirect(base_url(). 'posts/'); 
    } 

} 

[結構] [1] [1]:http://i.stack.imgur.com/SnsbW.png

回答

0

在笨,你必須使用控制器去他的功能訪問你說

「當我在地址欄本地主機/ new_post.php鍵入我收到了未找到」因爲你嘗試直接訪問它的函數名,你必須使用example.com/controllername/functionname這樣

http://localhost/posts/new_post 

更多信息檢查codeignier網址

https://ellislab.com/codeigniter/user-guide/general/urls.html

,如果你不使用的index.php刪除的.htaccess,那麼你必須使用你的網址這樣

http://localhost/index.php/posts/new_post 
+0

您好,感謝您的答覆,我明白你的說法。現在我可以在使用index.php的時候訪問這些頁面,但是我使用'RewriteEngine' RewriteCond $ 1!^(index \ .php | images | robots \ .txt) RewriteRule ^(。*)$ /index.php/$ 1 [L]「'從我的網址中刪除index.php。根據用戶指南,我必須在htaccess中放置這些代碼,並且我已經這樣做了,但是我仍然無法使用'http:// localhost/posts/new_post'訪問這些頁面 – 2014-10-28 13:36:03

0

CI中,一切都通過主 「的index.php」 文件運行時,在你的根目錄。

因此,您將訪問您的新帖子頁面,就像這樣;

http://localhost/index.php/posts/new_post 

閱讀CodeIgniter用戶指南,它會回答您的任何問題。

https://ellislab.com/codeigniter/user-guide/