Question
Please help with the following in PHP build a PHP MVC Web application by using the code framework that we built together in class tutorials.
Please help with the following in PHP
build a PHP MVC Web application by using the code framework that we built together in class tutorials. The MVC application will have the following views at the following URLs:
1- localhost/Main/index -> site landing page
2- Localhost/Main/about_us -> the about us page
3- localhost/Contact/index -> the contact us form
4- localhost/Contact/read -> the listing of all messages
5- localhost/Count/index -> a page load counter
All views will be written in php files that you will organize in the views subfolder under the app folder as follows:
1- app/views/Main/index.php -> site landing page view
2- App/views/Main/about_us.php -> the about us page view
3- app/views/Contact/index.php -> the contact us form view
4- app/views/Contact/read.php -> the message listing view
5- Contrary to the other controller methods, the page load counter will simply return JSON-encoded data through one echo command
Page load counter
The Main controller class will contain a counter method which will
-
If the counter.txt file exists (use the file_exists function)
-
Open the counter.txt file for reading (use fopen);
-
Lock the file (use flock);
-
read the file into the $counter variable;
-
Close the file (use fclose);
-
-
Else
a. Set the $counter variable to '{"count":0}';
-
json_decode the $counter variable string and store it to $dCounter;
-
increment the page load counter attribute count in $dCounter;
-
json_encode the $dCounter back into $counter;
-
output the JSON string as the HTTP Response (echo the $counter);
-
Open the counter.txt file for writing (use fopen);
-
Lock the file for writing (use flock);
-
overwrite the file contents with the new JSON string (use fwrite).
Each of the following pages will call the Page load counter through an AJAX request and display the page load count at the bottom of the page, to the right of the page.
Helpful hint: Since the views are called into objects that are in Controller subclasses, it is valid to use the view method within views to include sub views, such as an Ajax-based page counter, or a navigation menu.
Site landing page:
About Us page
Introduce your team use random images
Contact Us page
In this method, you will need to open the log.txt file in append mode.
Add a form to send a message to the company. The form will have labels and fields for the author email (use an input of type email) and for the message (a textarea).
To receive the data, extract the appropriate data from $_POST and add it to a new associative array. Then, you will open a log.txt file for appending, lock the file for writing, write the new messages to it, and close the file.
Once the writing is complete, redirect to the localhost/Contact/read URL with the following instruction:
header('location:/Contact/read');
Helpful hint: When writing to the log.txt file, make sure that each write ends with a character to separate each message logged on a separate line. This will be important for the next page.
Read Message log page
Read and display the log.txt file, on a nice page. To make your life easier, use the file() function to read all lines of the log.txt files into a single array. In the view, use a foreach loop to output each element of the array, matching each line of the log.txt file.
In this example, log.txt contains the following:
{"author":"amy@email.com","message":"Hi"} {"author":"bob@email.com","message":"I love my job!"}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started