Welcome Guest, Not a member yet? Register   Sign In
Naming Controllers for Keyword Rich URLs - Good or Bad practice?
#4

[eluser]Michael Wales[/eluser]
The URL of domain.com/controller/method is just the default way - a way for you to access your website before you learn about routing.

We'll use your example here, from a developer standpoint it makes much more since to have one controller named pages. Let's assume your content is in a database:

Code:
class Pages extends Controller {

  function Pages() {
    parent::Controller();
  }

  function view($slug) {
    // Code here to go get the data from the database, based on slug, and then push it to a view
  }

}

Alright, so, as we're browsing through our directories we know that class deals with Pages. Just plain old content, pulled out of the database - nothing real special. But - we want SEO friendly URLs right? We want people to be able to go to domain.com/about-us and domain.com/contact-us, right?

This is where routing comes in to play:
Code:
$route['([a-z-]+)'] = 'pages/view/$1';

This says: anything that matches domain.com/??? where ??? is a string containing lowercase letters and dashes, send it to the pages controller, view method, passing the string in ??? to that method.

You're getting the best of both world here - your code makes sense to a developer and your URLs are SEO friendly.

Let's take your search for example: First of all, ditch the underscores. Matt Cutts and virtually every SEO out there agrees, dashes are preferred. Secondly, I would format the URL as such: domain.com/search/pencils/bic - it's pretty easy to tell from that URL that you are search pencils for the word "bic".

So, we have our store and we're selling all kinds of products, well we need a products class right? A Controller to handle these objects.

Code:
class Products extends Controller {

  function Products() {
    parent::Controller();
  }

}

Alright, now we know we need to be able to search these products:
Code:
class Products extends Controller {

  function Products() {
    parent::Controller();
  }

  function search() {
  }

}

For SEO friendliness, we want to include a product type in the URL:
Code:
class Products extends Controller {

  function Products() {
    parent::Controller();
  }

  function search($product_type = NULL) {
  }

}

And lastly, we're going to need a place for our search term:
Code:
class Products extends Controller {

  function Products() {
    parent::Controller();
  }

  function search($product_type = NULL, $query = NULL) {
  }

}

We could now access this page at domain.com/products/search/pencils/bic - which isnt' that bad, but we want to ditch the products controller from this URL. Routing!

Code:
$route['search/([a-z-]+)/(:any)'] = 'products/search/$1/$2';

Hope this helps - you can check out more of the magic of routing in the User Guide.


Messages In This Thread
Naming Controllers for Keyword Rich URLs - Good or Bad practice? - by El Forum - 02-03-2010, 11:55 AM



Theme © iAndrew 2016 - Forum software by © MyBB