Skip to main content
added 2 characters in body
Source Link
<?php

class Main
{
  public $URL;
  public $PageName;
  public function __construct($PageName)
  {
    $this->PageName = $PageName;
    $this->URL = $_SERVER['REQUEST_URI'];
  }
  public static function header() {
    echo '<!DOCTYPE html>
<html lang="en">
  <head>
    ...
  </head>';
  }

  public static function navstart() {
    echo '<body><nav><ul>';
  }

  public static function navend() {
    echo '</ul></nav>';
  }
} 


the navitem class is for making the items in the nav barbar<br>

class navItem extends Main
{

    public function __construct($PageName)
  {
    parent::__construct($PageName);
    $ActiveUrl = ($this->URL == "/".$this->PageName) ? " active " : " ";
    echo     '<li class="nav-item">
      <a class="nav-link'.$ActiveUrl.'" href="'.$this->PageName.'">'.$this->PageName.'</a></li> &nbsp;';
  }
}
<?php

class Main
{
  public $URL;
  public $PageName;
  public function __construct($PageName)
  {
    $this->PageName = $PageName;
    $this->URL = $_SERVER['REQUEST_URI'];
  }
  public static function header() {
    echo '<!DOCTYPE html>
<html lang="en">
  <head>
    ...
  </head>';
  }

  public static function navstart() {
    echo '<body><nav><ul>';
  }

  public static function navend() {
    echo '</ul></nav>';
  }
}

the navitem class is for making the items in the nav bar

class navItem extends Main
{

    public function __construct($PageName)
  {
    parent::__construct($PageName);
    $ActiveUrl = ($this->URL == "/".$this->PageName) ? " active " : " ";
    echo     '<li class="nav-item">
      <a class="nav-link'.$ActiveUrl.'" href="'.$this->PageName.'">'.$this->PageName.'</a></li> &nbsp;';
  }
}
<?php

class Main
{
  public $URL;
  public $PageName;
  public function __construct($PageName)
  {
    $this->PageName = $PageName;
    $this->URL = $_SERVER['REQUEST_URI'];
  }
  public static function header() {
    echo '<!DOCTYPE html>
<html lang="en">
  <head>
    ...
  </head>';
  }

  public static function navstart() {
    echo '<body><nav><ul>';
  }

  public static function navend() {
    echo '</ul></nav>';
  }
} 


the navitem class is for making the items in the nav bar<br>

class navItem extends Main
{

    public function __construct($PageName)
  {
    parent::__construct($PageName);
    $ActiveUrl = ($this->URL == "/".$this->PageName) ? " active " : " ";
    echo     '<li class="nav-item">
      <a class="nav-link'.$ActiveUrl.'" href="'.$this->PageName.'">'.$this->PageName.'</a></li> &nbsp;';
  }
}
Source Link

Makes a navbar for a website with object oriented php

I do this as sort of hobby and want to no if I can improve.
This is a simplified version of my code that normal works with bootstrap classes and id's.
At the bottom there is the input of the classes.
I have already a working version connected with SQL Database.
but want to take it a step further with object oriented php.

The next step for me is to create the content page underneath the navbar.
I work with a .htaccess file that redirects everything to index.php, and then use the URI to show the right content.

The main class

<?php

class Main
{
  public $URL;
  public $PageName;
  public function __construct($PageName)
  {
    $this->PageName = $PageName;
    $this->URL = $_SERVER['REQUEST_URI'];
  }
  public static function header() {
    echo '<!DOCTYPE html>
<html lang="en">
  <head>
    ...
  </head>';
  }

  public static function navstart() {
    echo '<body><nav><ul>';
  }

  public static function navend() {
    echo '</ul></nav>';
  }
}

the navitem class is for making the items in the nav bar

class navItem extends Main
{

    public function __construct($PageName)
  {
    parent::__construct($PageName);
    $ActiveUrl = ($this->URL == "/".$this->PageName) ? " active " : " ";
    echo     '<li class="nav-item">
      <a class="nav-link'.$ActiveUrl.'" href="'.$this->PageName.'">'.$this->PageName.'</a></li> &nbsp;';
  }
}

The nav dropdown class to make the dropdown in the navbar

class navDropdown extends Main
{
  public function __construct($PageName, $Item)
  {
    parent::__construct($PageName);
    $shortURL = substr($this->URL, 0, strpos($this->URL, "_"));
    $ActiveUrl = ($shortURL == "/".$this->PageName) ? " active " : " ";
    echo '
      <li class="nav-item dropdown ">
        <a class="nav-link'.$ActiveUrl.' href="#">'.$this->PageName.'</a>
        <div>';
        foreach ($Item as $Item) {
          $ActiveUrl = ($this->URL == "/".$this->PageName.'_'.$Item) ? " active " : " ";
          echo '
          <a class="dropdown-item'.$ActiveUrl.'" href="./'.$this->PageName.'_'.$Item.'" >'.$Item.'</a>';
        }
        echo '</div></li> &nbsp;';    
  }
}

the variables to make the page

main::header();
main::navstart();
$test = new navDropDown("test", array('test1' , 'test2', 'test3' , 'test4'));
$home = new navItem("home");
$about = new navItem("about");
$pages = new navDropDown("pages",array("one", "two", "three" ));
$contact = new navItem("contact");
main::navend();