• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SEO, routing, redirecting without redirecting.

#1
[eluser]rbnc[/eluser]
Hi everyone,

I've got a question about re-routing a request from a controller.

What I'd like to do is

Code:
class Cats extends Controller {

    function Cats()
    {
        parent::Controller();    
    }
    
    
    function view($some_id)
    {
        $this->reroute('/dogs/view/'.$some_id);
    }


}

So when I go to cats/view/123 I will get dogs/view/123 without being redirected.

I know this probably seems ridiculous but the purpose is as follows.

/cats/view/123 and similar URIs obviously isn't all that SEO friendly, so with the CMS there is field where the customer can input a 'fake' uri something like 'cats/purring/beautiful-black-furry-cat' which when visited will route to an SEO controller which looks up cats/purring/beautiful-black-furry-cat from a look up and returns /cats/view/123 which is then called.

The bit I'm stuck at is re-routing to the returned real uri '/cats/view/123'.

The problem could obviously be very easily solved by just putting my cats::view code in the method but I would like to be able to map to multiples controllers and methods. So /dogs/category/123 could be mapped to /black-labradors.

Anyway, my question really is can I reroute to a different controller and method using a string which represents the 'real' uri?

Thanks in advance Smile

#2
[eluser]mddd[/eluser]
If you really want to be able to rewrite to ANYTHING, you should look at using an 'dynamic routes' solution. In such a case you use a selection of routes from your database instead of the routes in the routes config file. This is the only way you can achieve a route like 'labradors' => 'dogs/category/123'. Because otherwise, you will always need a controller with the right name (in this case "labradors").

If you make it a bit simpler, by keeping a correct controller name at all times (like 'dogs/labradors' => 'dogs/category/123') then you can handle things in a _remap method in the controller. Something like:
Code:
class Dogs extends Controller()
{
   function __remap()
   {
      // check here if the uri parts after 'dogs' make sense.
      // for instance by looking up the 'seo name' in your database and finding the correct values from there.
      $catid = $this->mymodel->get_cat_id_from_seo_name($this->uri->segment(2);
      $this->category($catid);
   }
}

The point of this is, that you don't switch to another controller. You stay in the right controller, and do your lookups from the _remap method. From that method, you can safely fire up the right method without doing a real redirect.

#3
[eluser]rbnc[/eluser]
hmmm, using a correct controller name at all times could be a sensible solution!

#4
[eluser]mddd[/eluser]
There is a workaround to the 'correct controller name' issue.. you can make routes for all your known controllers and put a 'catchall' after that. Like so:
Code:
$route['cats(:any)'] = 'cats$1';     // 'cats' stays 'cats' and same for 'cats/species/123'
$route['dogs(:any)'] = 'dogs$1';
$route['(:any)'] = 'catchall/$1';    // 'labradors' becomes 'catchall/labradors'

But that means if you add a controller you need to add it to the routes allso. And you have to use a 'skinny controllers' way of programming if you really want to avoid redirecting. The 'catchall' controller needs to be able to handle all of the actions that could occur on all of the controllers.. because you don't know if you're going to end up working on 'cats', 'dogs' or whatever.
So I wouldn't advise doing this. Keeping it in the right controller is much easier. In there, you have the methods you need right at your disposal.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.