Welcome Guest, Not a member yet? Register   Sign In
URL Routing without using controller names
#1

(This post was last modified: 04-26-2018, 07:56 AM by CINewb.)

Hi there,

What I would like to do is create "pretty" URL's which don't feature the controller name.

So instead of:
  • website.com/category/bath-toys
  • website.com/product/rubber-duck
  • website.com/page/some-generic-page
I would like:
  • website.com/bath-toys
  • website.com/rubber-duck
  • website.com/some-generic-page
Each respective URL should load either the category, product or page controller.  My business logic already ensures that a URI cannot be used more than once.

So far I have done the following:

1) In config/routes.php I have set `$route['404_override'] = 'index';`

2) I've created a library called Item_type which inspects the requested URI and queries database tables to find out whether the requested URI is for a category, product or page.

3) I've created an `Index` controller which as per the 404_override receives any request which doesn't match a controller, calls the Item_type library to determine what the user is requesting, and then calls the relevant sub-controller from there.

I guess my question is: is there a better way of doing this?
Reply
#2

I'm curious why you would want this. What does it improve for you/anyone compared to standard routes? I'm imagining that this would negatively impact performance if the site grows to be large. This sort of reminds me of how WordPress handles permalinks. You end up with a gigantic "rewrite rules". I understand there may be SEO value to URLs crafted a specific way, but I can't see how that would be a problem if you include "product" where the page is a product, or product-category where the page is for a product category.
Reply
#3

Well I come from a WordPress background, which is kind of why I want to do this.  It's been many years since I had to prefix a URI with something, and it feels like a step backwards.

I prefer website.com/bath-toys over website.com/category/bath-toys because the latter is longer and not as nice visually, more difficult for someone to remember and potentially not as search engine friendly.
Reply
#4

(This post was last modified: 04-26-2018, 07:23 PM by skunkbad.)

(04-26-2018, 02:31 PM)CINewb Wrote: Well I come from a WordPress background, which is kind of why I want to do this.  It's been many years since I had to prefix a URI with something, and it feels like a step backwards.

I prefer website.com/bath-toys over website.com/category/bath-toys because the latter is longer and not as nice visually, more difficult for someone to remember and potentially not as search engine friendly.


The problem is that WordPress is probably the best example of how an application should not be written. The database schema is terrible, the code is terrible, and ideas like adjustable permalinks are just there because nobody can decide on what's right, AND for the sake of backwards compatibility the whole application must suffer. I've been using WordPress since 1.X. It can do a lot of things, but as a developer you can do way better than that. Normal PHP frameworks are super fast compared to WordPress. You don't even have to utilize caching to be as fast as WordPress served pages that were cached. The way normal PHP frameworks do routing is mostly all the same, and you should take advantage of that.

If it were me, and I was trying to pull off something like what you are doing, I'd store all of my rewrite rules in the database, and then use CodeIgniter to write those rules to .htaccess. This has potential to get super messy, but if you don't do it that way you'll take a performance hit. Even this would be slow once you reach a certain number of rewrite rules.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB