White-Glove Support: Behind the Scenes Benefits of...
Engineering Staff Augmentation 2.0 is all about enabling software teams to quickly add global talen...
During a recent technical assessment, I was tasked to develop a simple CRUD events management system in PHP. I planned on using Laravel as I have always done, after all, it is really easy. However, I run into a few problems and was unable to proceed beyond creating a new Laravel project. My only other option was to do the app in vanilla PHP, however, given that I had been using Laravel for a long time, I had totally forgotten how to accomplish this simple task. For anyone else out there like me, here is a simple step by step way of developing a simple CRUD application without any frameworks.
I will begin by taking you through what the file structure will look like. If you would like to look at the full project you can find it here
I will first show you what the file structure looks like:
Events
First, we need to create a connection to the database so we can be able to save the data. I will be using a MySQL database which I will call events.
We will do all the configurations in the config.php file.
PHP has an inbuilt class, PDO, for handling database connections.
It is, however, not advisable to pass in the database parameters as I did above, they should always be passed in as environment variables.
We will then define a class that will handle all the database transactions which means we only need to call the config.php in this one file, instead of every time
we need to interact with the database. I have defined INSERT, UPDATE, SELECT, and DELETE statements here. Notice that I make use of prepared statements to provide a level of security against SQL injection.
I created a script, create_migration.php to generate the migration files. When this script is run with the table name, it generates the migration file for creating that table.
To run my migrations, I run the script below, migrate.php. If I want to run all migrations, I run it without any flags otherwise, I run it with the file name of the migration to run.
A little a bit about this script, I have used the concept of the hash table to find which migrations have been run or have changed and which haven’t been run. Only the migrations that have either changed or not been run will run. To help with making this check and also running the migrations, I use the HashMigrations class in the hash_migrations.php file.
Within this class, I have methods which:
Check if a migration has been run before.
When the migrations are run, we check if a particular migration has been run before, if yes we check if the contents of the migrations file have changed, if yes we run the migrations otherwise we skip that migration. If the migration has not been run before we run the migration, hash it and add it to the migrations_list.
Next, we will add the functionality for the CRUD. I have done this in the event.php.
Within this file, I have defined methods for creating, updating, viewing and deleting an event.
This will be the index page. This page will display the events, and have a button for creating a new event.
Below is what the landing page would look like. I have made use of bootstrap in this project. Check out this link for more on bootstrap.
Each event item is clickable and when clicked, it should redirect to a page that shows the details of the event. To enable this, we pass the id of the event to the URL.
We will create a page that will display the details of a single event. When an event is clicked, it will redirect to this page.
The above line of code picks up the id which is passed in the URL and passes it along to the viewEvent method which then returns details of the selected event.
The page also has edit and delete buttons. The edit button when clicked also passes the id of the event to the URL.
The delete button loads a popup a modal which asks the user to confirm if they would like to delete the selected event. The pop up looks like this
The landing page has a link that allows a user to add an event. When clicked, the user is redirected to a page that loads a form so a user can enter details of the event to be created.
The user fills in the details in the form above when done, they hit the submit button, which submits the data.
This section of the code runs after the form is submitted. The $_POST is a super PHP global variable that is used to collect form data.
As you can see the input elements have a name attribute, this attribute becomes the key of the associative $_POST array with the form input value becoming the value.
As you can see, once the entered values are picked from the array, they are passed to the addEvent method which then handles the data insert.
The edit works more or less the same as the create except that in the case of edit, the form is pre-populated.
When a user selects Yes from the modal that pops up when the delete button is clicked.
The id of the event is passed to the URL and delete.php is called where the id is picked from the URL and passed to the method deleteEvent which handles the delete. Upon delete, the user is redirected to the landing page where a success message is displayed.
This is achieved through the use of PHP’s super global variable $_SESSION. We add the message to be displayed to the session.
The above line of code is setting the message to display in the PHP $_SESSION global variable.
We then pick this message from the session variable and display on the landing page
The above code handles the display of the flash messages from the session variable.
And that marks the end of our very simple CRUD application in vanilla PHP without using any framework. I hope it has been helpful. If you have any comments or questions please do feel free to ask.
Engineering Staff Augmentation 2.0 is all about enabling software teams to quickly add global talen...
Inc. magazine launched its first “Best in Business” awards to “celebrate the companies making...
According to research by The Predictive Index, 76 percent of CEOs say they plan to allow remote wor...
Staff augmentation can take many forms. Traditionally, this model involves sending a job descriptio...
According to Dunn and Bradstreet, “20 to 25% of all outsourcing relationships fail within two yea...
The pandemic has transformed the tech world seemingly overnight. As we close out a chaotic 2020, CT...
During a recent technical assessment, I was tasked to develop a simple CRUD events management system in PHP. I planned on using Laravel as I have always done, after all, it is really easy. However, I run into a few problems and was unable to proceed beyond creating a new Laravel project. My only other option was to do the app in vanilla PHP, however, given that I had been using Laravel for a long time, I had totally forgotten how to accomplish this simple task. For anyone else out there like me, here is a simple step by step way of developing a simple CRUD application without any frameworks.
I will begin by taking you through what the file structure will look like. If you would like to look at the full project you can find it here
I will first show you what the file structure looks like:
Events
First, we need to create a connection to the database so we can be able to save the data. I will be using a MySQL database which I will call events.
We will do all the configurations in the config.php file.
PHP has an inbuilt class, PDO, for handling database connections.
It is, however, not advisable to pass in the database parameters as I did above, they should always be passed in as environment variables.
We will then define a class that will handle all the database transactions which means we only need to call the config.php in this one file, instead of every time
we need to interact with the database. I have defined INSERT, UPDATE, SELECT, and DELETE statements here. Notice that I make use of prepared statements to provide a level of security against SQL injection.
I created a script, create_migration.php to generate the migration files. When this script is run with the table name, it generates the migration file for creating that table.
To run my migrations, I run the script below, migrate.php. If I want to run all migrations, I run it without any flags otherwise, I run it with the file name of the migration to run.
A little a bit about this script, I have used the concept of the hash table to find which migrations have been run or have changed and which haven’t been run. Only the migrations that have either changed or not been run will run. To help with making this check and also running the migrations, I use the HashMigrations class in the hash_migrations.php file.
Within this class, I have methods which:
Check if a migration has been run before.
When the migrations are run, we check if a particular migration has been run before, if yes we check if the contents of the migrations file have changed, if yes we run the migrations otherwise we skip that migration. If the migration has not been run before we run the migration, hash it and add it to the migrations_list.
Next, we will add the functionality for the CRUD. I have done this in the event.php.
Within this file, I have defined methods for creating, updating, viewing and deleting an event.
This will be the index page. This page will display the events, and have a button for creating a new event.
Below is what the landing page would look like. I have made use of bootstrap in this project. Check out this link for more on bootstrap.
Each event item is clickable and when clicked, it should redirect to a page that shows the details of the event. To enable this, we pass the id of the event to the URL.
We will create a page that will display the details of a single event. When an event is clicked, it will redirect to this page.
The above line of code picks up the id which is passed in the URL and passes it along to the viewEvent method which then returns details of the selected event.
The page also has edit and delete buttons. The edit button when clicked also passes the id of the event to the URL.
The delete button loads a popup a modal which asks the user to confirm if they would like to delete the selected event. The pop up looks like this
The landing page has a link that allows a user to add an event. When clicked, the user is redirected to a page that loads a form so a user can enter details of the event to be created.
The user fills in the details in the form above when done, they hit the submit button, which submits the data.
This section of the code runs after the form is submitted. The $_POST is a super PHP global variable that is used to collect form data.
As you can see the input elements have a name attribute, this attribute becomes the key of the associative $_POST array with the form input value becoming the value.
As you can see, once the entered values are picked from the array, they are passed to the addEvent method which then handles the data insert.
The edit works more or less the same as the create except that in the case of edit, the form is pre-populated.
When a user selects Yes from the modal that pops up when the delete button is clicked.
The id of the event is passed to the URL and delete.php is called where the id is picked from the URL and passed to the method deleteEvent which handles the delete. Upon delete, the user is redirected to the landing page where a success message is displayed.
This is achieved through the use of PHP’s super global variable $_SESSION. We add the message to be displayed to the session.
The above line of code is setting the message to display in the PHP $_SESSION global variable.
We then pick this message from the session variable and display on the landing page
The above code handles the display of the flash messages from the session variable.
And that marks the end of our very simple CRUD application in vanilla PHP without using any framework. I hope it has been helpful. If you have any comments or questions please do feel free to ask.
Tap into a global talent pool and hire the “right” developers in days, not months.
Accelerate your career by working with high-performing engineering teams around the world.
BECOME A DEVELOPERWe take great pride in matching our developers with the best partners. Tell us about your team below!