This new component will be built on the framework's basic structure, which already includes login, messaging (alertify), media management (JS and CSS), form helpers, menu management and so on. It will allow news articles to be added, edited and deleted in the system.
First, add the component to components.xml so that the framework has the information it needs about it:
<component>
<name>noticias</name>
<displayName>News</displayName>
<menu>true</menu>
<menus>
<menuItem>
<name>main</name>
<path>noticias/views/common/menu_main.phtml</path>
<default>true</default>
</menuItem>
</menus>
<sqlprefix>not_</sqlprefix>
<enabled>true</enabled>
</component>
The name is used to refer to it in code and as a unique identifier. The menu option controls whether it appears in the application's main menu.
The menuItem option lets us specify the menus our component will use. They can then be selected from the controller: simply set the MENU constant to the name of the menu to load.
Basic files:
| Controllers | Models | Views | Tables |
|---|---|---|---|
| index | noticia | listadonoticias | noticias |
| firmante | seccion (external) | editor | firmantes |
| firmante | listadofirmantes | ||
| menu_componente |
Let us examine the developing contents of our news component's main controller:
<?php
class indexController extends Controller{
public function __construct(){
parent::__construct();
Session::control();
}
//noticias/nueva
public function nueva(){
$firmante = $this->loadModel('firmante');
$seccion = $this->loadModelFromOtherComponent('seccion','secciones');
$this->_view->_secciones = $seccion->getSecciones();
$this->_view->_firmantes = $firmante->getFirmantes();
$this->_view->renderizar('nueva',true);
}
}
?>
The constructor checks the session. If the check succeeds, the default method is index(), which calls the listado view.
The nueva() method is more interesting: it loads authors through a model belonging to this component, and can also load models from other components, as with the section component. This demonstrates a clear relationship between components.
Here is an example of firmanteModel.php:
class firmanteModel extends Model{
public $id;
public $nombre;
public $email;
public function __construct(){
parent::__construct();
$this->cargaBD();
}
public function getFirmantes() {
try {
$firmantes = $this->_db->query("SELECT * FROM firmantes") or die(mysql_error().mysql_errno());
return $firmantes->fetchAll();
}
catch (PDOException $e) {
echo $e->getMessage();
return false;
}
}
}
The resulting view is called, for example nueva:
<form method="post" action="<?php echo BASE_URL.'noticias/nueva'; ?>">
<table>
<tr>
<td>
<?php Form::select($this->_firmantes,'id','nombre','Authors'); ?>
</td>
<td>
<?php Form::select($this->_secciones,'id','seccion','Sections'); ?>
</td>
<td>Order
<input type="text" name="orden" size="5" maxlength="9"/>
</td>
<td></td>
<td>
<?php Form::button('Save article','positive','add'); ?>
<?php Form::button('Expand article','regular','blue'); ?>
<?php Form::button('Delete article','negative','delete'); ?>
</td>
</tr>
</table>
<h2>Title</h2>
<textarea rows="1" name="texto"></textarea>
<h2>Subtitle</h2>
<textarea rows="1" name="subtitulo"></textarea>
<h2>Lead</h2>
<textarea rows="4" name="entradilla"></textarea>
<h2>Body</h2>
<textarea rows="15" name="texto"></textarea>
</form>
This illustrates another framework feature: the Form class helps render form elements linked to data sources, such as select lists, as well as elements without a data source, such as buttons, to which it simply adds images and styling.