• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Handle in controller category, subcategory,page_name

#1
Smile 
Hi guys! I have a big question about handling stuff like category subcategory item_uri and so on.

First of all, I saw most the same topic in old EllisLab CI forum https://ellislab.com/forums/viewthread/170274/#862868, but that solution i don't like, i did same things many times and

everytime it was horrable big controller and many repeated code.

So task is:

I have on a site category, subcategory. Obviously they have something like post (or product, whatever) and of course i using pagination segment.

So url will be next few

category/post_uri
category/pagination_segment
category/subcategory/pagination_segment
category/subcategory/post_uri

And now i need to know guys how you are handle this all !??? What is the best approach to deal with this all?

I am very need for your help. Every advice will be appreciable! I just did this all time that i think very bad and wrong and now i need to know how would be normal people doing this.

More important will be the approach to this with codeigniter controllers (and mb routing)

Thanks and sorry for my bad english Sad
Reply

#2
(12-05-2014, 07:20 PM)scion Wrote: So task is:

I have on a site category, subcategory. Obviously they have something like post (or product, whatever) and of course i using pagination segment.

So url will be next few

category/post_uri
category/pagination_segment
category/subcategory/pagination_segment
category/subcategory/post_uri

And now i need to know guys how you are handle this all !??? What is the best approach to deal with this all?


I have attempted to handle the issue stated by the forum link several different ways:
http://www.testsite.com/category/sub-category/page-name
http://www.testsite.com/category/page-name
http://www.testsite.com/page-name

Updating the routing, hooks, etc...I came to the conclusion, it was never clean and ultimately "broke" codeigniter's way of doing things.

For a Minimum.. make sure the first Segment is a controller

Then in that controller use the _remap function, by placing the below in your controller, it will check to see if the second segment is an actual method inside the controller, if not you can make it run a function all the time passing parameters.

Remap information: https://ellislab.com/codeigniter/user-gu...llers.html

PHP Code:
public function _remap($method$params = array() )
    {
         if(
$method <> "index")
 
               {
             if (
method_exists($this$method))
            {
                return 
call_user_func_array(array($this$method), array($params));
            }    
        }
        
$this->index($method,$params);
    } 



I would come up with a generic format you like to handle controller/method_or_variables and stick with it. so the above would be
http://www.testsite.com/category/sub-category/page-name
http://www.testsite.com/category/page-name
http://www.testsite.com/pages/page-name
Reply

#3
many thanks for reply! But mb this is not exactly what i am asking about.

So how i am doing that now

I have a controller name is "rubrics"

Class Rubrics and method index. All variants of url path have the same start "rubrics"

method

public function index(arg1 = null, arg2 = null, arg3 = 0)

and here i check how many args i have

Code:
$uri = 'rubrics';
$limit = 5;
$where = array();
//view for topic list
$view_tpl = 'topics/topic_list';
       switch ($args = func_num_args())
       {
        case 3:
                   //full uri ( category/subcategory - here topic or pagination will be)
                   $uri .= '/'.$arg1.'/'.$arg2;
                   
                   //search by category and subcategory uri
                   $where['c.uri'] = $arg1;
                   $where['s.uri'] = $arg2;
                   
                   //category - subcategory - pagination
                   if(is_numeric($arg3))
                   {
                       
                       $topics = $this->mod_topics->get_where($where, $limit, $arg3);
                       
                   }
                   //category - subcategory - topic
                   else if($id = abs((int)$arg3))
                   {
                       $topic = $this->mod_topics->get_by_id($id);
                       if( ! $topic) show_404();
                       //view for single topic
                       $view_tpl =  'topics/topic';
                   }
               
        break;
       
        case 2:
                       
         
                   $where['r.uri'] = $arg1;
                   
                   //category - pagination
                   if(is_numeric($arg2))
                   {
                        //full uri ( category/pagination)
                       $uri .= '/'.$arg1;
                       $topics   = $this->mod_topics->get_where($where, $limit, $arg2);
                   
                   }
                   //category - topic
                   else if($id = abs((int)$arg2))
                   {
                       $topic = $this->mod_topics->get_by_id($id);

                       if( ! $topic) show_404();
                       //full uri (category/topic)
                       $uri .= '/'.$arg1;
                      //view for single topic
                       $view_tpl =  'topics/topic';
                   }
                   //category - subcategory
                   else
                   {
                       //full uri (category/subcategory)
                       $uri .= '/'.$arg1.'/'.$arg2;
                       
                       $where['s.uri'] = $arg2;
                       $topics   = $this->mod_topics->get_where($where, $limit, $arg3);
                       
                   }
        break;
           
           
           case 1:
           
                   //pagination
                   if(is_numeric($arg1))
                   {
                     
                       $topics = $this->mod_topics->get_where($where, $limit, $arg1);
                     
                   
                   }
                   //topic
                   else if($id = abs((int)$arg1))
                   {
                       
                       $topic = $this->mod_topics->get_by_id($id);
                       if( ! $topic) show_404();                                                           //rand(0,3)
                     
                       $view_tpl =  'topics/topic';
                   }
                   //category
                   else
                   {
                       $uri .= '/'.$arg1;
                       
                       $where['r.uri'] = $arg1;
                       $topics = $this->mod_topics->get_where($where, $limit, $arg1);
                     
                   }
           break;
           
           case 0:
                   //all topics
                   
                   $topics = $this->mod_topics->get_where(array(), $limit, 0);
                   
        break;
       
        default : show_404('Wrong argument quantity');
           
                   
       }


So now you can see that all handle in one method controller and there i have many repeated code and it very huge!

This is normal? Or

maybe exists more readable and easy way to do same thing? Or maybe i missing something in your answer?

Thanks!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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