how to use common function in helper and component In Cakephp
Asked Answered
S

5

8

We are familiar with Components and Helpers in CakePHP.

I have an ABC Component and XYZ helper and both have same function (around 2000 lines total 4000 lines). there is any way to use same function in Controller and .CTP files. it's not good to use same function 2 time.

any method so i can use Component/Helper function in Helper/Component without rewrite ?

same method into component and helper >>

Component

class DATAComponent extends Component {

public $components = array('Session', 'THmail');

public function UsaStateList()
{ /********/}

Helper

class LabHelper extends AppHelper {
    public function UsaStateList()
    { /********/}
}
Sweeps answered 17/6, 2014 at 10:27 Comment(0)
C
8

Well, you will have to rewrite something, it's not going to solve itself.

CakePHP is still PHP, so you can easily apply common patterns to keeps things DRY. The most straight forward way would probably be to move the shared functionality into an utility class that your component and helper can both use internally while leaving their public API unchanged.

Some of CakePHPs helpers do something similar, check for example the time helper.

Traits might be an option too, depending on the amount of functionality being shared and how much it is tied to the use in a component/helper.

Cater answered 17/6, 2014 at 16:21 Comment(1)
I've noticed upvotes coming in for my rather old answer and spotted the bounty. It might not be obvious, but this answer has been added long before the bounty, ie it's not the detailed, canonical one that @trante is looking for (not sure what exactly s/he is looking for though).Cater
M
2

I wanted to use a component inside a helper. So i came out with the following solution.

<?php

App::uses('AppHelper', 'View/Helper');
App::import('Component', 'MyComponent');

class MyHelperHelper extends AppHelper {

    private $MyComponent = null;

    public function __construct(View $View, $settings = array()) {
        $collection = new ComponentCollection();
        $this->MyComponent = new MyComponentComponent($collection);
        parent::__construct($View, $settings);
    }

    public function myCustomFunction() {
        return $this->MyComponent->myCustomFunction();
    }

}
Milicent answered 2/11, 2014 at 12:58 Comment(1)
If you're using components in helpers, you're doing Cake MVC wrong. Components are for Controllers, and Helpers are for Views.Vampirism
A
2

I would go with a class in Lib folder. It is pretty clear how to deal with a library class that has only static methods. So, I omit this case. A workable solution for instantiating the class could be to create it in the controller and put it into the registry. If you really need to access CakeRequest, CakeResponse and CakeSession (take a note that CakeSession has many static methods, so you often do not need an instance of that class) from that class you can set it from the controller:

$MyLib = new MyLib();
$MyLib->setRequest($this->request); // optional
ClassRegistry::addObject('MyLib', $MyLib);

Then from the view or any other place you would just get an instance of MyLib from the registry:

ClassRegistry::getObject('MyLib');

or simply

$list = ClassRegistry::getObject('MyLib')->UsaStateList();

So, your class would be something like this:

// /Lib/MyLib.php
class MyLib {
    public function setRequest(CakeRequest request) {...}
    public function UsaStateList() {...}
}
Agnosticism answered 3/11, 2014 at 20:15 Comment(0)
V
2

Simple Answer

For common functions across your application, add a Lib or Utility class.

app/Lib/MyClass.php

class MyClass {

    public static function usaStateList() {
        // ...
    }
}

Then add this at the top of whichever file you want access to the function:

App::uses('MyClass', 'Lib');

And call your function wherever you like:

$myClass = new MyClass();
$states = $myClass::usaStateList();

Better Answer

It looks to me like your specific problem is that you want to be able to get a list of US states in both your controller and your view. The best way to do this is to have a database table of US states.

  1. Create a table in your database called us_states.

    Example SQL:

    CREATE TABLE `us_states` (
        `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        `name` VARCHAR(20) NOT NULL,
        `abbreviation` CHAR(2) NOT NULL
    ) ENGINE = MYISAM 
    
  2. Insert all the states as data in that table. You can find SQL dumps on the Internet which already have that for you (e.g. Like this one).

  3. Create a UsState model in CakePHP.

    /**
     * Model for US States
     *
     * @package app.Model
     */
    class UsState extends AppModel {
    
        /**
         * Default sort order
         *
         * @var string|array
         */
        public $order = 'UsState.name';
    }
    

What you can then do is access the states from your controller just by using the model.

    /**
     * Your Controller
     *
     * @package app.Controller
     */
    class YourController extends AppController {

        public function index() {

            // Get a list of US states
            $this->loadModel('UsState');
            $states = $this->UsState->find('all');
        }

    }

And if you want to access those states in your View, then you should pass along that data as you normally would any other variables.

I imagine you would want to do that so you can have a select menu of US states, perhaps.

        public function index() {

            // Get a list of US states
            $this->loadModel('UsState');
            $states = $this->UsState->find('all');

            // Make the states into an array we can use for a select menu
            $stateOptions = array();
            foreach ($states as $state) {
                $stateOptions[$state['id']] = $state['name'];
            }

            // Send the options to the View
            $this->set(compact('stateOptions'));
        }

And in your view you can display a select menu for that like this:

    echo $this->Form->select('us_state_id', $stateOptions);
Vampirism answered 5/11, 2014 at 15:49 Comment(0)
A
1

ok you want to use a single function in component and helper, I can think of 3 things you can do:

  1. Calling a function from the component in your helper.
  2. Calling a function from a helper in your component.
  3. Create a model or use an existing model where you put the function, and you can use this function in your component or your help.

Option numbre 3: In your helper and component, you need import a model, assuming that your function be in a model "StateList":

how you call the funcion of the model "StateList" in your helper, so:

App::import("Model", "StateList");  
$model = new StateList();  
$model->UsaStateList();

how you call the funcion of the model "StateList" in your component, so:

$model = ClassRegistry::init('StateList');
$model->UsaStateList();

ans if you want use the same function in a controller just:

$this->loadModel('StateList');
$this->StateList->UsaStateList();
Accelerando answered 3/11, 2014 at 13:5 Comment(1)
$model = new StateList(); is bad. $model = ClassRegistry::init('StateList'); is good from any place.Agnosticism

© 2022 - 2024 — McMap. All rights reserved.