Welcome Guest, Not a member yet? Register   Sign In
Quick _remap tutorial || code evaluation
#1

[eluser]meigwilym[/eluser]
Hi,

I used a _remap function for the first time yesterday, and I thought I'd post what I did so others can learn. Saying that, I'm sure it's not perfect so any feedback is also welcome.

I'm building a website for a TV production company. A section was needed to showcase their productions. These productions fall into categories: factual, drama, events, kids and co-productions.

I wanted a url structure like this (to show one production):
Code:
example.com/productions/factual/the-production-name
To show all in a category:
Code:
example.com/productions/factual/
And default, to show all that are current:
Code:
example.com/productions/
My first approach was to create a 'productions' controller, with a method for each category. But, the code was the same for each category, which meant code repetition, and I'd have to change code 5 times, when I made a change. There had to be a better way.

I tried an approach using the index method, taking the category and production name as arguments, but this resulted in this url structure:
Code:
example.com/productions/index/factual/this-prod-name
So I created a 'genre' method, which took the category name and production name as arguments.
Code:
function genre($category, $prodname = ''){
   // code to get db info and print out to a view
}
I then created a _remap function. The _remap function will always be called first in your controller and can remap the url segments:
Code:
# the segment of the url after the controller name is automatically passed as the argument
    function _remap($method){  
    
        if($method == 'current' ||
           $method == 'factual' ||
           $method == 'kids' ||
           $method == 'drama' ||
           $method == 'events' ||
           $method == 'co')
        {
# I use segment(4) here as I'm using the URI language class, which adds an extra segment before the controller, so usually segment(3) would be OK
            $this->genre($method, $this->uri->segment(4));
        }else{
            $this->index();
        }
        
    }

    function index(){
    
        redirect('productions/current');
        
    }
I set a simple redirect on the index method so that the current productions are set as default.

This works great, giving me the desired url structure but with no code repetition. It's also straightforward to add extra categories.

What does anyone else think?

Regards,

Mei
#2

[eluser]xwero[/eluser]
The routing alternative :
Code:
$route['(productions)/(current|factual|kids|drama|events|co)'] = '$1/genre/$2'
#3

[eluser]meigwilym[/eluser]
Would this still keep the url structure?

Mei
#4

[eluser]xwero[/eluser]
It works with the controller you have
Code:
class Productions extends Controller
{
   function index(){
    
        redirect('productions/current');
        
    }

   function genre()
   {
      // code here
   }
}
The improved version, handles arguments too now
Code:
$route['(productions)/(current|factual|kids|drama|events|co)(.*)'] = '$1/genre/$2$3';
#5

[eluser]meigwilym[/eluser]
D'oh!

Well, I'm glad I posted it. Thanks for your help.

Mei
#6

[eluser]xwero[/eluser]
I think _remap should be used when there is no routing solution possible, because the method is routing on controller level and so diffuses the routing to the controllers instead of keeping it in one single file.
#7

[eluser]meigwilym[/eluser]
I'm using the URI Language Class, so these routes are already setup
Code:
$route['(\w{2})/(.*)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];
How can I integrate your solution with these rules?

Thanks,

Mei
#8

[eluser]xwero[/eluser]
just move it one segment Smile
Code:
$route['(\w{2})/(productions)/(current|factual|kids|drama|events|co)(.*)'] = '$2/genre/$3$4';
$route['(\w{2})/(.*)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];
the route for the productions has to come before the catch-all route
#9

[eluser]meigwilym[/eluser]
Thanks, it work great.

Mei
#10

[eluser]Boyz26[/eluser]
Code:
$route['(\w{2})/(.*)'] = '$2';
$route['(\w{2})'] = $route['default_controller'];
Can someone explain to me what this means? Or point me to a place where i can read about it?

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB