2014-10-28 129 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/