GWER
GWER

Reputation: 133

Functions - Model, Controller

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

 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 the Controller have the function that checks if the url is right and redirects if I'm 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, then wanted to know if I can create these two functions in the Controller (together or separately) and how it could do that. Is it possible?

Note: I'm using CodeIgniter

Upvotes: 2

Views: 51

Answers (1)

The Alpha
The Alpha

Reputation: 146191

You may move this function into your own function helper file and also you can auto load the file, for example, create a helper file in application/helpers folder as functions_helper.php and declare/define this function in this file then add the following line in in your application/config/autoload.php file:

$autoload['helper'] = array('functions_helper');

Now you can use the function just like any other PHP function. For example:

url_format_category($data['category'], $lang_domin);

This is the simplest way to reuse common helper functions (Using a helper).

Upvotes: 1

Related Questions