url redirection

1

Well, it's been a few days since I've encountered some problems in a task. What I need to do is: Verify that the url is in the right format, and if it is not, redirect to the correct url. In the other urls I did I had no problems, but this one I'm having. Anyway, I will try to explain the error in the best way. I have this function in my Model to list the learnings:

 public function get_learning_category_list() {
$categorias = $this->db->select('t1.id, t1.title, t1.metadata, t1.meta_title, t1.description, t1.meta_description, t1.meta_spam, t1.url, t1.alt_img')
        ->from('learning_category t1')
        ->join('learning_rel_category t4', 't1.id = t4.category_id', 'left')
        ->join('learning t2', 't4.learning_id = t2.id', 'left')
        ->join('learning_rel_language t3', 't2.id = t3.learning_id', 'left')
        ->where('t3.language', $this->language)
        ->group_by('t1.id')
        ->get()->result_array();
    foreach($categorias as $c){
        $total = $this->db->select('count(learning_rel_language.learning_id) as total', false)
        ->from('learning')
        ->join('learning_rel_language', 'learning.id = learning_rel_language.learning_id', 'inner')
        ->join('learning_rel_category', 'learning.id = learning_rel_category.learning_id', 'inner')
        ->where('learning_rel_category.category_id', $c['id'])
        ->where('learning_rel_language.language', $this->language)
        ->group_by('learning_rel_language.language')->get()->first_row('array');

        $metadata = json_decode($c['metadata']);
        $tt = $c['title'];
        $url = $c['url'];
        if($this->language != 'pt_br'){
            $tt = $metadata->{$this->language};
            $tt_pt_br = $c['title'];
        }

        $return[] = array('url' => $url, 'title'=>$tt, 'id'=>$c['id'], 'total'=>$total['total'], 'title_pt_br'=>$tt_pt_br);

    }

It will return me an Array:

Array ( [0] => Array ( [url] => [title] => Cursos sobre como gerar leads qualificados [id] => 2 [total] => 6 [title_pt_br] => ) [1] => Array ( [url] => [title] => Cursos de Social Media Marketing [id] => 3 [total] => 2 [title_pt_br] => )...

And still in the Model I have this function that defines the format of the url:

 public function url_format_category($category, $lang_domin) { //Monta o formato da url
    if (lang('abbr') == 'en_US')
        $lang_domin = 'en/';
    else if (lang('abbr') == 'es_US')
        $lang_domin = 'es/';

    $categorias = $this->learn->get_learning_category_list();
    foreach($categorias as $cat){
        $cat = (object) $cat;
        if($cat->title != '') {
            $return = strtolower(url_title($cat->title)).'-cmdo-'.$cat->id;
        }else{
            $return = 'cursos-de-marketing-digital-online-'.$cat->id;
        }   
    return $return;   
    }
}

So far so good, he sets up the url with the title and id of Learning. In my Controller I have the function that verifies if the url is correct and redirects it:

    //redirect to real list_route
    if($this->uri->uri_string != $this->learn->url_format_category($category, $lang_domin)) {
        redirect($this->learn->url_format_category(),'location','301');
        exit;
        die($this->learn->url_format_category($category, $lang_domin));

    }

It is in redirection that the error occurs, it is done according to url_format_category , but it always redirects to the first result of the Array (Courses on how to generate qualified leads, that is, always the same title) independent of the page that it is. If it has not been clear, I can edit my question. Thanks.

    
asked by anonymous 06.04.2015 / 21:44

1 answer

2

Your error is here in this code:

//redirect to real list_route
    if($this->uri->uri_string != $this->learn->url_format_category($category, $lang_domin)) {
        redirect($this->learn->url_format_category(),'location','301');
        exit;
        die($this->learn->url_format_category($category, $lang_domin));

    }

Notice and review the changes I've made:

//redirect to real list_route
    if($this->uri->uri_string != $this->learn->url_format_category($category, $lang_domin)) {
        redirect($this->learn->url_format_category(),'location','301');

        die($this->learn->url_format_category($category, $lang_domin));

    }

The exit; command was preventing the script from moving forward, in addition you have the url_format_category function getting parameters that are never used, so what are they there then? url_format_category($category, $lang_domin)

So your function should look like this:

public function url_format_category() { //Monta o formato da url
    if (lang('abbr') == 'en_US')
        $lang_domin = 'en/';
    else if (lang('abbr') == 'es_US')
        $lang_domin = 'es/';

    $categorias = $this->learn->get_learning_category_list();
    foreach($categorias as $cat){
        $cat = (object) $cat;
        if($cat->title != '') {
            $return = strtolower(url_title($cat->title)).'-cmdo-'.$cat->id;
        } else {
            $return = 'cursos-de-marketing-digital-online-'.$cat->id;
        }   
    return $return;   
    }
}

And your model like this:

public function get_learning_category_list() {
    $categorias = $this->db->select('t1.id, t1.title, t1.metadata, t1.meta_title, t1.description, t1.meta_description, t1.meta_spam, t1.url, t1.alt_img')
        ->from('learning_category t1')
        ->join('learning_rel_category t4', 't1.id = t4.category_id', 'left')
        ->join('learning t2', 't4.learning_id = t2.id', 'left')
        ->join('learning_rel_language t3', 't2.id = t3.learning_id', 'left')
        ->where('t3.language', $this->language)
        ->group_by('t1.id')
        ->get()->result_array();

    $return =  array();

    foreach($categorias as $c){
        $total = $this->db->select('count(learning_rel_language.learning_id) as total', false)
            ->from('learning')
            ->join('learning_rel_language', 'learning.id = learning_rel_language.learning_id', 'inner')
            ->join('learning_rel_category', 'learning.id = learning_rel_category.learning_id', 'inner')
            ->where('learning_rel_category.category_id', $c['id'])
            ->where('learning_rel_language.language', $this->language)
            ->group_by('learning_rel_language.language')->get()->first_row('array');

        $metadata = json_decode($c['metadata']);
        $tt = $c['title'];
        $url = $c['url'];
        if($this->language != 'pt_br'){
            $tt = $metadata->{$this->language};
            $tt_pt_br = $c['title'];
        }

        $return[] = array('url' => $url, 'title'=>$tt, 'id'=>$c['id'], 'total'=>$total['total'], 'title_pt_br'=>$tt_pt_br);

    }

    return $return;
}

An important detail I noticed in your programming style: you force your model to return queries as array get()->result_array(); and get()->first_row('array'); (the default of CodIgniter is to return as objects), but when you are going to manipulate this data you force a cast reverting to working with objects $cat = (object) $cat; ???? This has no logic apart from being a waste of server resources. If it's going to be used as an object, do not force the model out as an array, keep the CodeIgniter pattern.

    
25.04.2015 / 06:21