Since the introduction of Composer package manager and the PHP standards, writing PHP became easier and more manageable, whereas in the past you were almost forced to use a framework to maintain your project in a professional matter, nowadays this is not necessary, and today I will show you how to glue together a small API project with basic routing, third party packages, and testing without a framework.

There are few reasons why you don’t want to use a framework, you are creating a library, a small app/API, have more control, and so forth, depending on your cases you might want to use a framework don’t get me wrong.

Our goal is to create a simple Blog Api, each post will have an **_id, title, and body, _**you will able to list, create and view a post, we won’t use any database, a simple JSON file that will act as DB should be enough, all request/responses will be in JSON format

As you see, there are some fields and features missing, like a slug, summary, published date, author, tags, categories, and so forth, or the ability to delete/update, I decided to not implement those, and I’ll briefly explain some classes and code without getting into too much detail to make this article shorter, if you need an extra explanation of any step please leave it in the comments and I will do my best to help you there.

All code is available in https://gitlab.com/dhgouveia/medium-blog-api

Prerequisites

  • PHP 7.0 or greater
  • Composer
  • Basic knowledge of PHP, classes, composer, MVC pattern

Ok, let’s start!

Setting up Composer

The first thing we need to do is create our

composer.json needed to add 3rd party packages and manage our project with the autoloading feature, this will make importing classes easier.

Create a folder and type

composer init in your terminal and fill the information, it will create the composer.json file for us, then create our basic folder structure with some empty files called index.php,config.phpand an empty folder called App

Let’s add the first package by using the command line

composer require monolog/monolog:1.25.1 , it creates a vendor folder with the package we just added and a file called autoload.php , this file will contain all the path to the classes we add from 3rd parties and ours, monolog is a package to create logs files that will be used later on

#php #framework #backend #mvc #php7 #web-development #programming #composer

How To Code on PHP7 Without using a Framework
1.30 GEEK