This project, showcase the process of web development, using modern PHP without Freameworks.
We use the LAMPP stack, the project can be ran with built in PHP server and Mysql server.
For Windows the project needs a Vhost
so that the routes and the URI
will allow the custom router
to handle the requests. In linux/macOS the built in PHP server will suffice.
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/PHP Demo website/public"
ServerName demo-website.test
ServerAlias *.demo-website.test
<Directory "C:/xampp/htdocs/PHP Demo website/public">
AllowOverride All
Require all granted
</Directory>
Alias /static "C:/xampp/htdocs/PHP Demo website/public/static"
<Directory "C:/xampp/htdocs/PHP Demo website/public/static">
Options -Indexes
AllowOverride None
Require all granted
</Directory>
ErrorDocument 403 /views/error.php
</VirtualHost>
if you're using Laragon you'll need to replace the path from C:/xampp/htdocs/
to C:/laragon/www/
, or you can specify your own path if you Root directory isn't placed inside the htdocs
or www
one.
Run this command in the Root directory of the website.
php -S localhost:8000 -t public/
By convention, the Static Directory, is placed inside the Public Directory. The static directory has an alias set in the VirtualHost
for easier access among different systems.
root
│
├── public
│ ├── index.php
│ ├── static
│ │ ├── style
│ │ ├── scripts
│ │ └── img
│ └── .htaccess
│
├── src
│ ├── controllers
│ │ ├── home.php
│ │ └── ... (other controllers)
│ ├── config.php
│ └── bootstrap.php
│
├── core
│ ├── router
│ │ ├── routes.php
│ │ └── routing.php
│ ├── Middleware
│ │ └── ... (middleware files)
│ ├── Database.php
│ ├── App.php
│ ├── Container.php
│ └── ... (other core components)
│
├── views
│ ├── home.views.php
│ └── ... (other views)
└── .env
Inside it there's the index.php file and the Static dir. This file will require every needed component from the src
files and will manage the requests via the Router function. It contains also a .htaccess file, that makes sure the requests are handleld correctly.
It containse all Core components of the website, such as the Router, the Middleware, the Container and so on.
The Src Directory contains all of the source code
that builds the backend of the website: functions, classes, environment configurations, database conncetion and so on. Inside the src Directory There's the Controllers Directory.
The Controllers Directory holds the controller files, they are required by the Router in index.php and manage their respective view logic. For example, home.php manages the complex logic for the view home.view.php.
The Views Directory, holds all of the view files of the websites. The view files contain simple logic to generate and display HTML
elements. The complex logic and the various kinds requests, are handled by the controllers
.
The Static Directory contains all the statics files and assets of the website such as images, css stylesheets
and javascipt
front end files.
The config.php file contains an array for environment configurations, it's required in the index.php file. The config.php file loads ,thanks to the dotenv lybrary, environment variables such as API keys, database usernames and psswords and so on. In this project we use the Dotenv library, you can consult required libraries in the composer.json file.
if (file_exists(BASE_PATH . '/.env')) {
$dotenv = Dotenv\Dotenv::createImmutable(BASE_PATH);
$dotenv->load();
}
return [
'database' => [
'host' => $_ENV['DB_HOST'],
'port' => $_ENV['DB_PORT'],
'dbname' => $_ENV['DB_NAME'],
'charset' => 'utf8mb4',
'user' => $_ENV['DB_USER'],
'password' => $_ENV['DB_PASSWORD']
],
'services' => [
'secret_key' = $_ENV['secret_key']
]
];
DB_HOST=localhost
DB_PORT=3306
DB_NAME=your_db_name
DB_USER=your_db_user
DB_PASSWORD=your_db_password
SECRET_KEY=your_secret_key
In this project we use pestphp/pest framework to handle testing.
To install the necessary PHP
libraries for the project, run the following command:
composer install
This command will read the composer.json file and install all of the needed libraries.