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.

4

3 回答 3

1

将列表项的数量作为参数 3

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

并根据需要放置 1、2 或 3。如果 0 什么都不放 - null,但是请确保控制器知道如果 null 发生了该怎么办 - 不要期望项目。

于 2012-09-25T20:34:16.273 回答
0

如果数据来自查询,如果您希望您的应用程序真正兼容 MVC,那么它应该位于 CodeIgniter 中的模型中。这可能意味着对您的应用程序进行重组,这可能很困难,但将来为您的所有数据库查询创建模型会真正使您受益。

您可以在此处阅读 codeigniter 模型:http: //codeigniter.com/user_guide/general/models.html

您可以在此处阅读数据库类:http: //codeigniter.com/user_guide/database/index.html

我真的建议你这样做。

如果您的数据已经来自模型,您可以通过包含模型来调用它:

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

编辑:这也将解决列表项数量未知的问题,因为您可以简单地解析从模型函数返回的响应,而不是试图找出 uri hack。

于 2012-09-25T20:34:54.413 回答
0

在阅读完所有其他答案+编辑之后,这绝对不是您想要的方式。

除非我误解了您的评论,否则问题出在:端口列表是存储在您服务器上的域数据。那么,为什么要提取数据,将其发送到表示层,然后将其显示给将其直接发送回应用程序的用户?跳过中间人并让“MethodB”获取该数据。

您的“MethodB”应该在处理它需要做的事情之前自己获取这些信息 - 域数据保留在域层中,并且视图永远不会直接看到任何这些信息(用户会看到直接到“MethodB”的链接)

或者,如果您的数据库模式有利于这种连接,您可以在一个查询中完成所有操作。

于 2012-09-25T21:05:30.183 回答