Welcome Guest, Not a member yet? Register   Sign In
Controller and URL/Navigation
#1

[eluser]mzuser[/eluser]
I'm pretty new to CI and I know that when you have a controller and a function within that controller you start to make up your URL.

So I have a url like this:
www.example.com/index.php/editor/categories/

I have a controller named editor with a function named categories. Simple and works exactly how I want it to so far.

What I'd like to know is how to go further in:
www.example.com/index.php/editor/categories/add/
www.example.com/index.php/editor/categories/edit/22/

Do I make functions within my categories function? Or is there some other trick to this? Forgive me if this is a noob question, I'm just missing something here.
#2

[eluser]mddd[/eluser]
First, you can make one level of folders inside the Controllers folder. So you could make a folder 'editor' inside the controllers folder, and put a controller in there that is called 'categories'. And then make functions for 'add', 'edit' etc.

The other way is, like you say, to find out what is asked for inside of the functions of your controller. For this purpose, the first url segment after the controller method is given as an argument to the method:
Code:
class Editor extends Controller
{
   // constructor omitted for clarity

   function categories($action)
   {
       switch ($action)
       {
           case "add":
              // do something
           break;
           case "edit":
              // do something else
           break;
           default:
              // do this if no action given
           break;
        }
    }
}
#3

[eluser]AndrewMalachel[/eluser]
it's the parameters....
your method on your editor controller should look like this...

Code:
class Editor extends Controller {
    // .....
    function categories($par1, par2 = NULL) {
        if($par1 == 'add') {
            // do your add stuff
        } elseif($par1 == 'edit') {
            // or you could call other function in the controller like this:
            $this->_do_edit($par2);
        }
    }
    /**
     * This function will be set to private (has an underscore before the function name...
     */
    function _do_edit($par) {
        // now do your editing stuff here...
    }
}

that what I do so far anyway...
There are other way also to handle with the URI, like using:
Code:
$this->uri->segment();
// or
$this->uri->rsegment();

but the above I'd wrote are just the basic...
#4

[eluser]AndrewMalachel[/eluser]
oups...
"mddd" has already answer it... :p
well, you had many option now, though...

Big Grin
#5

[eluser]mddd[/eluser]
Always good to have more options AndrewMalachel!




Theme © iAndrew 2016 - Forum software by © MyBB