Max
Max

Reputation: 841

CodeIgniter: URL structure based on folders and sub-folders

I'm having a bit of an issue with the current CodeIgniter. I have a CI install with a "Pages" controller that loads static files from /views, but it only goes up to 1 sub-folder and I'm looking to add another sub-folder to it.

Pretty much it does the following:

  1. If the URL segment requested (e.g. site.com/url) is in /views as a file (e.g. /views/url.php) it would load it from there, but if the URL segment (e.g. url) is actually a folder inside /views it would load index.php from that folder (e.g. /views/url/index.php while the URL is still site.com/url).
  2. The above goes with any other name of files and not only index.php. If I have site.com/url/buy, it would load the buy.php file from the /views/url folder.

What I'm trying to achieve is to add a new sub-folder to this structure:

  1. If I go on site.com/url/buy/faq to load the faq.php file from /views/url/buy and so on..

The current Pages.php controller is the following:

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 guys have any suggestions?

Thanks in advance!

Upvotes: 3

Views: 3886

Answers (3)

rgin
rgin

Reputation: 2311

If I go on site.com/url/buy/faq to load the faq.php file from /views/url/buy and so on..

I didn't have time to examine your code, but here's what you can try. Use routes:

config/routes.php

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

controllers/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;
    }
}

Upvotes: 0

safarov
safarov

Reputation: 7804

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

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

You must write codes in constuct method of controller, otherwise you would need to add method name in 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 ...
}

Upvotes: 1

Femi
Femi

Reputation: 64700

Use the URI class and translate the segments into directories. Something roughly 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';
    }

Upvotes: 0

Related Questions