php - Functions - Model, Controller

353

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

107

Answer

Solution:

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 inapplication/helpers folder asfunctions_helper.php and declare/define this function in this file then add the following line in in yourapplication/config/autoload.php file:

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

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

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

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

People are also looking for solutions to the problem: php - get an inline editable dropdown in Datatable with editor

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.