loading...
Cover image for Using Gila CMS as a back-end service

Using Gila CMS as a back-end service

vzuburlis profile image Vasilis Zoumpourlis ・3 min read

Gila CMS is a content management solution I am building the last few years. It started as an open source project that would give the ability to small businesses to build customized and flexible applications.

One of the advantages is that Gila also works as a headless cms. In this article I will show you how you can create a new database table and how to use the data in your web application. The examples will be shown with the axios library.

To continue you need Gila CMS installed on your server. In the administration menu, go to Administration->Settings and set Environment to "Development", so you can make changes in your code and they apply directly.

The first thing to do is create a new package to add our code. Inside the src folder create a new folder my-movies. Inside the my-movies add the following four files:
package.json
tables/movie.php
load.php
update.php

package.json

{
  "name": "My Movies Package",
  "version": "1.0.0",
  "description": "Adds a table to store your favorite movies"
}

tables/movie.php
In this schema note the permissions attribute: By default, all permissions require the admin user role, but for the read permission we set value true, so the data can be read from axios without a logged in user. More information about table schema you can find in documentation

<?php

return [
  "name"=> "movie",
  "tools"=> ["add"],
  "commands"=> ["edit","delete"],
  "permissions"=>[
      "read"=>true
  ],
  "fields"=> [
  "id"=> [
      "edit"=> "false"
    ],
    "title"=> [
      "qtype"=> "VARCHAR(120)"
    ],
    "score"=> [
      "type"=> "select",
      "options"=> [1=>"1",2=>"2",3=>"3",4=>"4",5=>"5"],
      "qtype"=> "TINYINT DEFAULT 1"
    ],
    "poster"=> [
      "type"=> "media",
      "qtype"=> "VARCHAR(120)"
    ]
  ]
];

load.php

<?php

Gila::content('movie', 'my-movies/tables/movie.php');
Gila::amenu_child('content', ['My Movies','admin/content/movie','icon'=>'play','access'=>'admin']);

update.php
This file runs in activation of the package and will create the table in the database for us

<?php

Gila::content('movie', 'my-movies/tables/movie.php');
$gtable = new gTable('movie');
$gtable->update();

After you have all files in place, you go to /admin/packages and activate the package My Movies

Then in the administration go to Content->My Movies and you will see the new table with the button "+New" to add your movies. Here is a screenshot with a few registries.
Movies table

Now the data that you add in the table, you can get them from your app with ajax requests or promises. If your app runs from a different domain of Gila website you have to set the cors in config.php:

...
"cors"=> [
  0=> 'https://my-app-domain.com'
]
...

Axios requests

Get all movies

axios.get('/cm/list/movie').then(function (response) {
  console.log(response);
})

Response

[
    {
        "id": "3",
        "title": "Captain America: Civil War",
        "score": "4",
        "poster": "assets/movies/captainamericacivilwar_lob_crd_01_9.jpg"
    },
    {
        "id": "2",
        "title": "Ant-Man",
        "score": "3",
        "poster": "assets/movies/ant-man_lob_crd_01_8.jpg"
    },
    {
        "id": "1",
        "title": "Troy",
        "score": "2",
        "poster": "assets/Troy.jpg"
    }
]

Get movie by id

axios.get('/cm/list/movie?id=2').then(function (response) {
  console.log(response);
})

Search movies

axios.get('/cm/list/movie?search=Man').then(function (response) {
  console.log(response);
})

Get movies with score greater than 3, and ordered by title

axios.get('/cm/list/movie?score[gt]=3&orderby[title]=ASC').then(function (response) {
  console.log(response);
})

Get only title and poster of the movies

axios.get('/cm/list/movie?select[]=title&select[]=poster').then(function (response) {
  console.log(response);
})

More information for the /cm endpoint and its parameters you can find in documentation pages.

Posted on by:

vzuburlis profile

Vasilis Zoumpourlis

@vzuburlis

LAMP developer, happy father and ubuntu user.

Discussion

pic
Editor guide