Functions - Model, Controller

Asked

Viewed 157 times

2

In my model I have this function that defines the url format :

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

    if (is_array($category))  
        $category = (object) $category; 

    if($category->title != '') {
        $return = strtolower(url_title($category->title)).'-cmdo-'.$category->id;
    }else{
        $return = 'cursos-de-marketing-digital-online-'.$category->id;
    }   

    return $return;   
}

And in Controller I have the function that checks if the url is right and redirects if it is wrong :

if($this->uri->uri_string != $this->learn->url_format_category($data['category'], $lang_domin)) {
 redirect($this->learn->url_format_category($data['category'], $lang_domin),'location','301');
exit;
 }

But now I have to do the same thing with a url that does not contain a Model, so I was wondering if I can create these two functions in Controller (together or separately) and how I could do that. It is possible?

Note: I am using Codeigniter.

  • Model validating URL? I don’t understand what’s happening in this code...

  • @Papacharlie In the model you are only setting the default url format. Validation is done in the Controller

  • But URL validation is done by router. You are referring to formatting the links to the view composition?

  • That’s right, for the view

  • 1

    Well, then I would recommend removing from the model and turning it into a helperpfor direct use by the view. I don’t see this method as part of a model, I see it as part of the scope of a model.

  • I could do a merge of these functions in Controller?

  • if checks if the Url is in the same format defined by the model, and if it is not it redirects to the correct url.

Show 3 more comments

1 answer

1

I believe that the simplest solution in your case is to create a Model and maintain the standard you have made in others. But as was said in the comments by @Papacharlie you could create a Helper and use it to do this. This link has an example of how to create a Helper, and you can adapt it according to your need.

  • I find it interesting not only to put the link.

Browser other questions tagged

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