Welcome Guest, Not a member yet? Register   Sign In
question about controllers, urls and good practice
#11

[eluser]keld[/eluser]
I think the only way to get rid of the id in the url would be to have an extra column in my database, for each product, i have the name of it all in lowercase with underscores and instead of looking for an id, i just look for the name of it by removing the .html from the url. Not sure I'm clear here...
#12

[eluser]jedd[/eluser]
[quote author="keld" date="1262665009"]
I guess I need to be more precise about what I'm trying to achieve.
[/quote]

[url="/wiki/How_to_ask_a_good_question/"]Do you think?[/url]

Quote:I'm gonna take the example of a merchant site (so i don't reveal my exact project Wink and the functionality is pretty similar) so I have home page that lists some products, a product page with the details of the product, etc...

If this were me, I'd have a Product controller.

Quote:The '5' is the product ID that I'm using to identify which product you've clicked on to view it so in my controller i'm doing this:
Code:
$data['product']=$this->Controller->getProduct($this->uri->segment(3));
to get the id and display the correct one.

Or better yet:
Code:
function  show ($product = FALSE)  {
      . . .



Quote:Right now my urls to the product page look like mysite.com/controller/product/5/ and i would like to have it look like mysite/product/greenshirt.html for example but I still need to have access to this '5' which is the id of the product in the db.

Is greenshirt from a unique column? If so, this is easy, else less so.


Quote:The problem with this is that if somebody types mysite.com/controller/product/123/greenshirt.html they dont get the greenshirt page anymore but whatever id 123 is in the db so that's why i wan to hide this ID from the url.

I think you're trying to solve several conflicting problems, some of which may be over-estimated in their import.

If you don't want or need the ID in the URL, don't put it in. Or in your code, if the ID and the description are both present, then ignore the ID. Or keep the ID in session data. Or ... (etc)
#13

[eluser]wowdezign[/eluser]
I use a similar structure for my sites and I don't have a Controller for every View (or vice versa).

I try to have a Controller for each AREA of the site. Like News, Main (or Home), Articles, and Products.

I also try to make the ID the last segment in the URL. I never use an extension of any kind so my URLs look like:

http://www.somewebapp.com/products/cloth...ks/1436532

I have also used a controller for things like Details pages:

http://www.somewebapp.com/details/1436532

CI is so versatile, you can do the same thing a number of ways.

I personally like being able to load views into other views based on a condition or a flag that I set in a controller. Since I use my Views in this way, it doesn't lend itself to my using a View for each Controller.
#14

[eluser]keld[/eluser]
Ok I guess i'll do like you recommend, seems fair to me. I just thought that having the html extension with exact product name in the url was more search engine and user friendly
#15

[eluser]wowdezign[/eluser]
keld, I didn't mean to imply that the way you were doing it was incorrect or wrong. I just was trying to show that each developer has their own way of doing things. I thought if I shared how I do it, it might give you some ideas.

I know for me, it took quite awhile for me to form my own way of structuring things.

If you are not working with other programmers, you don't have to worry so much about "right" or "wrong" because you'll be the only one editing the code.
#16

[eluser]keld[/eluser]
Oh no worries, I'll try different ways but after looking at some other sites using mvc framework, most of them has a url like yours with category name/id so that should work.

Another question, is it better to use anchor() or just a regular <a> tag, I find using anchor is pretty heavy in the code and it's almost impossible to make an image wrapped in an <a> tag without using some custom helpers. I hope in future version of CI, we will get some functions to do this automatically.
#17

[eluser]Random dude[/eluser]
I was in exactly the sames position a week ago.

Class Site extends Controller {

news() // site/news/

blog() //site/blog

people() //site/people
}

then I found http://ellislab.com/codeigniter/user-gui...uting.html

I looked through all the replies and routing was mentioned but it seems it wasn't emphasised.
Master routing! It's such a good thing in CI, I was so happy - I was going to have 100 functions just for urls!
#18

[eluser]keld[/eluser]
Thanks, I'll def have a look at it!
#19

[eluser]wowdezign[/eluser]
Yes, routing is definitely a good tool. I haven't taken the time to get to know it well though.

I probably am missing time-saving opportunities.

What if you are pulling page meta data (like titles, descriptions, and keywords) from the database? Does it lend itself to that structure?




Theme © iAndrew 2016 - Forum software by © MyBB