Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Pro PHP 8 MVC: Model View Controller Architecture-Driven Application Development
Pro PHP 8 MVC: Model View Controller Architecture-Driven Application Development
Pro PHP 8 MVC: Model View Controller Architecture-Driven Application Development
Ebook458 pages2 hours

Pro PHP 8 MVC: Model View Controller Architecture-Driven Application Development

Rating: 0 out of 5 stars

()

Read preview

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.  

LanguageEnglish
PublisherApress
Release dateMay 26, 2021
ISBN9781484269572
Pro PHP 8 MVC: Model View Controller Architecture-Driven Application Development

Related to Pro PHP 8 MVC

Related ebooks

Internet & Web For You

View More

Related articles

Reviews for Pro PHP 8 MVC

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    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.jpg

    HyperTerm 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.jpg

    phansible.​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.jpg

    Creating 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.jpg

    Provisioning 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.jpg

    DraftCode 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.jpg

    winphp 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.jpg

    Working 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.jpg

    Termius 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

    Enjoying the preview?
    Page 1 of 1