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

[eluser]keld[/eluser]
Hi all,

I'm a beginner with codeigniter and mvc framework.

I've started a few days ago the development of a new website and after creating a few pages, I was wondering if it's good practice to have a controller for each views, mostly to have cleaner urls.

Let's say I have pages (views) called portfolio, demo, contact ect... so in my controller i have 3 functions called function portfolio(), function demo(),... but then my urls look like mysite.com/mydefaultcontroller/portfolio/ so if I had a controller called portfolio then my urls would be mysite.com/portfolio/ ? am I correct? is it good practice, I also kept all my dbqueries into on model.

I'm using "Professional CodeIgniter" book for help but they've put everything in one controller...

Thank you for your help!
#2

[eluser]Sbioko[/eluser]
Of course, this is a very bad practice, when you have one controller per one page. You can use bit.ly for shorting urls. Oh, as I remember, I have posted in Ignited Code a library for working with Bit.ly. Grab it.
#3

[eluser]keld[/eluser]
Well I don't really want my site to have bit.ly urls, it doesn't make much sense and if this site is down one day, my site will be down too...
#4

[eluser]Sbioko[/eluser]
I guarantee you, that while twitter.com is alive, bit.ly will be alive also :-) Bit.ly service has 600 million dollars of investments. Use it :-)
#5

[eluser]keld[/eluser]
Thanks but talking about bad practice, this is one. Don't rely/depend on other websites to make yours working. It's just like using jquery with a straight link from google code like some do. If they get hacked, you're down if they're down. And I want clean urls that people can remember with my domain name in it, not some /4Tuzl2/ url to link to one of my page.

Besides this wasn't even in my question, I was asking if most people use one controller and then use an htaccess file to change their urls.
#6

[eluser]Sbioko[/eluser]
Ok. After all, I still not see why you need to create one controller per view. This is unpractically. You can give short name to your controller like "m", that means "main" controller.
#7

[eluser]jedd[/eluser]
Hi keld - don't pay too much attention to the bit.ly shareholders. Smile

And it's been at least a few months since twitter last crashed. I keep hearing rumours that any day soon, now, they'll find a way of making money (as distinct from asking VC's for more) so there's not much risk of twitter disappearing in the next few weeks, at least.

[quote author="keld" date="1262657133"]
I've started a few days ago the development of a new website and after creating a few pages, I was wondering if it's good practice to have a controller for each views, mostly to have cleaner urls.
[/quote]

I don't think so, no. Worst case (though I'd suggest you try to avoid this for now) you can always start implementing routes to make URL's look more like what you want. If you stick with a controller as being something that manages one of (take your pick) : thing, entity, resource, noun - within your grand design - then things might make a bit more sense to you while you're building and learning. I gather you are of the 'sit down and start coding, and the design will come later' crowd?



Quote:Let's say I have pages (views) called portfolio, demo, contact ect... so in my controller i have 3 functions called function portfolio(), function demo(),... but then my urls look like mysite.com/mydefaultcontroller/portfolio/ so if I had a controller called portfolio then my urls would be mysite.com/portfolio/ ? am I correct? is it good practice, I also kept all my dbqueries into on model.

Keep in mind that I don't have much detail on the size and scope and complexity of what you're doing - so take this with a grain of salt ...

I'd likely go for a set of controllers that included: portfolio , contact, and possibly demo (but it's such a vague term, and I'm not sure how you're using it here)

You probably have the option (functions or methods) to edit, add, delete portfolios and contacts - so these would be your functions within those controllers, and so your URLs would include mysite.com/portfolio/edit , mysite.com/portfolio/add , mysite.com/contact/delete - and so on.

Quote:I'm using "Professional CodeIgniter" book for help but they've put everything in one controller...

Check through the forums - I recall that of the handful of books about CI, there's been a fair bit of bad press about many of them. You may want to read / watch the various tutorials (go to [url="/wiki"]the wiki[/url] and follow the Tutorials link) to get a broader set of ideas from different coders.
#8

[eluser]cahva[/eluser]
For a small site I dont think thats bad practice, but if it grows I would suggest to use more general way to do things. This is mostly personal preference and the way you want to work. After 1,5 years with CI I still try to find the best, most superion and über way to manage my controllers and code(Im getting there) Wink

Anyway, you can use routing to get rid of the controller If you wish.
#9

[eluser]Colin Williams[/eluser]
I'm a firm believer in REST conventions. I tend to look at everything as resources and actions, which not-so-coincidentally map nicely to classes and methods, which not-so-coincidentally map nicely to Controllers and Functions.

So, typically I have one controller per resource. If it was just a small site, the resource would be a page, so I'd have one Page controller. The only necessary front-facing action would be to view the page. So your default URIs look like /page/view/id, but you can eventually make them whatever you'd like with routing (you could even have a catch-all route that points to the Page controller).
#10

[eluser]keld[/eluser]
Thanks a lot for your answers!
I guess I need to be more precise about what I'm trying to achieve.

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...
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.

The 'product' means i'm calling the function product() in my controller which is calling the function getProduct() in my model.

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.

The link to the product (from the home page for example) looks like this:
Code:
anchor('controller/product/'.$featured['id'].'/'.url_title($product['name'], 'underscore');
which create a url like mysite.com/controller/product/5/greenshirt.html
(because in config.php i did $config['url_suffix'] = ".html"; for search engine and user friendly urls).
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.

Any suggestions?




Theme © iAndrew 2016 - Forum software by © MyBB