Url forwarding

Asked

Viewed 170 times

1

Well, it’s been a few days since I’ve encountered any problems on an assignment. What I need to do is : Check if the url is in the right format, and if it is not, redirect to the correct url. In the other urls I did not have problems, but this one I am having. Anyway, I will try to explain the mistake 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);

    }

She 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 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, it mounts the url with the title and id of Learning. In my Controller I have the function that checks 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 the redirect that the error occurs, it is done according to the url_format_category, but it always redirects to the first result of the Array (Courses on how to generate qualified leads, ie always the same title) regardless of the page it is on. If I haven’t been clear, I can edit my question. I thank you.

  • remove the function that defines the ur format from within the model. leave the model ONLY to query and return the data from the dataset. The way you’re doing, you’re undoing the whole concept of MVC. Pass all functions to the controller itself (make them private) or create a helper to do so.

1 answer

2

Your mistake 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 analyze 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 command exit; was preventing the script from going forward, besides you have the function url_format_category receiving parameters that are never used, so that are there then? url_format_category($category, $lang_domin)

Then your job should stay that way:

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 his model so:

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;
}

One important detail I noticed in your programming style: you force your model to return queries as an array get()->result_array(); and get()->first_row('array'); (the default of Codigniter is to return as objects), but when it will manipulate this data you force a cast reverting to work with objects $cat = (object) $cat; ???? This has no logic beyond being a waste of server resources. If you are going to use as an object, do not force the model output as an array, keep the Codeigniter pattern.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.