This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

csavelief7092
csavelief7092

I would love being able to use the exact same interface and way of defining route to create REST API !

Robin
daftspunky
daftspunky

I'm not sure what you mean, this is already possible using Laravel.

Mohsin
Mohsin

OctoberCMS is great for making a REST API. I've made an Android app for a friend with OctoberCMS as the backend and a REST API exposed via a plugin. You basically need to make a plugin for this and then expose the model and it's columns using JSON. The app would make the queries to the different routes to fetch and display data.

numnine
sulaz1x
sulaz1x

@Mohsin is it possible to have access to the code of the android app and the october plugin you've build ? Just to see how it's work and how to implement it ?

jprevot
Mohsin
Mohsin

Hey sorry about that. Haven’t seen this space for a while. You can find code here

Last updated

qspec
qspec

I really appreciate the help you've already given.

If you could clarify (this is my first time ever trying to create an API), what do you mean by, "expose the model"?

After creating the plugin and the component, I'm guessing code (like in your link) goes into the Plugin.php file? Does the default.htm stay empty (I imagine so)?

Anyway, thanks again!

Mohsin
Mohsin

Ugh... I guess that was a bad choice of words.

What I meant to say was that you translate the table into json and hand it to the application requesting the data. So, by exposing the model I mean to say that you present the data to your app so that it is able to get the data it needs from this backend. By default, these tables (or models) are well hidden and cannot be accessed from a browser normally. That is why I wrote a plugin with logic to add routes (or API nodes) from which it can access various tables.

If you see the code you’d notice I’ve done a array('before' => 'auth.basic', function() each time. This is because the app that I made was done in a hurry so minimal security was added. Each time the app sends the username and password unencrypted and OctoberCMS responds are returns the model associated with the requested route in the JSON format ONLY IF the credentials are correct. So it is somewhat secure. A better way to do it is to use some security mechanism such as token exchange (I recommend JWT) so that each request is more secure. Ofcourse you can ignore this for now and do it my way until you get more familiar with how it works.

Last updated

goodleon12473
joelhandwell
joelhandwell

We could learn how October API can be made from other CMS

The API will be the foundation of "Decoupled CMS" or "Headless CMS" ( https://pantheon.io/decoupled-cms )

Last updated

Mohsin
Mohsin

@joelhandwell I’ve written a HOWTO tutorial on this here. This is based on Laravel 5’s way of doing it, so I think OctoberCMS can adopt this technique to create a RestController backend behaviour.

Robin
Robin

Awesome thanks. I like the way on how you seperate your API routes with controllers and not just messing up your routes.php file.

Mohsin
Mohsin

For anyone else trying to do this, check out my plugin which helps to do this quickly.

info14532
info14532

This is a useful extension.

While working on an iOS & Android app, I created my own stripped down version of an API by:

  • creating a plugin with the command line
  • adding a routes file within the plugin
  • mapping a route to a controller method

This is just for the registration postback endpoint as the app requires no additional endpoints. It is built like a standard October website and is simply wrapped with an external service.

If anyone is interested turning your website into an app, I've written a guide on how to Build A Native App With October CMS that explains our workflow.

1-16 of 16

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.