christian.giupponi's avatar

Add API to existing Laravel project

Hi, I'd like to know how to add API for an existing project.

The only think that comes to my mind was return a json instead a view, but how to manage authentication?

What is the best workflow in this type of situation?

0 likes
7 replies
jlrdw's avatar

Saying API is a very broad thing, perhaps you could be more specific as to what you are trying to accomplish.

christian.giupponi's avatar

Thanks jlrdw, I have read that topic but didn't solve my problem. Already exists a mobile versione of the site with media queries but the client required me to develop native app, so I need to create a api to connect the mobile app to the site ( login - registration - profile - CRUD )

If I divide API and web routes should I write two times my application and I think that in this way I can miss some functionalities but if it the best way to do it I will.

Any other option?

willvincent's avatar

I think ideally you'll want dedicated api routes that use similar functionality. What that might look like in practice would be stripping logic out of your controllers into reusable traits so that your controller methods simply call the appropriate trait method to get data, and the controller responds either with a view template for your existing routes or with json for new api routes.

As for authentication, you'll likely want to use something like JWT

1 like
rouckay's avatar

I have the same issue I can't able to write the logic for my app from scratch is there any way to generate the API from existance controller?

martinbean's avatar

@rouckay Dude. This thread was almost a decade old đŸ˜©

If you want to add an API to your app then add routes and controllers. You can’t “magically” create an API from web controllers because you’re unlikely to have a one-to-one mapping between web and API functionality. Think of a home page. It’s probably going to query and display multiple types of models, so isn’t going to map to a single, resourceful API endpoint nicely.

Please or to participate in this conversation.