Git Product home page Git Product logo

php-from-scratch-workshop's Introduction

Workshop: creating a PHP 7+ application from scratch

Initialize the project:

git clone [email protected]:patrickallaert/php-from-scratch-workshop.git workshop

Configuration

Edit your /etc/hosts and add:

::1    workshop.local

or, if you still live in the IPv4 era:

127.0.0.1    workshop.local

Now let's configure Apache with a VirtualHost:

<VirtualHost *:80>
    ServerName workshop.local
    DocumentRoot /path/to/workshop/web

    <Directory /path/to/workshop>
        Require all granted
        AllowOverride all
    </Directory>
</VirtualHost>

After restarting Apache, let's try the whole configuration and edit web/index.php and put the above:

<?= "Hello world" ?>

Open http://workshop.local/, it should display: Hello world

Create the first web pages

In this workshop, we will create a skinny Twitter-like application made of two simple pages: a homepage showing a list of accounts and an account page showing the tweets for a specific account. For the sake of this workshop, we will first do this the dirty way and it will look like procedural PHP 3 code like some of us did 20 years ago ;-)

Some data are available in data/tweets.php as a big serialized array and should be considered as the database.

data/tweets.php:

<?php

return [
    "Princess_Leia" => [
        "name" => "Princess Leia",
        "tweets" => [],
    ],
    "Luke" => [
        "name" => "Luke Skywalker",
        "tweets" => [
            [
                "ts" => "2017-01-01T12:34:56+00:00",
                "tweet" => "@Princess_Leia => I'm @Luke Skywalker and I'm here to rescue you!",
            ],
        ],
    ],
    "Darth_Vader" => [
        "name" => "Anakin Skywalker",
        "tweets" => [
            [
                "ts" => "2017-01-13T16:00:00+00:00",
                "tweet" => "@Luke, I am your father",
            ],
            [
                "ts" => "2016-06-06T16:23:00+00:00",
                "tweet" => "Your powers are weak, old man.",
            ],
            [
                "ts" => "2016-05-10T19:00:00+00:00",
                "tweet" => "I've been waiting for you, @Obi-Wan. We meet again, at last.",
            ],
        ],
    ],
    "Obi-Wan" => [
        "name" => "Obi-Wan \"Ben\" Kenobi",
        "tweets" => [
            [
                "ts" => "2016-05-09T11:30:00+00:00",
                "tweet" => "Blast. This is why I hate flying.",
            ],
            [
                "ts" => "2016-05-08T09:00:00+00:00",
                "tweet" => "Your clones are very impressive. You must be very proud.",
            ],
            [
                "ts" => "2016-03-02T19:15:00+00:00",
                "tweet" => "Use the Force, @Luke",
            ],
            [
                "ts" => "2016-01-01T14:00:00+00:00",
                "tweet" => "@Luke, the Force will be with you",
            ],
        ],
    ],
];

Example of what web/index.php and web/account.php could look like:

web/index.php:

<!DOCTYPE html>
<html>
<head>
    <title>Twitter homepage</title>
</head>

<body>
<h1>Twitter homepage</h1>
<ul>
    <?php foreach (require "../data/tweets.php" as $account => $data): ?>
    <li>
        <a href="account.php?username=<?= htmlentities($account)?>">
            <?= htmlentities($data["name"]) ?>
        </a>
    </li>
    <?php endforeach ?>
</ul>
</body>
</html>

web/account.php:

<?php
$account = $_GET["username"];
$data = (require "../data/tweets.php")[$account];
?>
<!DOCTYPE html>
<html>
<head>
    <title>Tweets from <?= htmlentities($data["name"])?> - @<?= htmlentities($account) ?></title>
</head>

<body>
<h1>Tweets from <?= htmlentities($data["name"])?> - @<?= htmlentities($account) ?></h1>
<ul>
    <?php foreach ($data["tweets"] as $tweet): ?>
    <li><?= htmlentities($tweet["tweet"]) ?> <span class="date">(<?= $tweet["ts"] ?>)</span></li>
    <?php endforeach ?>
</ul>
</body>
</html>

If you haven't completed the above steps, you can checkout step-1 to not be late:

git checkout step-1

php-from-scratch-workshop's People

Contributors

patrickallaert avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.