Brief:
This project focuses heavily on the PHP and the Back-end. The main goal is to enhance our skills and gain a deeper understanding of how things work. In the real world, there are frameworks that handle the job with the support of dedicated teams and communities, ensuring stability and security. Hence, what we create is not advisable for production due to security concerns.
This project was inspired by Laracasts: PHP For Beginners. However, what I'm writing may not be beginner-friendly. I recommend you read it after acquiring a foundational understanding of the basics.
View the full project on GitHub: Repository.
check the changelog to see added/updated features.
Walkthrough:
Considering the project's scope, it will be divided into multiple articles and parts. Each article will have links to facilitate easy navigation and tracking. We will cover fundamental aspects of creating a PHP web app, including database management, routing, request validation, authentication, authorization, middleware, file system handling (uploading), sessions, and more. Additional features may be added in the future as I expand my knowledge.
Prerequisites:
-
PHP 8 >.
-
Apache server (XAMPP, MAMP, WAMP, Laragon,...) (I use Laragon).
-
MySQL Database (You can use whatever Database you are comfortable with).
Setup Environment:
-
Run Apache & MySQL,
-
Create a MySQL database, named
vanilla_blog
,
Folder structure
Feel free to design the file structure according to your preferences, as long as it remains well-organized. The file structure I will use:
.htaccess:
The .htaccess files provide users with the ability to configure specific directories on the web server they have control over, without directly modifying the main configuration file. In our case, we can utilize this file to redirect all routes to: index.php
, thereby enabling proper routing functionality.
The file's content:
Useful constants
Since our entry point is located within the public
folder, it is important to ensure that all directory paths are relative to it. To facilitate this, we can use the BASE_PATH
constant, which will serve as a reference point for constructing file paths and accessing resources within our project.
Why put an entry point in the public folder:
Let's imagine we have this folder structure:
If we visit: http://someurl/router.php
, we will have direct access to the router.php
file, which poses a security vulnerability.
Set public folder as root:
It depends on how you are serving your PHP application. However, if you are using Laragon, you can follow these steps to make the necessary updates: \laragon\etc\apache2\sites-enabled\auto.vanilla-blog.test.conf
Update DocumentRoot
from:
To:
If the file does not exist, create it:
Useful Utility function:
| function | purpose |
| --- | --- |
| isUrl | Check if the current route equals the given route |
| formatDate | Format date to DD/MM/YYYY
|
| base_path | Return the base path of a file |
| public_path | Return the base path of a file (Images, uploads,...) |
| component | To require component by its file name (without ".php") |
| view | To require component by its file name (without ".view.php") |
| abort | Abort the request and render the associated view |
| config | require config file by its name |
| dd | Dump & die. similar to Laravel |
Front-end and style:
To primarily concentrate on the PHP side, I have made the decision to incorporate Tailwind CSS and Flowbite UI. Additionally, I plan to integrate Alpine.js at a later stage.
Partials:
we will need three base components to start with:
-
Head,
-
Navigation,
-
Footer.
Let's create them!
Conclusion:
Now that we have successfully created and set up our environment, the next step is to explore how to access and implement the routing system.
See you in the next one!