Welcome Guest, Not a member yet? Register   Sign In
URL advice neeeded: Unknown number of ethod args...
#1

[eluser]mhulse[/eluser]
Hi,

I was hoping I could get some feedback from the pros... I am looking for advice on "best practice" for setting a URL.

Long story short, my app will allow users to "bookmark" their creations. Currently, the best way I can think of passing data to the "bookmark" controller/method is to do this:

Code:
/application/controller/bookmark/a1a1a1,a2a2a2,b1b1b1,b2b2b2,c1c1c1,c2c2c2...

As you can see, the method argument could get kinda long and squirrely.

On the flip side, the above seems to make the most sense in terms of handling an unknown number of possible method arguments.

The only other alternative I could think of would be to do something like this:

Code:
/application/controller/bookmark/a1a1a1,a2a2a2/b1b1b1,b2b2b2/c1c1c1,c2c2c2/...

... Hmmm, that seems kinda funky. In fact, I am not even sure if it is possible to pass an unknown number of args to a CI controller method (which would probably be unsafe practice anyway).

Just hoping the pro CI devs can help me figure out the best way to handle this situation.

Should I encode the argument before passing it to my bookmark method (and then decode via the bookmark method/view)? If so, what PHP encoding method would you use?

Any tips ya'll could provide would be greatly appreciated! Smile

Thanks so much!

Cheers,
Micky
#2

[eluser]juanvillegas[/eluser]
May i ask why do you need so much data passed through the URL? If you want to bookmark some item, just guive the id to the controller and let it look db for item details. That is the traditional approach i guess.
Id's should be encoded some way; im not sure how and what to use.
#3

[eluser]WanWizard[/eluser]
You can access the different URI segments via de methods of the URI library. See the user guide.
#4

[eluser]mhulse[/eluser]
Hi folks! Many thanks for the replies, I really appreciate the help here. Smile

@juanvillegas:

Good question! Sorry that I did not clarify.

This is a very simple/small app, and (up until now) I have not thought about/needed to use the DB to store any data. I can definitely setup a model specifically for bookmarks but would opt to pass args via the URI.

@WanWizard

Thanks for tip. I will browse the user guide.

Cheers,
Micky
#5

[eluser]mhulse[/eluser]
After having thought about it, I think I will add the bookmark to the database.

Just out of curiosity, would this be a valid code workflow:

1. User customizes page
2. User clicks form button to generate "bookmark"
3. Form submits to controller which adds the bookmark data to database
4. Controller loads new view and displays bookmark page

Would that seem like a logical setup?

Now I am wondering how I would setup the form action to point to this new page, if I have not created the info in the DB at that point? I am betting there is a way to do a redirect from the controller?

Something like this perhaps:

Code:
// Controller 1:
// Generate entry in database and create random string
// Assign var $bookmark_uri_segment random string
// Redirect to new controller/method:
redirect('/app/controller2/method/' . $bookmark_uri_segment);

Code:
// Controller 2:
// Take $bookmark_uri_segment and lookup in DB
// Generate bookmark page based on data stored in DB

Feedback?

Many thanks in advance!

Cheers,
Micky
#6

[eluser]juanvillegas[/eluser]
The form to create the bookmark should point to something like bookmark_controller/create. Then , in create method and if data is correct you create the bookmark in the database, save it and then redirect to bookmark_controller/view/recently_created_bookmark_id (which you may obtain using insert_id() ). To redirect to another method just call that method:

if ($correct){
$this->view($id);
}else{
//load view
}
#7

[eluser]mhulse[/eluser]
Cool! Many thanks for the pro help and guidance Juan! I greatly appreciate it.

I am glad I asked. I think using the DB will make for a better app. Smile

I may be back with more questions, but for now, thanks!!!

Have a nice day,
Cheers,
Micky




Theme © iAndrew 2016 - Forum software by © MyBB