Pro PHP 8 MVC: Model View Controller Architecture-Driven Application Development
()
About this ebook
Examine the building blocks that make any good MVC framework using PHP 8. This book exposes all the considerations that many developers take for granted when using a popular framework, and teaches you how to make this MVC framework your own.
You'll quickly get started writing your first bit of framework code, then, you build a variety of examples using aspects of an MVC framework, including a router, a template engine, a database library, a persistence engine (ORM), and a testing framework. In the next section, you'll implement sessions, caches, file systems, queues, logging, and mail. You'll wrap up by building a larger scale sample web application: a sales website for a company that sells rockets.
Along the way, this book lays bare all the secret parts of MVC to take with you to apply to your own PHP-based MVC projects.
What You Will Learn
- Build PHP-based web applications using the model view controller (MVC) architecture
- Write your first bit of framework code
- Compare the code you write with how the popular frameworks do the same kinds of things
Create various aspects of applications, engines, and other frameworks - Wrap up with a sample case study application
Who This Book Is For
Experienced PHP and web developers. Some prior experience with PHP and web development at least recommended.
Related to Pro PHP 8 MVC
Related ebooks
Web Development Essentials: A Beginner's Guide to HTML, CSS, JavaScript, and SEO: HTML 101 : Beginner to pro Rating: 0 out of 5 stars0 ratingsPHP 8 Solutions: Dynamic Web Design and Development Made Easy Rating: 0 out of 5 stars0 ratingsLearn ASP.NET MVC Rating: 4 out of 5 stars4/5Modern Web Development with Deno: Develop Modern JavaScript and TypeScript Code with Svelte, React, and GraphQL (English Edition) Rating: 0 out of 5 stars0 ratingsExpressive Websites Rating: 0 out of 5 stars0 ratingsCreative Education and Dynamic Media Rating: 0 out of 5 stars0 ratingsBuilding Design Systems: Unify User Experiences through a Shared Design Language Rating: 0 out of 5 stars0 ratingsBuilding a Human Framework with AI Rating: 0 out of 5 stars0 ratingsPro TypeScript: Application-Scale JavaScript Development Rating: 4 out of 5 stars4/5Pro ASP.NET 4.5 in C# Rating: 0 out of 5 stars0 ratingsCreating Dynamic UIs with Android Fragments - Second Edition Rating: 0 out of 5 stars0 ratingsSwift 5 for Absolute Beginners: Learn to Develop Apps for iOS Rating: 0 out of 5 stars0 ratingsBasic Guide to Programming Languages Python, JavaScript, and Ruby Rating: 0 out of 5 stars0 ratingsStructured writing A Clear and Concise Reference Rating: 0 out of 5 stars0 ratingsGame Programming with Unity and C#: A Complete Beginner’s Guide Rating: 0 out of 5 stars0 ratingsThe Elements of Internet Style: The New Rules of Creating Valuable Content for Today's Readers Rating: 0 out of 5 stars0 ratingsLearn iOS Application Development: Take Your Mobile App Development Skills to the Next Level with Swift and Xcode (English Edition) Rating: 0 out of 5 stars0 ratingsTest Driven Development A Complete Guide - 2020 Edition Rating: 0 out of 5 stars0 ratingsCreating Great Android Apps: Tips and Advice Rating: 0 out of 5 stars0 ratingsASP.NET 3.5 CMS Development Rating: 0 out of 5 stars0 ratingsAngular 9 UI/UX Mastery Rating: 0 out of 5 stars0 ratingsWeb Programming with Go Rating: 0 out of 5 stars0 ratingsSwift Essentials - Second Edition Rating: 0 out of 5 stars0 ratingsAngularJS Web Application Development Blueprints Rating: 0 out of 5 stars0 ratingsHow to Make a Game: Go From Idea to Publication Avoiding the Common Pitfalls Along the Way Rating: 0 out of 5 stars0 ratingsBeginning JavaScript: The Ultimate Guide to Modern JavaScript Development Rating: 0 out of 5 stars0 ratingsMy First Mobile App for Students: A comprehensive guide to Android app development for beginners (English Edition) Rating: 0 out of 5 stars0 ratingsEvaluation of Some Android Emulators and Installation of Android OS on Virtualbox and VMware Rating: 0 out of 5 stars0 ratings
Internet & Web For You
The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 5 out of 5 stars5/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5Beginner's Guide To Starting An Etsy Print-On-Demand Shop Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5How To Make Money Blogging: How I Replaced My Day-Job With My Blog and How You Can Start A Blog Today Rating: 4 out of 5 stars4/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsCoding All-in-One For Dummies Rating: 4 out of 5 stars4/5The Mega Box: The Ultimate Guide to the Best Free Resources on the Internet Rating: 4 out of 5 stars4/5Coding For Dummies Rating: 5 out of 5 stars5/5The Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5The Designer's Web Handbook: What You Need to Know to Create for the Web Rating: 0 out of 5 stars0 ratingsCybersecurity For Dummies Rating: 4 out of 5 stars4/5Everybody Lies: Big Data, New Data, and What the Internet Can Tell Us About Who We Really Are Rating: 4 out of 5 stars4/5200+ Ways to Protect Your Privacy: Simple Ways to Prevent Hacks and Protect Your Privacy--On and Offline Rating: 0 out of 5 stars0 ratingsHow To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5How To Start A Podcast Rating: 4 out of 5 stars4/5Mike Meyers' CompTIA Security+ Certification Guide, Third Edition (Exam SY0-601) Rating: 5 out of 5 stars5/5Hacking : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Ethical Hacking Rating: 5 out of 5 stars5/5Six Figure Blogging Blueprint Rating: 5 out of 5 stars5/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5How to Disappear and Live Off the Grid: A CIA Insider's Guide Rating: 0 out of 5 stars0 ratingsThe Internet Is Not What You Think It Is: A History, a Philosophy, a Warning Rating: 4 out of 5 stars4/5Remote/WebCam Notarization : Basic Understanding Rating: 3 out of 5 stars3/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5Podcasting For Dummies Rating: 4 out of 5 stars4/5Stop Asking Questions: How to Lead High-Impact Interviews and Learn Anything from Anyone Rating: 5 out of 5 stars5/5
Reviews for Pro PHP 8 MVC
0 ratings0 reviews
Book preview
Pro PHP 8 MVC - Christopher Pitt
© Christopher Pitt 2021
C. PittPro PHP 8 MVChttps://doi.org/10.1007/978-1-4842-6957-2_1
1. Ways to Use PHP
Christopher Pitt¹
(1)
Verbena Close 1, Stellenberg, Durbanville, Cape Town, South Africa
Let’s talk about a few different ways to use PHP. The main ones you’re likely to encounter are
Running scripts inside a web server, to make websites
Running scripts from the command line/terminal
Let’s look at a few variations of these. With the exception of coding on the go, I’m not going to go too deep into the setup. By the time you get your hands on this book, instructions like that are likely to be out of date.
You’re better off searching for something like how do I install PHP on [your operating system here]
…
Running Scripts in the Terminal
Have you ever seen a computer hacker in a movie? They are usually hunched in front of a keyboard and screen, typing frantically. Sometimes they are wearing a hoodie.
The truth is that programming and using a terminal are normal things to do. You’re probably more used to seeing a terminal window if you use a computer made by Apple or are running Linux than if you are on a Windows machine.
Terminal windows, sometimes called command prompt or console, are just a direct line of communication to the inside of your computer. You can use them to install new programs or run scripts you make yourself.
You don’t have to use a terminal window to run your scripts. If you prefer a visual interface, skip ahead.
The steps to install and use PHP differ depending on the operating system you are using. If you’re using macOS, you can use Homebrew to install PHP and a database.
If you’re using Linux, you can use a built-in package manager to install the same things.
On Windows, you could try to use the Windows Subsystem for Linux, which will provide the same terminal interface you’d find on a Linux computer. Or you can go with a package manager like Chocolatey.
The official PHP documentation provides an up-to-date list of instructions for how to install PHP on the most common operating systems.
As an aside, the terminal app I use is called HyperTerm. I like it because I can configure it using JavaScript, and I can also use the same theme as I am using in my code editor.
../images/299823_2_En_1_Chapter/299823_2_En_1_Figa_HTML.jpgHyperTerm terminal app, running on macOS
Running a Website Through a GUI
Some folks prefer a more visual approach to running their website. There are plenty of good options to choose from, but the one I’d recommend is an app called XAMPP.
You can find downloadable installers for common operating systems, as well as instructions for how to use the installers, on the XAMPP site. As opposed to running PHP scripts in a terminal window, XAMPP will give you a place to put PHP files that are run by a web server. The difference is subtle and important.
When we run scripts directly, they are run until they complete. If they don’t complete, it’s typically because there’s a problem with the script, like an infinite loop.
Some frameworks and libraries introduce the idea of long-running scripts or server scripts. I’m not talking about those in this case. I’m talking about scripts we might want to execute for a simple purpose or that are intended to be run frequently, as part of server maintenance.
Common examples are a script to rename a bunch of files, one to remove old log files, or even a script to run some scheduled tasks.
When we use a web server, the web server takes the particulars of the request (headers, parameters, etc.), and it executes the script. At the end of the day, the same code is run, but the web server is taking some of the work off our hands. It’s also taking the output and error logging off our hands, to some extent.
Later, when I ask you to run a script, you may have to interpret that in the context of your web server. If I tell you to run a script, that may mean putting it in a file that the web server can serve. I’ll tell you when and what to do…
Running a Website Through a Virtual Computer
Let’s say you want to run your code on your own computer, but you don’t want it clogging up your filesystem or leading to all sorts of new things being installed. In that case, you can use something like VirtualBox.
VirtualBox is a program you install that allows you to create virtual computers
that run on your computer. You can decide how many resources they’re allowed access to. When they’re suspended, they don’t use any resources, except the hard drive space they need to remember what they were doing before.
The process of setting them up and using them is not unlike setting up a new physical computer. You need installation files for your preferred operating system, and then you need to install PHP (and other tools) on them as you would if they were a physical computer.
It’s a bit more work than installing those on your actual computer, but often a lot cleaner too.
You can use the base VirtualBox application, or you can go a step further and use the automatic setting-up help that comes from a bit of software called Vagrant. It’s a tool that lets you use scripts to set up and maintain VirtualBox (and other) virtual computers. You can even use the recipes other people make, so you don’t have to do any of the heavy lifting yourself.
I recommend these resources when learning more about Vagrant:
Vagrant CookBook will explain what Vagrant does and how to use it.
Phansible will ask what you want installed and then create the Vagrant scripts for you.
../images/299823_2_En_1_Chapter/299823_2_En_1_Figb_HTML.jpgphansible.com Vagrant provisioning tools
Running a Website on a Remote Server
At some point, you’re going to want other people to see and use the website you’ve made. There are ways to allow them to see the website running on your local computer, but they’re not meant as a permanent solution.
Instead, many companies offer what they like to refer to as cloud hosting
or virtual server hosting.
There are some big names, like Amazon Web Services and Google Cloud. There are also smaller names like DigitalOcean and Vultr. I prefer the smaller companies because their management consoles are much easier to understand.
Once you have an account with DigitalOcean, you can log in and create a virtual server. It’s similar to a VirtualBox server in that it isn’t a physical machine. You can still run popular operating systems on it, like Ubuntu Linux.
../images/299823_2_En_1_Chapter/299823_2_En_1_Figc_HTML.jpgCreating virtual servers on DigitalOcean
In fact, all of the companies I’ve mentioned will allow you to set up some form of virtual server, running Linux. From that point, you need only follow the instructions you would otherwise have done in a terminal window on your personal computer.
If you prefer someone else doing the heavy lifting for you – as do I – you can use a service like Laravel Forge to install everything you’ll need to run a PHP application.
We will be referring back to Laravel often in this book. While Laravel Forge is geared toward supporting Laravel applications, it can host websites built to work with other frameworks and even websites written in other languages.
I host many NodeJS websites on my Forge servers, because I still get all the security and automation that comes from using Forge for those sites.
Taylor Otwell – the creator of Forge – has generously offered a coupon for 35% off the first year of Forge. You can use the coupon code lHz71w7Z, added to your billing profile, after adding a payment method (but before subscribing).
../images/299823_2_En_1_Chapter/299823_2_En_1_Figd_HTML.jpgProvisioning virtual servers on Laravel Forge
Hosting in the cloud
is not free. Some of these companies will give you generous trial accounts, but you’ll start to pay for their services sooner or later. Fortunately, you don’t need to pay anything to get started with PHP MVC development, so long as you do it on your personal computer…
Coding in a Sandbox
When you want to test out some code, but you’re not around a familiar computer, you can code in a sandbox. Sandbox websites allow you to run PHP code and share links so you can demonstrate something to others.
There are two I’d recommend you try:
1.
Laravel Playground is tailored to work well for testing Laravel code, but you can execute any PHP code in it. You can also embed a playground (with custom code) on another site, which makes it great for documenting your PHP code in a wiki or documentation site.
2.
3v4l is the perfect place to see how the same code functions across different versions of PHP. Have a weird bug? Put the code there and share a link to it on Twitter.
Coding on the Go
As a final bit of fun, I’d like to talk about coding on an iPad. A lot of developers I interact with have access to an iPad, but don’t really know that it can be a powerful tool for coding on the go.
Here are a few apps I’d recommend you try, if you’d like to explore this topic…
The first app is called DraftCode. It’s a PHP code editor that allows for the execution of local PHP code in much the same way as if you were running the code in a GUI like XAMPP. At the time of writing, it costs $4.99.
../images/299823_2_En_1_Chapter/299823_2_En_1_Fige_HTML.jpgDraftCode code editor
This is one of the few apps I could find that even attempts to execute code without an Internet connection, which means you can use it on a train or a plane. It has good support for WordPress apps, and I’ve even gotten it to run Laravel apps in the past.
Unfortunately, it seems the maintainer has decided to offer supported versions of PHP (7.2 and 7.3) as additional in-app purchases. You can use the base app to run PHP 5.6 code, but you’ll have to pay an additional $3.99 or $5.99 to unlock the newer versions.
Alternatively, you could try an app called winphp. I haven’t had much experience with it, but it appears to offer the same functionality as DraftCode and then some. You can download it for free, but you can also unlock a ton of extra functionality (and hide ads) with a $4.99 in-app purchase.
../images/299823_2_En_1_Chapter/299823_2_En_1_Figf_HTML.jpgwinphp code editor
Both of these apps support external keyboards and mice/trackpads, provided you can get them to work with your iPad. I find even just having a keyboard cover goes a long way to helping me code. Nobody enjoys tapping a lot of text out on a screen, after all…
Next up, there’s an app called Working Copy . It’s a Git client that easily integrates with GitHub. The idea is that you’d use Working Copy to clone a repo you’re working on and then edit it in an app that can execute code. While you can edit text files in Working Copy, there’s no functionality built in to execute those files locally.
../images/299823_2_En_1_Chapter/299823_2_En_1_Figg_HTML.jpgWorking Copy Git client
iOS (and, in particular, iPadOS) has come a long way since I first started trying to code on an iPad. Aside from the great new keyboard and trackpad covers Apple has started making, the Files app makes it a lot easier to work with project files.
The last app I want to mention is called Termius. It’s an SSH client for iPad. There are many such apps, but Termius is interesting because it has companion apps you can use on desktop, so you can share settings between them.
If you are done with local development, on the iPad, and want to deploy your website to a remote virtual server, you’re going to need a way to communicate with that server. SSH is the way. Granted, you’ll need Internet access for this part, but if you’re used to working on an iPad, then deploying from an iPad might suit you.
../images/299823_2_En_1_Chapter/299823_2_En_1_Figh_HTML.jpgTermius SSH client
As with most of the other apps, in-app purchases will unlock extra features and remove ads. I don’t use Termius enough to need those features, so I’m still using a free account at the moment.
Of course, with GitHub offering a code editor built into their app, coding on an iPad is going to become much easier. You’ll obviously need Internet access and a paid GitHub account to use that option, but I personally think it’ll be worth it for the mobility.
A Note About Docker
You might have heard mention of Docker , especially when it comes to hosting websites. It’s a great way to manage servers, but it can be tricky to learn and use. I’ve outlined many great options for how to run PHP code, and I don’t see much value in adding another to the list.
You’re welcome to try it, but I’m not going to bring it up much beyond this point.
Where to from Here?
In the next chapter, we’ll start to run some PHP code. If you want to run that code from the terminal, you’ll need to have the terminal open to the same place as the script you want to run, and
Run the file directly (with something like php script.php).
Or use the PHP development server (with something like php -S).
Don’t worry – I’ll explain how to use these, in more detail, when we need to run the code.
If, on the other hand, you would prefer to use a web server, then you’ll need to install it and put your scripts inside the special web root
folder. Each web server is different, so you’ll need to refer to the documentation for the one that you choose to install.
© Christopher Pitt 2021
C. PittPro PHP 8 MVChttps://doi.org/10.1007/978-1-4842-6957-2_2
2. Writing Our First Bit of Code
Christopher Pitt¹
(1)
Verbena Close 1, Stellenberg, Durbanville, Cape Town, South Africa
It’s time to dive in and write some code! Whoosh needs a website, and we’re going to build it for them. Since this is the first time we’re coding, at least in the context of this book, I’m going to spend a bit of time talking about how and where we write the code.
All the code I show you will be on GitHub. If this is your first time using something like Git, don’t stress. It’s a system of storing code and tracking changes to that code. Kind of like a database of events that are defined by what has happened to the code of your application. With it, you can go back to any point in your code and see what you had before.
It’s also a great way to work with other developers. They can create copies of your code and use those copies to suggest changes for you to make. GitHub provides an easy-to-use interface for reviewing and accepting those changes, so that you always know what’s going on in your project.
I’ll leave it up to a=the fantastic GitHub guide to Git to cover the basics. There are only a handful of commands you’ll need to review the code in this book:
1.
git clonegit@github.com:assertchris/pro-php-mvc.git
2.
cd pro-php-mvc
3.
git branch -a
If you’re familiar with GitHub and Git, feel free to skip ahead to the part of this chapter where we handle a request.
These commands create a local copy of the source code repository in the folder your terminal window is currently