This is a sequence of progressive enhancements, taking a static HTML site into an authenticated modern PHP MVC website
The website is about movies and user voted stars.
DB-drive websites in PHP are much simpler when programming in Object-Oriented PHP, so that's the approach taken here. If you've not programmed with an OO language before, you may wish to first work through the 2019 version of this case study:
https://github.com/dr-matt-smith/evote-movie-2019
-
Starting point
-
a flat HTML website with hardcoded links
-
https://github.com/dr-matt-smith/evote-movie-2022-01-basic-html
-
-
Change HTML to PHP
-
change all
.html
file extensions to.php
-
in every page change the navigation links to files ending with
.php
-
https://github.com/dr-matt-smith/evote-movie-2022-02-all-files-dot-php
-
-
Add Front Controller PHP OO architecture
-
move all display pages into folder
/templates
- (keep) images and css in folder
/public
- (keep) images and css in folder
-
create
src
folder (for all our class files) -
create PHP class
/src/MainController.php
class with methods to display each of the page templates -
create
composer.json
to define PHP namespacedTudublin
classes in/src
: -
at the terminal command line run
composer dump-autoload
to create a/vendor
folder and containing autoloading scriptautoload.php
-
add root website script
/public/index.php
to run the autoload, create anApplication
object and invoke itsrun()
method: -
in the
/templates
folder change all navigation links in the HTML content to the form/?action=<PAGE>
. This means that EVERY request goes through/public/incdexphp
and our application's switch-statement inApplication::run()
-
e.g.
/?action=about
for link to about page -
e.g.
/?action=sitemap
for link to sitemap page
-
-
create class
/src/Application.php
with arun()
method to test for value ofGET
variableaction
, create a$mainController
object, and invoke theMainController
method that corresponds to the value found foraction
in the URL -
https://github.com/dr-matt-smith/evote-movie-2022-03-front-controller
-
-
Separate header and foot page content in part-page templates that can be re-used
-
create
/templates/_header.php
containing HTML header and nav content -
create
/templates/__footer.php
containing HTML footer content -
update each page to remove duplicated code, set a
$pageTitle
variable, and require-in the header and footer templates -
https://github.com/dr-matt-smith/evote-movie-2022-04-header-footer-templates
-
-
List details from array of
Movie
objects-
declare a PHP class
/src/Movie.php
for data about movie objects -
add to class
Movie
a methodgetStarImage()
which returns a star image name, based on the vote averge: -
update the movie list template (
/templates/list.php
) to loop through an array ofMovie
objects to create the movies table -
create a new class
/src/MovieFixtures.php
that declares a single methodgetObjectArray()
. This method creates someMovie
objects, and returns an array containing these objects. -
update the
list()
method of classMainController
to get the array of objects from aMovieFixtures
instance, and pass that array ofMovie
objects to the movie list template -
https://github.com/dr-matt-smith/evote-movie-2022-05-movie-class-loop
-
-
Add PDO DB library and get Movies from database table
-
add library
pdo-crud-for-free-repositories
to the project using Composer: -
create a file
.env
containing your MySQL database credentials, for example: -
create a new class
/src/MovieRepository.php
and make this empty class inherit fromMattsmithdev\PdoCrudRepo\DatabaseTableRepository
: -
create a new directory
/dbsetup
, containing filemigrationsAndFixtures.php
to create/reset a DB tablemovie
, then get the array ofMovie
objects from aMovieFixtures
object, and insert all those objects into the database -
at the command line, execute PHP script
/dbsetup/migrationAndFixtures.php
. If no schema matching the name in your.env
file exists, then a new schema of the specified name will be created, and a messasge displayed to confirm this action: -
refactor the
list()
method of classMainController
to create a repository object, and dynamically retrieve allMovie
objects from the MySQL database: -
https://github.com/dr-matt-smith/evote-movie-2022-06-movie-db-table
-
-
Adding current page link styling in the navbar
-
add to the beginning of
/templates/_header.php
a set of variables, one for each nav link. -
In the nav-bar links themselves of template
/templates/_header.php
, a CSSclass
is added to each link, whose value will be either an empty string or the value (current_page
) from the style variable for each link. -
add to the beginning of each page template a statement to set the nav style link variable for that page to be the string
current_page
. -
https://github.com/dr-matt-smith/evote-movie-2022-07-current-page-link-styling
-
-
CRUD - adding a delete link on the Movie list page
-
add to
/templates/list.php
an extra table column headingActions
-
add to
/templates/list.php
an extra table column data cell being the wordDELETE
whose link URL ishref="/?action=delete&id=<movieIdHere>
-
add a new case to the front controller login in
/src/Application.php
. When the URL action isdelete
, a value forid
should be extracted from the URL, and passed toMainController
methoddelete(...)
-
add to
/src/MainController.php
new methoddelete(...)
, which uses the argument received to invoke the delete method of aMovieRepository
object. -
https://github.com/dr-matt-smith/evote-movie-2022-08-crud-delete
-
-
CRUD - adding a form for a NEW Movie to be added to the database
-
create new template
/templates/new_form.php
to display a form for a new Movie with a submission action of/?action=process_new
-
add to
/templates/list.php
a linkcreate a NEW Movie
whose link URL is/?action=new_form
-
add a new case to the front controller login in
/src/Application.php
. When the URL action isnew_form
, and whose action is to invoke methodform()
inMainController
-
add to
/src/MainController.php
new methodform()
to display the new form template -
add a new
process_new
case to the front controller login in/src/Application.php
-
add to
/src/MainController.php
new methodprocessNewMovie(...)
, which takes in the values received from the submitted form, creates a newMovie
object, -
https://github.com/dr-matt-smith/evote-movie-2022-09-crud-create
-
To get ideas of how to take things further see details from the 2020 version of this step-by-step project: