-
-
Notifications
You must be signed in to change notification settings - Fork 25
Using DOM in PHP
The easiest way to use DOM in your PHP application is to install it as a dependency via Composer.
From your project's root directory, assuming you have Composer installed:
composer require phpgt/dom
Composer will download and install DOM and its dependencies into your project within the vendor/
directory. Inside that directory is the Composer autoloader script, which can be used to magically load any class you have installed.
Once DOM is installed using Composer:
<?php
require "vendor/autoloader.php";
$document = new Gt\Dom\HTMLDocument("<!doctype html><h1>Hello, World!</h1>");
$h2 = $document->createElement("h2");
$h2->textContent = "Goodbye, World!";
$document->body->appendChild($h2);
echo $document->saveHTML();
All releases are available on Github in zip and tar format for downloading. Of course you can clone the git repository too. This library follows the PSR-4 standard for autoloading, so once you have this library's PHP scripts in your project you can use a simple autoloader function to load the classes as they are referenced in your code.
A webpage's lifespan begins with an HTTP request from a browser to a server. After the server processes the page in some way, a corresponding HTTP response is sent back to the browser. For example, a request might be to get /quote-of-the-day
and a corresponding response might be <!doctype html><h1>Quote of the day</h1><p id="output">Don't waste your time, or time will waste you.</p>
. Building the response using code is where DOM comes in to make things easier.
The DOM is an indispensable tool when working with webpages, but to get the most out of it will require some code to route the incoming request to your code in a structured way. See the following sections for trivial examples of how to do this.
More from PHP.Gt: The PHP.Gt WebEngine is a rapid development engine for PHP 7 applications that utilises DOM throughout and handles all of the routing, loading, rendering, etc. for you.
To create a DOM representation of an HTML page, all that is required is to construct a new HTMLDocument
and pass the HTML as a string to the constructor's first parameter.
page.html
:
<!doctype html>
<h1>List of useful programming books</h1>
<ul>
<li>Code Complete 2, Steve McConnell</li>
<li>Clean Code, Robert Martin</li>
<li>Git for teams, Emma Jane Hogbin Westby</li>
<li>Regular Expressions Cookbook, Jan Goyvaerts, Steven Levithan</li>
</ul>
index.php
:
$html = file_get_contents("page.html");
$document = new HTMLDocument($html);
// Create a new entry and add it to the end of the list:
$ul = $document->querySelector("ul");
$li = $document->createElement("li");
$li->textContent = "Boolean Logic for Babies, Eric Redmond";
$ul->appendChild($li);
In the above example we can see a contrived example of how to take a static string of HTML and begin adding dynamic content to it. We are loading the HTML from a file called page.html
as to not mix the page view with the page logic, but we will cover [separating logic][#separating-logic] later in this section.
// TODO: echo (string)$document;
// TODO: file_get_contents("page/$uri");
// TODO: executing page logic PHP in context of page view. Note WebEngine here.
PHP.Gt/Dom is a separately maintained component of PHP.Gt/WebEngine.