1

What's a good way to pass a list of items 2-3 items to a method in my controller? I was thinking of just using the URL.... like so:

   http://myserver/myapp/mycontroller/mymethod/parm1/parm2/listitem1/listitem2/listitem3

Is there a better way to do this? This data is not coming from a form, but rather from a database query and I'm building a hyperlink with it.

I guess the only part that bothers me is that I won't know in advance how many items I have when i'm parsing this url.

Its possible that I'll get none, or all 3 or some value in between. So the method that then has to parse this url will just keep looping until uri->segment() returns false, indicating that it's hit the first empty uri segment. Any suggestions would be appreciated.

Thanks

EDIT 1:

Just in case it wasn't clear, my model is getting the data from the database and will also build the list. The question is really about parsing an undetermined number of uri segments. Just wondering if there's a better way to do this. Thanks!

EDIT 2

Here's some more information to help you understand my MVC app. I don't think my issue is the way I've organized my code as far as who is doing what.. But just in case it helps...

I have methodA in my model that queries database and passes back to my controller listitem1, listitem2 and listitem2. The controller then builds a string that represents a URL like:

  http://myserver/myapp/mycontroller/methodB/parm1/parm2/listitem1/listitem2/listitem3

Then the view display a hyperlink using the url above. When the user clicks on this hyperlink, it calls methodB. In methodB, I since I don't know the number of items, I will just loop through all segments until I hit my first false.

As far as why I need to do this / what I'm doing... here's some background info: I'm query a database for a list of ports on a switch that are considered trunks - ones that should not be modified. this is what method A does.

methodB run a command against a switch and it returns a bunch of data back. the view that displays the data from methodB will allow the end user to make further changes to the switch. before I display the data from methodB, i want to filter out the list of ports I got from methodA so they cannot be tampered with.

Hope this helps.

Edit 3

I need both methodA and methodB because they serve two different purposes. methodA displays summary data about ports from my database. Think of methodA as a function that shows documentation about the switch. The view for methodA in turn, provides "live" links to communicate with the actual switch - this is where methodB comes in. methodB is triggered by one of those live links and it goes and gets a list of ports - similar to methodA - except that it represents what actual, and it doesn't include user defined information about the port. I guess I can have methodB communicate with my database and filter its data before it displays, but if i want to treat these two functions as separate APIs... aka - one set of functions get data out of the database, the other set is a tool to communicate with switches... - then i don't think i want one talking directly to the other. I would like the GUI to tie them together. In fact, i have created two separate models and controllers for what I'll call the database interface, and then the switch interface. So far, i think the forms idea is the most elegant solution. Thanks everyone, for reading.

5
  • Does that list of 2-3 items come through http from the view (client), or what?
    – Esben Tind
    Sep 25, 2012 at 20:29
  • @Esben Tind - it comes from the model. I think I mentioned that it comes from a database query.
    – dot
    Sep 25, 2012 at 20:32
  • With what you've given us, there is no better way to do it - without knowing more specifics about why you need so many segments and/or what they're for, it's difficult to offer alternatives or specific advice other than "redesign everything"
    – orourkek
    Sep 25, 2012 at 20:42
  • @orourkek - i've updated my post. Check out Edit 2 - at the bottom
    – dot
    Sep 25, 2012 at 20:49
  • 1
    It might be easier to turn the hyperlink into a form instead and use _POST to send the required data to a function at your controller, so you do not have to deal with the uri at all. You could also use an ajax request to do this at an onclick event. Sep 25, 2012 at 20:52

3 Answers 3

1

place number of listitems as parametr 3

../mymethod/parm1/parm2/numberofitems/listitem1/listitem2/listitem3 

and put 1, 2, or 3 as needed. In case when 0 put nothing - null, however make sure that controller would know what to do if null happend - do not expect items.

2
  • I just added a comment to my post about how to parse the url. any thoughts?
    – dot
    Sep 25, 2012 at 20:35
  • I don't think adding more parameters to the already complex URI is the right direction to go...
    – orourkek
    Sep 25, 2012 at 20:44
0

If the data is coming from a query it should be within a model in CodeIgniter if you wish for your application to truly MVC compliant. This might mean a restructuring of your application, which may be difficult but it would really benefit you in the future to create a model for all your database queries.

You can read up on codeigniter models here: http://codeigniter.com/user_guide/general/models.html

And you can read up on the database class here: http://codeigniter.com/user_guide/database/index.html

I really suggest you do this.

If your data is already coming from a model you can call it by including the model:

$this->load->model('model_name');
$response = $this->model_name->model_function(parameters);

Edit: This would also solve the issue of an unknown number of list items as you can simply parse the response returned from the model function instead of trying to figure out a uri hack.

2
  • You should be calling the model function from your controller then instead of passing it parameters, this is how models are used in codeigniter. Sep 25, 2012 at 20:39
  • While I agree with the notion of SOC, The phrase "Truly MVC Compliant" goes completely out the window when you start restricting the model to being just a class...
    – orourkek
    Sep 25, 2012 at 20:50
0

After reading all of the other answers + edits over, that's definitely not the way you want to do it.

Unless I'm misunderstanding your comments, here's the issue: The list of ports is domain data stored on your server. So why then, are you going to pull that data out, send it to the presentation layer, and show it to the user who will send it right back to the application? Skip the middle-man and have "MethodB" get that data.

Your "MethodB" should get this information itself before processing what it needs to do - domain data stays in the domain layer, and the view never sees any of that information directly (the user would see a link directly to "MethodB")

Alternatively, you could do this all in one query if your DB schema is conducive to such a join.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.