CodeIgniter: folder and subfolder based URL structure

I have a problem with the current CodeIgniter. I have a CI installation with a "Pages" controller that loads static files from / views, but only up to 1 subfolder, and I want to add another subfolder to it.

To a large extent, this does the following:

  • If the requested URL segment (e.g. site.com/url) is located in / views as a file (e.g. / views / url.php), it will load it from there, but if the URL segment (e.g. url) is actually in to the folder inside / views, it will load index.php from this folder (for example, / views / url / index.php while the URL is still on site.com/url).
  • The above applies to any other file name, not just index.php. If I have site.com/url/buy, it will download the buy.php file from the / views / url folder.

I am trying to add a new subfolder to this structure:

  • If I go to .com / url / buy / faq to download the faq.php file from / views / url / buy, etc.

The current Pages.php controller is as follows:

class Pages extends CI_Controller {

public function view($page = 'index', $sub_page = 'index') {

    if ($page == 'index') {
        $path = $page . '.php';
    } else {
        $path = $page . '/' . $sub_page . '.php';
    }       

    if (!file_exists('application/views/pages/' . $path)) {
        if (!file_exists('application/views/pages/' . $page . '.php')) {
            show_404();
        } else {
            $path = $page . '.php';
        }
    }

    // $this->output->cache(60);
    // $this->output->enable_profiler(TRUE);

    $data['title'] = ucfirst($page);
    $data['url'] = lcfirst($page);

    $this->load->view('pages/modules/header', $data);
    $this->load->view('pages/'. $path, $data);
    $this->load->view('pages/modules/footer', $data);

}
}

Do you have any suggestions?

Thanks in advance!

+3
source share
3 answers

First, make sure you have the correct routing. In config/routes.php:

$route['default_controller'] = "pages";

You must write the codes in the controller's constuct method, otherwise you will need to add the method name in the url. In Pages.php:

class Pages extends CI_Controller {

     public function __construct() {
         parent::__construct();
         $this->view(); 
     }

     private function view() {

        $url_string = $this->uri->uri_string(); //get all parameters form url

        if(!file_exist(APPPATH . 'views/pages'. $url_string. '/index.php') {
             if (!file_exists(APPPATH . 'views/pages/' . $url_string . '.php')) {
                  show_404();
             } else {
                  $path = 'pages/' . $url_string . '.php';
             }
        }
        else {
           $path = 'pages/' . $url_string . '/index.php';   
       }
     }

    //here codes continue ...
}
+1
source

Use the URI class and translate the segments into directories. Something like this:

  $i = $this->uri->total_segments();
  $path = APPPATH."views/pages";
  for($j=2;$j<$i;$j++){
     $path = $path . "/" . $this->uri->segment($j);
  }
  if (!file_exists($path . "/index.php")) {
        if (!file_exists($path . ".php")) {
            show_404();
        } else {
            $path = $path . '.php';
        }
    }else{
            $path = $path . '/index.php';
    }
0

.com/url/buy/faq, faq.php /views/url/buy ..

, . :

/routes.php

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

/pages.php

class Pages extends CI_Controller {

function __construct() {

    parent::__construct();
}

function view($page) {

    if(!file_exist(APPPATH . "views/pages/{$page}.php") {

        $this->load->view("pages/{$page}");
    }
    else {
        show_404;
    }
}
0

All Articles