To be honest I follow a tutorial and I put here the Url Class and the Navigation Class. With .htaccess I have to make the menu to work. Right now only the index.php works. If I click on other menu item I get page not found.
I got to this point in the tutorial, and everything worked fine, but when I wrote the .htaccess I didn’t get a good result.
class Url {
public $key_page = 'page';
public $key_modules = array('panel');
public $module = 'front';
public $main = 'index';
public $cpage = 'index';
public $c = 'login';
public $a = 'index';
public $params = array();
public $paramsRaw = array();
public $stringRaw;
public function __construct() {
$this->process();
}
public function process() {
$uri = $_SERVER['REQUEST_URI'];
if(!empty($uri)) {
$uriQ = explode('?', $uri);
$uri = $uri[0];
if (count($uriQ) > 1) {
$this->stringRaw = $uriQ[1];
$uriRaw = explode('&', $uriQ[1]);
if (count($uriRaw) > 1) {
foreach($uriRaw as $key => $row) {
$this->splitRaw($row);
}
} else {
$this->splitRaw($uriRaw[0]);
}
}
$uri = Helper::clearString($uri, PAGE_EXT);
$firstChar = substr($uri, 0, 1);
if ($firstChar == '/') {
$uri = substr($uri, 1);
}
$lastChar = substr($uri, -1);
if ($lastChar == '/') {
$uri = substr($uri, 0, -1);
}
if(!empty($uri)) {
$uri = explode('/', $uri);
$first = array_shift($uri);
if (in_array($first, $this->key_modules)) {
$this->module = $first;
$first = array_shift($uri);
}
$this->main = $first;
$this->cpage = $this->main;
if ($count($uri) > 1) {
$pairs = array();
foreach($uri as $key => $value) {
$pairs[] = $value;
if(count($pairs)) {
if (!Helper::isEmpty($pairs[1])) {
if ($pairs[0] == $this->key_page) {
$this->cpage = $pairs[1];
} else if ($pairs[0] == 'c') {
$this->c = $pairs[1];
} else if ($pairs[0] == 'a') {
$this->a = $pairs[1];
}
$this->params[$pairs[0]] = $pairs[1];
}
$pairs = array();
}
}
}
}
}
}
public function splitRaw($item = null) {
if (!empty($item) && !is_array($item)) {
$itemRaw = explode('=', $item);
if (count($itemRaw) > 1 && !Helper::isEmpty($itemRaw[1])) {
$this->paramsRaw[$itemRaw[0]] = $itemRaw[1];
}
}
}
public function getRaw($param = null) {
if (!empty($param) && array_key_exists($param, $this->paramsRaw)) {
return $this->paramsRaw[$param];
}
}
public function get($param = null) {
if (!empty($param) && array_key_exists($param, $this->params)) {
return $this->params[$param];
}
}
public function href($main = null, $params = null) {
if (!empty($main)) {
$out = array($main);
if(!empty($params) && is_array(params)) {
$out[] = $value;
}
}
return '/'.implode('/', $out).PAGE_EXT;
}
public function getCurrent($exclude = null, $extension = false) {
$out = array();
if ($this->modul != 'front') {
$out[] = $this->module;
}
$out[] = $this->main;
if (!empty($this->params)) {
if (!empty($exclude)) {
$exclude = Helper::makeArray($exclude);
foreach($this->params as $key => $value) {
if (!in_array($key, $exclude)) {
$out[] = $key;
$out[] = $value;
}
}
} else {
foreach($this->params as $key => $value) {
$out[] = $key;
$out[] = $value;
}
}
}
$url = '/'.implode('/', $out);
$url .= $extension ? PAGE_EXT : null;
return $url;
}
}
class Navigation {
private $table = 'navigation';
private $table_2 = 'navigation_types';
private $table_3 = 'pages';
private $table_4 = 'pages_content';
private $table_5 = 'navigation_types_content';
private $objDb;
public $objUrl;
public $objLanguage;
public $classActive = 'active';
public function __construct($objUrl = null, $objLanguage = null) {
$this->objUrl = is_object($objUrl) ? $objUrl : new Url();
$this->objLanguage = is_object($objLanguage) ? $objLanguage : new Language();
$this->Db = new Dbase();
}
public function getOne($id = null) {
if (!empty($id)) {
$sql = "SELECT *
FROM `{$this->table}`
WHERE `id` = ?";
return $this->Db->getOne($sql, $id);
}
}
public function active($main = null, $pairs = null, $single = true) {
if (!empty($main)) {
if (empty($pairs)) {
if ($main == $this->objUrl->main) {
return !$single ? ' '.$this->classActive : ' class="'.$this->classActive.'"';
}
} else {
$exceptions = array();
foreach($pairs as $key => $value) {
$paramUrl = $this->objUrl->get($key);
if ($paramUrl != $value) {
$exceptions[] = $key;
}
}
if ($main == $this->objUrl->main && empty($exceptions)) {
return !$single ? ' '.$this->classActive : ' class="'.$this->classActive.'"';
}
}
}
}
public function getAllTypes() {
$sql = "SELECT *
FROM `{$this->table_5}`
WHERE `language` = ?
ORDER BY `navigation` ASC";
return $this->Db->getAll($sql, $this->objLanguage->language);
}
public function getRecords($case = null) {
if (!empty($case)) {
$sql = "SELECT `n`.*, `t`.`label`, `p`.`identity`, `c`.`name`
FROM `{$this->table}` `n`
JOIN `{$this->table_5}` `t`
ON `t`.`navigation` = `n`.`type`
JOIN `{$this->table_3}` `p`
ON `p`.`id` = `n`.`page`
JOIN `{$this->table_4}` `c`
ON `c`.`page` = `n`.`page`
WHERE `n`.`type` = ?
AND `c`.`language` = ?
AND `t`.`language` = ?
ORDER BY `n`.`order` ASC";
return $this->Db->getAll($sql, array($case, $this->objLanguage->language, $this->objLanguage->language));
}
}
public function get($case = null) {
if (!empty($case)) {
$list = $this->getRecords($case);
if (!empty($list)) {
$out = array();
switch($case) {
case 1:
foreach($list as $row) {
$item = '<li';
$item .= $this->active($row['identity']);
$item .= '><a href="';
$item .= $this->objUrl->href($row['identity']);
$item .= '" title="';
$item .= $row['name'];
$item .= '">';
$item .= $row['name'];
$item .= '</a></li>';
$out[] = $item;
}
$out = '<ul id="navigation">'.implode('', $out).'</ul>';
break;
case 2:
$i = 1;
foreach($list as $row) {
$item = '<li';
if ($i == count($list)) {
$item .= ' class="last';
$item .= $this->active($row['identity'], null, false);
$item .= '"';
} else {
$item .= $this->active($row['identity']);
}
$item .= '><a href="';
$item .= $this->objUrl->href($row['identity']);
$item .= '" title="';
$item .= $row['name'];
$item .= '">';
$item .= $row['name'];
$item .= '</a></li>';
$out[] = $item;
$i++;
}
$out = '<ul id="navigation-left">'.implode('', $out).'</ul>';
break;
case 3:
foreach($list as $row) {
$item = '<a href="';
$item .= $this->objUrl->href($row['identity']);
$item .= '" title="';
$item .= $row['name'];
$item .= '"';
$item .= $this->active($row['identity']);
$item .= '>';
$item .= $row['name'];
$item .= '</a>';
$out[] = $item;
}
$out = '<p>'.implode(' :: ', $out).'</p>';
break;
default:
return null;
}
return !empty($out) ? '<nav>'.$out.'</nav>' : null;
}
}
}
public function updateOrder($id = null, $order = null) {
if (!empty($id) && !empty($order)) {
$sql = "UPDATE `{$this->table}`
SET `order` = ?
WHERE `id` = ?";
return $this->Db->execute($sql, array($order, $id));
}
}
public function getLast($type = null) {
if (!empty($type)) {
$sql = "SELECT *
FROM `{$this->table}`
WHERE `type` = ?
ORDER BY `order` DESC
LIMIT 0, 1";
return $this->Db->getOne($sql, $type);
}
}
public function add($type = null, $page = null) {
if (!empty($type) && !empty($page)) {
$last = $this->getLast($type);
$order = !empty($last) ? $last['order'] + 1 : 1;
$sql = "INSERT INTO `{$this->table}`
(`type`, `page`, `order`)
VALUES (?, ?, ?)";
return $this->Db->insert($sql, array($type, $page, $order));
}
}
public function remove($id = null) {
if (!empty($id)) {
$sql = "DELETE FROM `{$this->table}`
WHERE `id` = ?";
return $this->Db->execute($sql, $id);
}
}
}