Quick Start

Last updated: April 5th, 2019

Setup

To create your new MpPHP application, first make sure you're using PHP 7.1 or higher and have Composer installed. If you don't, start by installing Composer globally on your system. If you want to use an all-in-one setup for your development environment, check out Wampserver if your on windows.

Installation

Create your new project by running:

$ composer create-project mpphp/mpphp my-project

This will create a new my-project directory, download some dependencies into it and even generate the basic directories and files you'll need to get started. In other words, your new app is ready!

Running your MpPHP Application

On production, you should use a web server like Nginx or Apache. But for development, it's convenient to use the PHP's built ins web server.

Move into your new project and start the server:

$ cd my-project

$ php -S localhost:4000 -t public

Open your browser and navigate to http://localhost:4000/. If everything is working, you'll see a welcome page.

IMpPHP welcome page

Later, when you are finished working, stop the server by pressing Ctrl+C from your terminal.

Storing your Project in git

Storing your project in services like GitHub, GitLab and Bitbucket works like with any other code project! First you need to have git installed on your system. Init a new repository with Git and you are ready to push to your remote:

From within your project directory run:

$ git init

$ git add .

$ git commit -m "Initial commit"

Your project already has a sensible .gitignore file. And as you install more packages. For more on git visit the git documentation

Create your First Page in MpPHP

Creating a new page in MpPHP is a two-step process:

  1. Create a route: A route is the URL (e.g. /about) to your page and points to a controller;
  2. Create a controller: A controller is the PHP function you write that builds the page. You take the incoming request information and use it to return a response to the browser.

Create a Controller

Suppose you want to create a page - /lucky/number - that generates a lucky (well, random) number and prints it. To do that, create a "Controller file" in the "app/http/controllers" directory and a "controller" function inside of it:

Create a controller

To name a controller we use a snake case and we prefix action at the end of it.

Create a Route

Now you need to associate this controller function with a public URL (e.g. /lucky/number) so that the number() function is executed when a user browses to it. This association is defined by creating a route in the routes/web.php file:

Create a controller

That's it! If you are using PHP's built in web server, try it out by going to:

http://localhost:8000/lucky/number

If you see a lucky number being printed back to you, congratulations!

Rendering a Template

A PHP templating engine is the one which gives you ability to write HTML for your clients efficiently using PHP variables.

Originally PHP was built to be a templating engine, but yet it was never used primarily as a templating engine. There has been so many PHP templating engine so far in the market since PHP evolved, like Twig, Smarty, Volt, Latte to name a few, sadly they come bundled with OOP drama and we are trying to avoid all that So we are going to use the default engine but with a little tweak to make it more fun.

Notice

Up until the time of this documentation I am not aware of any PHP templating engine built without OOP so if you know of any please let me know by raising an issue.

Create the temlate

Template files live in the “resources/views” directory so too render HTML template in our application we simply create a “number.phtml” in our “resources/views” directory.

Now, use the handy _view() function to render a template. Pass it a number variable so you can use it in our template.

Call the view function

The syntax is used to “print” variables in PHP. Refresh your browser to get yournew lucky number!

Checking out the Project Structure

Just by creating our first MpPHP page we’ve managed to familiarize our selves with the directories we would be working with most of the time, but you still need to be formally introduce to the MpPHP directories:

  • app/

    The app directory holds the base code for your MpPHP application. If your working on the back end, this is where you will be spending most of your time.

    • entities/
      • models/

        This directory contains all the model files.

      • repositories/

        This directory contains all the repository files.

    • http/

      The Http directory holds different middleware, and controllers.

  • bootstrap/

    The bootstrap directory holds all the bootstrapping scripts used for your application.

  • config/

    Contains... configuration of course!. You will configure routes, services and packages.

  • public/

    This is the document root for your project: you put any publicly accessible files here.

    • css/

      This holds all the css files.

    • js/

      This holds all the javascript files.

  • resources/
    • views/

      This is where your template files live.

  • routes

    The routes directory hold all your definition files for routing such as web.php etc.

  • vendor/

    Third-party (i.e. "vendor") libraries live here! These are downloaded via the Composer package manager.