0
votes

I have the following code in my routes.php in a codeigniter project.

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

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

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

The following url takes me to the about page correctly (which is located here: views/pages/about.php)

http://localhost:1234/ciblog/index.php/pages/view/about

Also http://localhost:1234/ciblog/index.php/news takes me to the news page correctly which is located here: views/news/index.php

I would like, however, http://localhost:1234/ciblog/index.php/about to take me to the about page, and instead a 404 error is returned. To clarify, I don't want to have to type in the "pages/view" in the url, to get to the about page. And the same for the login page. I don't want to have to type in login/view ....to get to the login.php page.

I thought that this line code, arranged for this to be routed such that pages/view is bypassed (that is the views/pages directory) so that it goes straight to the about.php page, but clearly, I am missing some logic.

The controller for the Pages.php is as so:

<?php
    class Pages extends CI_Controller{
        public function view($page = 'home')
{
        if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
        {
                // Whoops, we don't have a page for that!
                show_404();
        }

        $data['title'] = ucfirst($page); // Capitalize the first letter

        $this->load->view('templates/header', $data);
        $this->load->view('pages/'.$page, $data);
        $this->load->view('templates/footer', $data);
}
    }

Can someone point out what I've done wrong, but also, as an accepted answer, explain how exactly the routing works in simple terms. If I wanted to simply create a new page, and create a route (url) that allowed me to access that page from the localhost/page (i.e directly) what is the best method to do so?

For reference, a screenshot of the directory structure can be seen on the left.

enter image description here

1

1 Answers

0
votes

You have two conflicting routes (:any).

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

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

During the bootstrap CI will look com $route array trying to find a match for the current request. If a match is found, it'll then use the corresponding value as controller/method.


Unless you planned to do this way, you don't need a "view" method on your controller.

You could do something like this:

class Pages extends CI_Controller{
  public function index()
  {
    $data['title'] = "Home";
    $this->load->view('templates/header', $data);
    $this->load->view('pages/home', $data);
    $this->load->view('templates/footer', $data);
  }
  public function about()
  {
    $data['title'] = "About";
    $this->load->view('templates/header', $data);
    $this->load->view('pages/about', $data);
    $this->load->view('templates/footer', $data);
  }
}

So your route could be:

$route['about'] = 'pages/about';
$route['home'] = 'pages/home';
$route['default_controller'] = 'pages/home';

It is more verbose, but is easy to understand.