0

I have a function like this:

func_seo.php

<?php
function seo_title($s) {
    $c = array (' ');
    $d = array ('-','/','\\',',','.','#',':',';','\'','"','[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+');

    $s = str_replace($d, '', $s); 

    $s = strtolower(str_replace($c, '-', $s)); 
return $s;

}
?>

I want to use the function in App::Model.

I create like this, but it doesn't work:

<?php
class MyModel extends AppModel{
    var $name = 'MyModel';
    public function beforeSave(){
        $this->element('func_seo'); //Function Element View/Elements/func_seo.php
        $this->data['MyModel']['name_seo'] = seo_title($this->data['MyModel']['tutorial_name']);
        return true; 
    }
}
?>

4 Answers 4

1

This code should go into a Helper as it formats the output. This will also make sure the code can be easy reused between projects. Best would be to put it into something like an Utils plugin and share that as a git submodule between the apps.

If you want to store the changed data persistent to the DB make it a behaviour instead.

Your example code is wrong because it is a violation of the MVC pattern as you try to render an element in a model which is wrong.

Your variable naming is bad. $a + $b = $c. Hey, did you know that I meant to calculate a date by this? No. always give variables meaningful names. Check the coding conventions and follow them as well. Clean code is a good read as well.

Also pay attention to the scope keywords, don't mix var with public / protected / private. If you don't know what they mean check this page.

0

I don't know what's the best practice to use your own functions like this, but I would actually put the stuff from func_seo.php into a Behavior, so all of your models can use it like $this->seoTitle().

It also might be a design mistake to include your generic functions like this into the app.

0

You can use the function like this.

<?php

class MyModel extends AppModel {

    var $name = 'MyModel';

    public function beforeSave() {
        $this->data['MyModel']['name_seo'] = $this->seo_title($this->data['MyModel']['tutorial_name']);
        return true;
    }

    public function seo_title($s) {
        $c = array(' ');
        $d = array('-', '/', '\\', ',', '.', '#', ':', ';', '\'', '"', '[', ']', '{', '}', ')', '(', '|', '`', '~', '!', '@', '%', '$', '^', '&', '*', '=', '?', '+');

        $s = str_replace($d, '', $s);

        $s = strtolower(str_replace($c, '-', $s));
        return $s;
    }

}

?>

or you can implement this function in App controller

public function seo_title($s) {
            $c = array(' ');
            $d = array('-', '/', '\\', ',', '.', '#', ':', ';', '\'', '"', '[', ']', '{', '}', ')', '(', '|', '`', '~', '!', '@', '%', '$', '^', '&', '*', '=', '?', '+');

            $s = str_replace($d, '', $s);

            $s = strtolower(str_replace($c, '-', $s));
            return $s;
        }

and in your controller you can set like this

$this->request->data['MyModel']['name_seo'] = 
     $this->seo_title($this->request->data['MyModel']['tutorial_name']);
3
  • Thats not what he is asking for Aug 27, 2013 at 5:27
  • means, I have to create a function in each appmodel ? Can I put the function in Cake/Libs/Model/AppModel.php
    – ynz
    Aug 27, 2013 at 8:36
  • In that case you can use vendors or write a function in behaviors
    – Anil kumar
    Aug 27, 2013 at 8:55
0

This function already exist as Inflector::slug

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.