Ik ben bezig met een Navigation class en het gebruik en laten zien in twig, nu ben ik benieuwd of dit op de goede manier is.
Navigation.class.php
<?php
/**
* @author Tom Swinkels
* @version v1.0 last edit on 31-10-2012
*/
class Navigation
{
private $_id;
private $_name;
private $_url;
public function setId( $id )
{
$this->_id = $id;
}
public function getId()
{
return $this->_id;
}
public function setName( $name )
{
$this->_name = $name;
}
public function getName()
{
return $this->_name;
}
public function setUrl( $url )
{
$this->_url = $url;
}
public function getUrl()
{
return $this->_url;
}
}
?>
NavigationMapper.class.php
<?php
/**
* @author Tom Swinkels
* @version v1.0 last edit on 31-10-2012
*/
class NavigationMapper
{
private $_db;
public function __construct( PDO $db )
{
$this->_db = $db;
}
public function select( $place, $navigationId )
{
$sql = "SELECT
id,
name,
url,
module_id
FROM
navigation
WHERE
state_id = 1
AND
navigation_id = :navigationId
AND
place = :place
ORDER BY
sort
ASC
";
$result = $this->_db->prepare( $sql );
$data = array( 'navigationId' => $navigationId,
'place' => $place );
$result->execute($data);
$list = $result->fetchAll();
if($list)
{
$navigations = array();
foreach($list as $listArray)
{
$navigation = new Navigation();
$navigation->setId($listArray['id']);
$navigation->setName($listArray['name']);
$navigation->setUrl($listArray['url']);
$navigations[] = $navigation;
}
return $navigations;
}
}
}
?>
Base.html.twig
{% if navigation.select( 'cms', '0' ) %}
{% for nav in navigation.select( 'cms', '0' ) %}
<li class="limenu"><a href="{{ nav.getUrl() }}"><span class="ico gray shadow home" ></span><b>{{ nav.getName() }}</b></a>
{% if navigation.select( 'cms', nav.getId() ) %}
<ul>
{% for SubNav in navigation.select( 'cms', nav.getId() ) %}
<li><a href="{{ SubNav.getUrl() }}">{{ SubNav.getName() }}</a></li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endfor %}
{% endif %}