title: PHP Routing System - own MVC
body: <p>I am trying to develop my own MVC and i got to the point where a routing system is due.</p>
  <p>I have used some examples from the internet and partially it works fine. Evan so, i think something is wrong and i will detail it right bellow.</p>
  <p>My MVC has a frontend and a backend. The first question is : Do i need to load the same routing system for the frontend and the backend? Or do i need to have different routing systems? How would you choose?</p>
  <p>In the frontend, whenever i am using the main menu to navigate i can go to home or contact page and the pages are displaying corectlly - and the browser url - looks fine : localhost/home or localhost/contact (without the php extension - which is the desired behaviour.)</p>
  <p>In the backend section of my website - reaching it by using localhost/admin - of course the routing system from my front-end section has some rules for the admin, so when i am reaching the admin section the browser url is fine - showing http://localhost/admin/ and displays some login form. The Login Form uses post to send info to another page- login.php which runs the login system, and if successfull requires a page named home.php. The problem is after the successfull login - the browser url is http://localhost/admin/login.php !!! which IS NOT the desired behaviour because i am expecting in fact localhost/admin/home (without the php extension) . i have tried with a different routing system for the backend - but for some reason it's not taken in consideration and i have also tried to include the routing system after the succesfull login. I cannot find a good solution. I am surely missing something and i do not know why so please help me. This is how my routing system is  looking.</p>
  <pre><code>&lt;?php
      // Include router class
      include('Route.php');
  
  // Add base route (startpage)
      Route::add('/',function(){
          require 'pages/home.php';
      });
  
  // Simple test route that simulates static html file
      Route::add('/contact',function(){
          require 'pages/contact.php';
      });
  
  // Post route example
      Route::add('/contact-form',function(){
          echo '&lt;form method=&quot;post&quot;&gt;&lt;input type=&quot;text&quot; name=&quot;test&quot; /&gt;&lt;input type=&quot;submit&quot; value=&quot;send&quot; /&gt;&lt;/form&gt;';
      },'get');
  
  // Post route example
      Route::add('/contact-form',function(){
          echo 'Hey! The form has been sent:&lt;br/&gt;';
          print_r($_POST);
      },'post');
  
  // Accept only numbers as parameter. Other characters will result in a 404 error
      Route::add('/foo/([0-9]*)/bar',function($var1){
          echo $var1.' is a great number!';
      });
      
      Route::run('/');
  </code></pre>
  <p>My router Class:</p>
  <pre><code>class Router
      {
          /**
           * The request we're working with.
           *
           * @var string
           */
          public $request;
          
          /**
           * The $routes array will contain our URI's and callbacks.
           * @var array
           */
          public $routes = [];
          
          /**
           * For this example, the constructor will be responsible
           * for parsing the request.
           *
           * @param array $request
           */
          public function __construct(array $request)
          {
              /**
               * This is a very (VERY) simple example of parsing
               * the request. We use the $_SERVER superglobal to
               * grab the URI.
               */
              $this-&gt;request = basename($request['REQUEST_URI']);
          }
          
          /**
           * Add a route and callback to our $routes array.
           *
           * @param string   $uri
           * @param Callable $fn
           */
          public function addRoute(string $uri, \Closure $fn) : void
          {
              $this-&gt;routes[$uri] = $fn;
          }
          
          /**
           * Determine is the requested route exists in our
           * routes array.
           *
           * @param  string  $uri
           * @return boolean
           */
          public function hasRoute(string $uri) : bool
          {
              return array_key_exists($uri, $this-&gt;routes);
          }
          
          /**
           * Run the router.
           *
           * @return mixed
           */
          public function run()
          {
              if($this-&gt;hasRoute($this-&gt;request)) {
                  $this-&gt;routes[$this-&gt;request]-&gt;call($this);
              }
          }
      }
  </code></pre>
  <p>and my successfull login statement which redirect to home :</p>
  <pre><code>    
          if ($login) {
              require './pages/home.php';
          } else {
              echo 'ERROR';
          }
  </code></pre>
  <p>my htaccess from the admin section folder :</p>
  <pre><code>DirectoryIndex index.php
  
  FallbackResource /index.php
  
  # enable apache rewrite engine
  RewriteEngine on
  
  # set your rewrite base
  # Edit this in your init method too if you script lives in a subfolder
  RewriteBase /admin
  
  # Deliver the folder or file directly if it exists on the server
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  
  # Push every request to index.php
  RewriteRule ^(.*)$ index.php [QSA]
  </code></pre>