【问题标题】:CI routing issueCI路由问题
【发布时间】:2014-12-23 18:58:48
【问题描述】:

我目前正在学习 CI,但遇到了一个我似乎无法解决的问题。

我在驱动器和 www(root) 文件夹中设置了我的 wamp 服务器,我已经提取了 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

【问题讨论】:

    标签: php codeigniter routing base-url


    【解决方案1】:

    在 CodeIgniter 中,所有内容都通过根目录中的主“index.php”文件运行。

    因此,您可以像这样访问您的新帖子页面;

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

    阅读 CodeIgniter 用户指南,它将回答您遇到的任何问题。

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

    【讨论】:

      【解决方案2】:

      在 CodeIgniter 中你必须使用控制器来访问他的功能

      你说“当我在地址栏输入 localhost/new_post.php 时我得到一个 Not Found”,因为你试图直接访问它的函数名,你必须像这样使用 example.com/controllername/functionname

      http://localhost/posts/new_post
      

      更多信息请查看codeignier url

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

      如果您没有使用 .htaccess 删除 index.php,那么您必须像这样使用您的网址

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

      【讨论】:

      • 您好,感谢您的回复,我明白您的意思了。现在我可以在使用 index.php 时访问这些页面,但我正在使用 "RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L]" 从我的 url 中删除 index.php。根据用户指南,我必须将这段代码放在 htaccess 中,我已经这样做了,但我仍然无法使用 http://localhost/posts/new_post 访问页面
      猜你喜欢
      • 2011-10-08
      • 2017-03-26
      • 2018-03-25
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 2016-11-09
      • 1970-01-01
      • 2011-09-16
      相关资源
      最近更新 更多