PHP FrameworkEstablishing a Foundation |
Summary
Create PHPServer-side scripting language for web development. MVC framework with a front controller design, a MySQLRelational database used to hold website content and search data. database to store content, ComposerUsed to manage the installation of third-party PHP libraries. to manage dependencies and TwigPHP template engine that simplifies the generation of HTML pages. as the templating engine.
Overview
Purpose | Establish a framework to support and manage the ongoing development effort. |
Goals |
|
Technology | MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS. - Apache, MySQLRelational database used to hold website content and search data. and PHPServer-side scripting language for web development., Brackets, ComposerUsed to manage the installation of third-party PHP libraries., TwigPHP template engine that simplifies the generation of HTML pages., GitCommand line tool for keeping track of changes in source files., BitBucketWeb-based service for hosting and sharing Git repositories. |
Resources | Udemy class “Write PHPServer-side scripting language for web development. Like a Pro: Build a PHPServer-side scripting language for web development. MVC Framework from Scratch” taught by Dave Hollingworth. (Highly recommended. The MVC framework used for this website comes almost directly from this class. This project writeup is at a high level, the class goes into details of the hows and whys.) |
Implementation
-
Install MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS.
In order to create development environment on a local machine MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS. stack was installed on a Macbook - OSX Yosemite v10.10.5. (I've worked with WAMP on Windows 10 with similar results.
-
Create Directory Structure
In the Document Root of the MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS. install, the following directory structure was created.
- App - Application specific files.
- App/Controllers - PHPServer-side scripting language for web development. classes that control application behavior.
- App/Models - PHPServer-side scripting language for web development. classes representing the data sources.
- App/Views - HTML and TwigPHP template engine that simplifies the generation of HTML pages. files that present the results to user.
- Core - PHPServer-side scripting language for web development. classes that support the framework.
- logs - Error log files.
- migration - Database tasks required for migration.
- public - Only folder directly publicly available to users.
- vendor - Vendor libraries
For MAMPS, the Document Root defaults to the htdocs directory e.g. /Applications/MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS./htdocs. For other environments this directory may be www. PHPInfo can be used to find the location.
-
Modify PHPServer-side scripting language for web development. Configuration
Modify php.ini file to turn on errors error reporting. Default location in MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS.: /Applications/MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS./bin/php/php7.1.0/conf/php.ini
display_errors = On
-
Modify Apache Configuration File
In the Apache configuration file - httpd.conf (Default MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS. location: /Applications/MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS./conf/apache/httpd.conf):
-
Change the Document Root to the pubic directory (created above).
DocumentRoot "/Applications/MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS./htdocs/public"
-
Add the following environmental variables:
SetEnv APP_STAGE DEVELOPMENT - Application Stage SetEnv RDSAmazon service for launching and managing relational databases._HOSTNAME localhost:8889 - Database Host SetEnv RDSAmazon service for launching and managing relational databases._DB_NAME tbd - Database Name SetEnv RDSAmazon service for launching and managing relational databases._USERNAME guest - Database Username SetEnv RDSAmazon service for launching and managing relational databases._PASSWORD enter-password - Database Password SetEnv SHOW_ERRORS true - Show errors on page
-
-
Install ComposerUsed to manage the installation of third-party PHP libraries.
Install ComposerUsed to manage the installation of third-party PHP libraries. to manage application dependencies and installations. It is available at getcomposer.org.For this project the command-line install option was used
Additionally the following commands were run to make ComposerUsed to manage the installation of third-party PHP libraries. globally available:
sudo mkdir /usr/local/bin
sudo mv composer.phar /usr/local/bin/composer
composer about -
Install TwigPHP template engine that simplifies the generation of HTML pages.
Use ComposerUsed to manage the installation of third-party PHP libraries. to install TwigPHP template engine that simplifies the generation of HTML pages., which is a PHPServer-side scripting language for web development. template engine that simplifies the creation of php and html view files. In a terminal window run:
composer require “twig/twig:~1.0”
-
Create Database
Create MySQLRelational database used to hold website content and search data. database TBD with the below tables:
- LKP_CONTENT_TYPE - Table listing the different types of content (e.g. projects, posts, etc.
- FACT_CONTENT - Table with the actual content text.
- GUEST - User that will be used by the application to access the database. This user should be granted select on the two tables above.
-
Create Front Controller Classes
The website has a single point of entry index.php and uses a Router class to direct request to the appropriate classes:
- public/.htaccess - Apache configuration file set to rewrite incoming request to go to the public index.php file, passing the original request as a query string.
- public/index.php - Entry point for all requests. It is responsible for calling the Router and passing it the query string.
- Core/Router.php- The Router converts the given query string into a call to the appropriate (application) Controller class method.
-
Create (Application) Controller Classes
The controller classes interact with the model and view classes and manage the behavior of the site:
- Core/Controller.php - is an abstract class of which all application controllers are subclasses.
- App/Controllers/Home.php - controls the behavior of the home page.
- App/Controllers/Posts.php - controls the behavior of the journal pages.
- App/Controllers/Projects.php - controls the behavior of the project pages.
-
Create Model Classes
Model classes interact with the database and supply data to the controllers:
- Core/Model.php - is an abstract class from which all application model classes are a subclass.
- App/Models/Content.php - pulls page content from database to supply to the views.
-
Create View Classes and Pages
- Core/View.php - View class responsible for rendering view request using the TwigPHP template engine that simplifies the generation of HTML pages. template engine.
- App/Views/base.html - Base TwigPHP template engine that simplifies the generation of HTML pages. template that contains elements common to all views such as the navigation bar from which all view html pages inherit.
- App/Views/Home/index.html - Home page.
- App/Views/Posts/index.html - Journal index page with list of all posts.
- App/Views/Posts/show.html - Shows the single journal entry selected by the user.
- App/Views/Projects/index.html - Project index page with list of all projects.
- App/Views/Projects/show.html - Shows the details of the single project selected by the user.
- App/Views/Errors/404.html - Shows a more user-friendly message if a requested page cannot be found.
- App/Views/Errors/500.html - Shows a more user-friendly message for all other errors not caused by the requested page not being found.
-
Setup and Initialize GitCommand line tool for keeping track of changes in source files. Repository
Since I had XCode installed GitCommand line tool for keeping track of changes in source files. was already installed on my Macbook.
-
Configure GitCommand line tool for keeping track of changes in source files.
git config --global user.name "enter-username"
git config --global user.email "enter-email" -
Initialize GitCommand line tool for keeping track of changes in source files. Repository in the Document Root directory (MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS. default location: /Applications/MAMPPersonal webserver stack to allow PHP and MySQL development on MacOS./htdocs)
git init
-
Create .gitignore file in the Document Root directory to exclude any files not to be included in the repository (e.g. error log files).
-
Add, commit and tag changes to the local GitCommand line tool for keeping track of changes in source files. repository:
git add .
git commit -m "Project 1: PHP FrameworkCreate PHP MVC framework with a front controller design, a MySQL database to store content, Composer to manage dependencies and Twig as the templating engine.: PHPServer-side scripting language for web development. Framework"
git tag -a PRJ001 -m "PHPServer-side scripting language for web development. Framework" -
Create BitBucketWeb-based service for hosting and sharing Git repositories. (or GitHub) account and establish relationship to local repository. BitBucketWeb-based service for hosting and sharing Git repositories. has these commands on their website and are available under the "I have an existing project" option.
git remote add origin https://TrialByData@bitbucket.org/TrialByData/trialbydata.git
-
Push local repository to remote host.
git push -u origin master
-