Php router and controller

Dear astonecipher, JimL and benanemen,
I’ve started reading about routers and controllers. I’ve taken code from a tutorial and i’m trying to understand it. I wish to make my own router and controller without a framework dependency. I can learn more building my own. However, i need some guidance along the way. Please try to help me if you have the time and patience to do so.

i want to make a skeleton website using a router and a controller with an autoload file. Then i will take my current website and work it into this skeleton. Thus, my first question is the config of the index.php file.

index.php

<?php
 $GLOBALS["config"] = array(
  "appName" => "NatureSite",
  "domain" => "localhost",
  "path" => array(
      "app" => "app/",
      "core" => "core/",
      "index" => "index.php"),
  "defaults" => array(
      "controller" => "main",
      "method" => "index"),
  "routes" => array(),
  "database" => array(
      "host" => "localhost",
      "username" => "",
      "password" => "",
      "name" => "")
 );
 require_once $GLOBALS["config"]["path"]["core"]."autoload.php";
?>

this code was shown in a tutorial. I’ve changed only the appName to NatureSite.

so as a skeleton site, i will have the following setup:
htdocs:

index.php
[css] .css files
[js] .js files
[img] .jpg and .png images
[about] index.php
[legal] index.php
[subscribe] form.php processform.php
[nature] -> [aves] -> [brachycera] -> [arachnida] -> [fungi]  -> etc.

i was told that i do not need to build folders for each species, such as,
nature -> animalia -> vertebrata -> aves -> turdidae -> turdus -> migratorius -> index.php

so i ask how to modify the index.php file from the tutorial to work with my website.

in this code:

  "path" => array(
      "app" => "app/",
      "core" => "core/",
      "index" => "index.php"),

do i place the directory names with array names?
so based upon my skeleton: css folder needs to be added to the path array as “style” => “css/”,? is this correct? so my path array should be as follows:

  "path" => array(
      "style" => "css/",
      "script" => "js/",
      "images" => "img/",
      "about" => "about/",
      "laws" => "legal/",
      "join" => "subscribe/",
      "content" => "nature/",
      "index" => "index.php"),

is this correct? my website structure belongs in the path array, yes?

Thank you for your time.

I’ll have to circle back around to this when my head isn’t so clogged. What tutorial are you following so everyone is on the same page? And are you looking to do this with OOP or procedural style coding practices?

1 Like

Probably not. The point of this is to dynamically produce the different pages on a web site without creating folders, files, or writing out bespoke code/configuration values for each different page of content.

The paths to different pages that appear in URLs are virtual and don’t physically exist. They exist based on data stored in a database.

And, yes what is the tutorial you are using for this since we cannot provide specific answers without knowing what the code expects.

1 Like

Hello astonecipher and phdr,

I’ve been following a youtube video about building a framework:
https://www.youtube.com/watch?v=MnFRBuVKkMA

I’m not so sure that i should be using this video. I followed part 1 up to the main controller and i had several errors that i needed to repair in order for the code to work. I think that the code is outdated or designed for an older version of PHP. I am using the latest installation of xampp with the latest version of PHP.

Nicely put. I understand what you are saying. I have yet to see how this works but i understand the logic here.

Anyway, i am trying to find a simple tutorial for routing and controllers. I was hoping to find a simple skeleton that i can start adding content. I am a little slow when learning something new. I apologize in advance. I expected to find a working skeleton with instructions of how to utilize that skeleton. I am struggling to find something simple. Any recommendations?

For a generic simple starter you can download my PDO Bumpstart Database.

1 Like

Thank you for the link, benanamen. I will look at the code ans see what i can do with it. Meantime, i will continue reading about routers and controllers. I also need to relearn database programming.

Sponsor our Newsletter | Privacy Policy | Terms of Service