Welcome Guest, Not a member yet? Register   Sign In
Dynamic URL routing... needing some ideas
#1

[eluser]Unknown[/eluser]
Hey guys, how you all doing ?

First, let me tell you what i have, then comes the question:
This project is a kind of a search engine for companies throughout a whole country, each one having their own categories and subcategories.
As i want it to be as SEO Friendly as possible, here are the possible structures for the URLs:

Code:
example.com/state/ //state is always there, selected manually, before
//anything else.  Ex: example.com/tx for Texas state.

example.com/state/city/ //all the companies in this city
example.com/state/category/ //all companies under the category, on that state;
example.com/state/subcategory/ //well, you got it...

example.com/state/city/category/
example.com/state/city/subcategory/

pretty simple, huh ? Tongue
But then again, these URLs are for filtering purposes - In case i find the company which i'm looking for and go for its page, then the url becomes:
Code:
example.com/company-url

Been getting stuck a few days now, trying to figure out the better way to do something like that, thought on using _remap, but things started getting crazy with all those if statements and all... thought on using SESSION variables, but crawlers don't have sessions, so that would be a problem too, I think.

If you have any ideas on how to do this, please, share with me on a comment below. Where should I put all those filters ? Model, Controller ? Should i stick with _remap on the main controller ?

I'll be very grateful for any help you can provide me. Thanks for reading this, have a nice day !
#2

[eluser]PhilTem[/eluser]
Please refer to this little "tutorial" I wrote

https://gist.github.com/4157418

on how to create a bootstrap controller that will take handle every request made to your application. It should give you a good starting point so you can complete your task Wink

Using above code you may want to inject some database calls that check whether the first argument is either a state, otherwise if it's a company, otherwise a controller, ... then

Of course you could also put all that content in the __construct() method of your MY_Controller and run respective functions once a matching state/company whatsoever is found Wink
#3

[eluser]TunaMaxx[/eluser]
Will you only ever be using states to filter? Or will you eventually have provinces, territories and regions from other parts of the world?

Also, can you alter the URL structure slightly? You can make thing immensely easier if the URL could be formed like:

Code:
example.com/show/state/city/

Then you just build a 'show' controller like this:

Code:
class Show extends CI_Controller {

public function index($state = NULL, $city = NULL, $category = NULL)
{
  if ( $state ) {
   // Do something with the $state
  }

  if ( $city ) {
   // Do something with the $city
  }

  if ( $category ) {
   // Do something with the $category
  }
  // Build the rest of your logic here.
}
}

Then you just build a 'companyurl' controller like this:

Code:
class Companyurl extends CI_Controller {

public function index($url = NULL)
{
  if ( $url ) {
   // Do something with the $url
  } else {
   // Redirect or error or show search box or whatever...
  }
}
}

Then in your routes.php, all you have to do is:

Code:
$route['default_controller']  = "home";
$route['404_override']         = '';

// Finally, route everything else...
$route['(:any)']                     = "companyurl/index/$1";
#4

[eluser]TunaMaxx[/eluser]
After a little more thinking about this, you could make things more flexible with a 'show' controller like this:

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Show extends CI_Controller {

public $state       = '';
public $city        = '';
public $category    = '';
public $subcategory = '';

public function __construct()
{
  parent::__construct();
  
  $this->state       = $this->uri->segment(2);
  $this->city        = $this->uri->segment(3);
  $this->category    = $this->uri->segment(4);
  $this->subcategory = $this->uri->segment(5);
}

public function state()
{
  echo 'Show info for ' . $this->state;
}

public function city()
{
  echo 'Show info for ' . $this->city . ', ' . $this->state;  
}

public function category()
{
  echo 'Show info for ' . $this->category . ' in ' . $this->city . ', ' . $this->state;
}

public function subcategory()
{
  echo 'Show info for ' . $this->subcategory . ' ' . $this->category . ' in ' . $this->city . ', ' . $this->state;
}
}

...and change routes.php to this:

Code:
$route['default_controller']  = "home";
$route['404_override'] = '';

// Most specific to least specific
$route['show/(:any)/(:any)/(:any)/(:any)']  = 'show/subcategory';
$route['show/(:any)/(:any)/(:any)'] = 'show/category';
$route['show/(:any)/(:any)'] = 'show/city';
$route['show/(:any)'] = 'show/state';

// Finally, route everything else...
$route['(:any)'] = "companyurl/index/$1";

So you could do the following:

http://example.com/show/wa
Quote:Show info for wa

http://example.com/show/wa/seattle
Quote:Show info for seattle, wa

http://example.com/show/wa/seattle/parks
Quote:Show info for parks in seattle, wa

http://example.com/show/wa/seattle/parks/water
Quote:Show info for water parks in seattle, wa

http://example.com
Quote:whatever is set in 'home' controller

http://example.com/company-name
Quote:whatever is set in 'companyurl' controller and 'index' method

[/quote]
The key thing to remember though, is this relies on adding a little bit to your URL scheme. It can be done without it, but the complexity goes through the roof.

Also, because of the final routes.php entry, you will have to manually add any controllers that you'd like to route to. The CI automagic routing will no longer work as expected.
#5

[eluser]Unknown[/eluser]
Thank you @TunaMaxx and @Philtem for your answers. After them I put some more thought to it and ended up using a _remap function over a controller, and ended up with something pretty similar to that Show Controller from TunaMaxx working here.

Now i've got something else here to workaround:

Following that URL idea from the first post, what is really NEEDED in the url in most pages are those two parameters:
Code:
http://example.com/state/city

When a user selects them, they'll be saved in a session variable for quick use and also saved in a cookie, so that the site remember what the user selected for when he/she comes back.

But they won't be present over the whole site. URL for static pages will remain simple and so will companies', for SEO sake. (ie. http://example.com/static-page, http://example.com/company-name)

To make it clear: What I need now is some function, helper, something that changes its value following the url above for some links (ie. http://example.com/wa/seattle/parks, or http://example.com/nv/las-vegas/parks) but not all of them.

Thought about creating a helper that would use this Session Variable, something similar to site_url() function from CI, autoload it and make it handle these URL changes.
But then again, if i recall it right, Google and other bots don't use Session, so that will not help me out =(

Could use a hook to change $CI->config->base_url value, but that would not be nice to all .css, images and .js links

Any ideas on this one ? I think i might be complicating this more than necessary, so any thoughts you can share will be really appreciated again heheh
thanks for helping me out, have a nice day ! Smile




Theme © iAndrew 2016 - Forum software by © MyBB