Do I need a framework to build a REST API in PHP? [closed]
Asked Answered
M

9

68

I am fairly new to PHP (about 8 months). I am building a web app, which is almost ready for beta. I am only now starting to think about what I would need to do to make a mobile version of the app.

As I understand, I should be building a REST API (please correct me if I'm wrong). I am currently NOT using a PHP framework for my web app. Should I be?

Should I now begin using a framework so that I could more easily implement my API? Or can I build my API without any framework at all?

Marniemaro answered 29/6, 2012 at 21:5 Comment(1)
REST API is a way to standardize the way you communicate with the backend. Namely through HTTP verbs GET, POST, UPDATE/PATCH and DELETE on endpoints such as /api/<object>/id/<related objects> and so on and so forth.Alli
J
56

SHORT ANSWER No, you don't need a framework to achieve your goal.

BUT it will be really easier if you use a framework to manage your API. I suggest you to go for a lightweight framework and maybe you can convert easily your webapp to the framework too, having one "app" to return two different "things" (web stuff & API).

Take a look at Laravel, Laravel 4 based REST API or a list of popular php rest api frameworks that can be used to build one.

Jem answered 29/6, 2012 at 21:10 Comment(9)
They will also add securityPhosphorus
@Jleagle Yes, I forgot to mention all the "ready-made" features of a framework such as security, CRUD, MVC compliance, etc...Jem
@Napolux why to use elephant when thing can be done with help of aunt! You are advising to load 23MB lib just for REST!Wrapped
I'm not advertising anything. I'm just saying a framework (not only big beasts like Symfony, but also Slim or other microframeworks) gives you a structure to start from.Jem
to complete your answer, could you provide some sort of starting point to begin coding without a framework?Spectacle
@defmx I would start from here: code.tutsplus.com/tutorials/… and then adapt to my needs.Jem
i hate laravel directory structure. mostrous, atrocious hydraKirin
Lumen is better suited for an API than Laravel, since you won't need a state and Lumen is faster.Cum
laravel is the best one if you want to manage DB manually ;)Urena
B
20

You certainly don't need any kind of framework to build a PHP REST API. REST is nothing more than a protocol convention built on top of HTTP. Since PHP can obviously handle HTTP requests, it has everything you need to build RESTful API's.

The whole point of frameworks is to handle common tasks and things that are otherwise tedious. REST API's are commonly built with PHP, so a plethora of frameworks exist. Personally, I would use a lightweight framework like slim simply to handle things like URI routing, parsing/cleaning request data, and generating responses.

Bogey answered 29/6, 2012 at 21:15 Comment(0)
P
5

No you do not need a framework to build a REST API, but it is highly recommended, as a well built framework can take care of things that can be very difficult and complicated otherwise, namely session authentications and caching and well separated architecture. Reinventing the wheel only gets you so far.

I'm a developer of Wave Framework which was developed keeping in mind an API-centric design (read more here). I encourage you to take a look into this framework and see if it might be something that could help you. It has a small group of developers, but it is slowly gaining recognition.

I encourage you to take a look at that and if it might fill your needs.

Pointenoire answered 18/9, 2013 at 10:23 Comment(2)
is the development stopped now?Pyrography
The link does not work either. I guess it is abandoned.Orthoepy
C
3

There are also tools that create a REST api from the DB without the need for extra code.

If you are using Postgres there is the excellent program postgREST that

serves a fully RESTful API from any existing PostgreSQL database. It provides a cleaner, more standards-compliant, faster API than you are likely to write from scratch.

Conley answered 17/9, 2016 at 5:34 Comment(0)
C
3

NO, YOU DON'T NEED ANY FRAMEWORK FOR PHP BACKEND.

If you are using php as backend then you don't need to use any resetAPI framework. Just create you own php files and generate JSON output for each response.

You must generate JSON output. That is enough. Benefits:- If you use own login to generate Json output then it will be tremendous light speed (based on your logic). If you use any framework then the performance issues will occure.

It will be better if you can design your own mini framework to do specific site's work.

Thanks

Clotho answered 23/3, 2019 at 9:58 Comment(0)
M
2

REST is more of a design ideology than a language framework, so NO you don't NEED to use any framework. However there is no advantage in reinventing the wheel (Sure some disadvantages are there like security, structure etc.).

If you want to avoid mayhem of MVC (which is not required specification of a REST architecture) you can use any PHP mico-framework (Slim, Lumen etc) they are really quick to learn and implement and allow PHP developer to write route based app (similar to those of MEAN and Express) saving a lot in time.

Most of these frameworks comes with an MVC as well but if you don't want to give a CMS for the API, MR (Model-Routes) is good enough (and practically the best) for all REST needs.

Since the dawn of angular like frameworks which allows your website to communicate with sever like any other API easily, I think even the CMS should access the API like any other app just with elevated rights or specific end-poin

Mandrake answered 15/7, 2015 at 10:37 Comment(2)
Hi Himanshu, I am already using AngularJS in my front-end and would like to use a PHP framework as a backend (currently coding in plain PHP). The only job of PHP is to fetch data from database and convert it to REST and vice-versa. Most importantly, I hate OOP, prefer simple functional programming. Already using MVC in client-side angularJS. Which framework would be best suited for me ? Thanks. (Note: I am NOT the one who asked this question)Sidewalk
Well the Web API ideally should give response in a JSON format. If I were in your shoes, I'd go with the Slim Framework. It's really easy to implement, lightweight and fast. Plus it allows you to keep your routing simple. REST doesn't have to be OOP or MVP (though it's recommended for scaling) Slim would get you up and running in no time :)Mandrake
B
1

The World is very very big, so, no one can do all things alone. Someone will help someone (who do something new) do something old. This is reason, libray and FW exist in our IT World.

In production/live environment, anything is very more complex than we think. Until a day in future, we will need to build so many thing for our project, and we will see that those things has been built by FW completely before.

Although just a RESTful Server, with pure PHP, we need resolve problems with: URI routing, parsing/cleaning request data, data access, dependency management, generating responses, bla bla bla...

I recommend using Slim or Phalcon (Micro App). Slim is a easy and quickly method, but Phalcon is a effective and high performing method.

Burner answered 26/5, 2016 at 8:12 Comment(1)
URI routing, parsing/cleaning request data << mod_rewrite?Charily
G
0

Not necessarily, you don't really need a framework for anything! The framework is made just to provide you with tools to help building your projects faster. These days in most cases it is smarter to use a framework, cause who likes to reinvent the wheel!

Since you are a PHP developer and the best answer suggests to take a look Laravel, I would also suggest that you to take a look at Apiato (apiato.io), this is a PHP API framework built on top of Laravel. It lets you create your API way faster than starting with a vanilla Laravel, as it provides you with tens of features that you already need for every API.

Gerhard answered 31/1, 2020 at 22:36 Comment(0)
V
0

try my new framework, is very simple and ligth, his name is Micron and i hope it will be usefull!

https://github.com/gpisano97/Micron

Valine answered 2/11, 2022 at 0:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.