CodeIgniter Forums

Full Version: Rewrite url vs Controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]stingray9[/eluser]
Hi,

i got a little question, let me explain my problem.
I made it possible for a costumer on my application to create three pages. They can give the page a title and content.
Every page is saved in the database under page1, page2 and page3.

Now, my controller is extra, with functions: page1, page2 and page3.
So the url they see is basic_url/extra/page1

But it would be more nice, if the url was the title they have set up their-self (saved in the database) and not that ugly extra/page1.
The problem is referring to my correct controller by then.

I hope i explained it well and someone have a possible solution for me.

El Forum

[eluser]theprodigy[/eluser]
Take a look at CodeIgniter's routing

El Forum

[eluser]stingray9[/eluser]
[quote author="theprodigy" date="1306555012"]Take a look at CodeIgniter's routing[/quote]
Thanks for answering.
So you believe it's possible to write some php code in that route config file?
As i don't know the titles, they are saved in the database.

El Forum

[eluser]osci[/eluser]
You define only route there and then you can remap in your controller
User Guide - Remap

for example
Code:
public function _remap($method, $params = array())
   {
      switch ($method)
      {
         case 'admin':  // Your crud maybe?
         case 'admin_create':
         case 'admin_update':
         case 'admin_delete':
         case 'index': //Your page list maybe, so from admin to index are your reserved words - not to be used in titles
            if (method_exists($this, $method))
            {
               return call_user_func_array(array($this, $method), $params);
            }
            break;
         default:
            $slug = $method;
            $query = $this->your_model->record_exists($slug);
            if ($query)
            {  //this will lead to the desired function, $query->page being from db the function
               return call_user_func_array(array($this, $query->page), $params);
               break;
            }
      }
            
      show_404();
   }


but you should probably make page1, page2, page3 one function ie page since the serve the same purpose (display a title and a content)
and the you would have
Code:
return call_user_func_array(array($this, 'page'), $params);
and manage all the logic from there

btw the title should be translated to safe url (no spaces or disallowed characters), maybe saving it at db while updating record title so you can query against that.

El Forum

[eluser]stingray9[/eluser]
Thanks mate, _remap() did the trick for me. Your post really helped me out!

El Forum

[eluser]osci[/eluser]
I kind of love _remap() Wink