Question

I am currently working on CMS for a client, and I am going to be using Codeigniter to build on top of, it is only a quick project so I am not looking for a robust solution.

To create pages, I am getting to save the page details and the pull the correct page, based on the slug matching the slug in the mysql table.

My question is however, for this to work, I have to pass this slug from the URL the controller then to the model, this means that I also have too have the controller in the URL which I do not want is it possible to remove the controller from the URL with routes?

so

/page/our-story

becomes

/our-story

Is this possible

Was it helpful?

Solution

Yes, certainly. I just recently built a Codeigniter driven CMS myself. The whole purpose of routes is to change how your urls look and function. It helps you break away from the controller/function/argument/argument paradigm and lets you choose how you want your url's to look like.

  1. Create a pages controller in your controllers directory
  2. Place a _remap function inside of it to catch all requests to the controller
  3. If you are using the latest version of CI 2.0 from Bitbucket, then in your routes.php file you can put this at the bottom of the file: $routes['404_override'] = "pages"; and then all calls to controllers that don't exist will be sent to your controller and you then can check for the presence of URL chunks. You should also make pages your default controller value as well.

See my answer for a similar question here from a few months back for example code and working code that I use in my Codeigniter CMS.

OTHER TIPS

I would recommend to do it this way.

Let's say that you have : controller "page" / Method "show"

$route['page/show/:any'] = "$1";

or method is index which I don't recommend, and if you have something like news, add the following.

$route['news/show/:any'] = "news/$1";

That's it.

Here's the code I used in a recent project to achieve this. I borrowed it from somewhere; can't remember where.

function _remap($method)
{
  $param_offset = 2;

  // Default to index
  if ( ! method_exists($this, $method))
  {
    // We need one more param
    $param_offset = 1;
    $method = 'index';
  }

  // Since all we get is $method, load up everything else in the URI
  $params = array_slice($this->uri->rsegment_array(), $param_offset);

  // Call the determined method with all params
  call_user_func_array(array($this, $method), $params);
}

Then, my index function is where you would put your page function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top