Welcome Guest, Not a member yet? Register   Sign In
Handle in controller category, subcategory,page_name
#3

(This post was last modified: 12-06-2014, 08:12 AM by scion.)

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


Messages In This Thread
RE: Handle in controller category, subcategory,page_name - by scion - 12-06-2014, 08:10 AM



Theme © iAndrew 2016 - Forum software by © MyBB