{301} - 3.6.2 Creating Custom Components
Suppose our online application needs to perform a complex mathematical operation in many different parts of the application. We could create a component to house this shared logic for use in many different controllers.
The first step is to create a new component file and class. Create the file in /app/controllers/components/math.php. The basic structure for the component would look something like this.
<?php
class MathComponent extends Object {
function doComplexOperation($amount1, $amount2) {
return $amount1 + $amount2;
}
}
?>
<?phpclass MathComponent extends Object {function doComplexOperation($amount1, $amount2) {return $amount1 + $amount2;}}?>
Once our component is finished, we can use it in the application’s controllers by placing the components name in the controller’s $components arrays.
//Make the new component available at $this->Math var $components = (‘Math’, ‘Session’);
//Make the new component available at $this->Mathvar $components = (‘Math’, ‘Session’);
{3819} - 3.6.2 Creating Custom Components
Suppose our online application needs to perform a complex mathematical operation in many different parts of the application. We could create a component to house this shared logic for use in many different controllers.
The first step is to create a new component file and class. Create the file in /app/controllers/components/math.php. The basic structure for the component would look something like this.
<?php
class MathComponent extends Object {
function doComplexOperation($amount1, $amount2) {
return $amount1 + $amount2;
}
}
?>
<?phpclass MathComponent extends Object {function doComplexOperation($amount1, $amount2) {return $amount1 + $amount2;}}?>
Differences
| Lines: 15-27 | Lines: 15-18 | ||
| } | } | ||
| ?> | ?> | ||
| </pre> | </pre> | ||
| - | <p> | ||
| - | Once our component is finished, we can use it in the application’s controllers by placing the components name in the controller’s $components arrays. | ||
| - | </p> | ||
| - | <pre> | ||
| - | //Make the new component available at $this->Math | ||
| - | var $components = (‘Math’, ‘Session’); | ||
| - | </pre> | ||
