Search code examples
phpcodeigniterroutesbase-url

CI routing issue


i am currently learning CI and i have come to an issue that i cant seem to solve.

i set up my wamp server in a drive and inside the www(root) folder i have extracted the codeigniter files. ![example][1][1]: https://i.sstatic.net/7RKqG.png

then I created my php files for view/model and controller and set the default route in the config/routes.php

so now when I go to my browser and type localhost I get the post.php displayed without anyissue.

but I am unable to access any of the views from here. for example i have a new_post.php view and when i type in the address bar localhost/new_post.php i get a "Not Found

The requested URL /new_post.php was not found on this server." error.

what am i doing wrong? below i have posted the code which i have written in the post.php controller along with a image of the file structure/names i have.

posts.php - controller

    <?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/');
    }

}

![structure][1] [1]: https://i.sstatic.net/SnsbW.png


Solution

  • In CodeIgniter you have to use controller to get access to his function

    you are saying "when i type in the address bar localhost/new_post.php i get a Not Found" because you try to direct access its function name you have to use example.com/controllername/functionname like this

    http://localhost/posts/new_post
    

    for more information check codeignier url

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

    if you not removed index.php using .htaccess then you have to use your url like this

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