How To Code on PHP7 Without using a Framework

How To Code on PHP7 Without using a Framework

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.

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire Dedicated Backend Developers

Want to create a backend for a web or mobile app using PHP & JS frameworks? **[Hire Dedicated Backend Developers](https://hourlydeveloper.io/hire-back-end-developer/ "Hire Dedicated Backend Developers")** who offer end-to-end, robust, scalable...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...