# Creating an API

**URL:** https://community.xibo.org.uk/t/creating-an-api/14502
**Category:** Developer Experience
**Created:** [May 25, 2018, 8:09am UTC](https://community.xibo.org.uk/t/creating-an-api/14502 "2018-05-25T08:09:00Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![reemaRaven](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/reemaraven/32/5239_2.png) [@reemaRaven](https://community.xibo.org.uk/u/reemaRaven)
#### Post date: [May 25, 2018, 8:09am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/1 "2018-05-25T08:09:00Z")

</div>

Hi Alex,

I wanted to know the process of creating a new API.  
I understand the flow as below so far :

1. Create a new module (? can I also create it without module ?) : mod with function func
2. make the controller : lib/controller/con.php
3. create result view as view.twig (within views)
4. add the route to routes-web.php  
like this : $app-\>get(’/mod’, ‘\Xibo\Controller\con:func’)-\>name(‘view’);

Of course I am missing out a lot of things.  
Can you help me correct the method / workflow?

Thanks,  
Reema

---

<div class="post-metadata">

### Author: ![Peter](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/peter/32/8154_2.png) [@Peter](https://community.xibo.org.uk/u/Peter)
#### Post date: [May 25, 2018, 8:32am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/2 "2018-05-25T08:32:31Z")

</div>

Could you specify what exactly do you want to achieve please?

In general custom modules/themes that you can create should go to /custom folder with config file (to avoid issues with upgrading)

You should be able to find required information in the manual pages here - [https://xibo.org.uk/manual/en/advanced.html](https://xibo.org.uk/manual/en/advanced.html)

It looks to me like you’re interested in `extending` page from the manual, however as I don’t know what exactly you want to add I might be wrong there.

---

<div class="post-metadata">

### Author: ![reemaRaven](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/reemaraven/32/5239_2.png) [@reemaRaven](https://community.xibo.org.uk/u/reemaRaven)
#### Post date: [May 25, 2018, 9:13am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/3 "2018-05-25T09:13:00Z")

</div>

I wanted to add a new page in advanced section on the left side (after log).  
Where I want to populate an overview of how many users used which display and which content/layout was watched most.

---

<div class="post-metadata">

### Author: ![reemaRaven](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/reemaraven/32/5239_2.png) [@reemaRaven](https://community.xibo.org.uk/u/reemaRaven)
#### Post date: [May 28, 2018, 5:53am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/4 "2018-05-28T05:53:47Z")

</div>

Hi,

Are there any updates from your end?  
There are no manuals to create API in player end in advanced section.  
At least the windows source code is expected to be customizable.

Thanks,  
Reema

---

<div class="post-metadata">

### Author: ![Peter](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/peter/32/8154_2.png) [@Peter](https://community.xibo.org.uk/u/Peter)
#### Post date: [May 29, 2018, 12:28pm UTC](https://community.xibo.org.uk/t/creating-an-api/14502/5 "2018-05-29T12:28:33Z")

</div>

> [@reemaRaven](#):
>
> Where I want to populate an overview of how many users used which display and which content/layout was watched most.

I’m not sure what you mean by `how many users used which display` as for content on the displays then that’s already reported in the proof of play statistics (which can be called via API as well as web ui).  
Or do you need some other specific data that is not included there?

You certainly could create a new menu item with twig views and controller that would have the function you want then connect that to middleware for webui/api access route - that is explained in `extending` manual page

---

<div class="post-metadata">

### Author: ![reemaRaven](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/reemaraven/32/5239_2.png) [@reemaRaven](https://community.xibo.org.uk/u/reemaRaven)
#### Post date: [June 4, 2018, 9:23am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/6 "2018-06-04T09:23:03Z")

</div>

Okay. I will give you an example of the workflow I want to understand.

1. adding a page on left side panel in design section “tags”
2. which will fetch all tags from the “TAG” table and display in the page  
 ![image](https://community.xibo.org.uk/uploads/default/original/2X/3/35f527944361e6616655d80e37344e292d4db206.png)

I will need to add the twig file also. but there is not documentation for the whole flow which to connect when. would be great help if you could assist.

 ![image](https://community.xibo.org.uk/uploads/default/original/2X/7/778e4992aed8f69fea56671e0f22b0bb013c3b27.png)

---

<div class="post-metadata">

### Author: ![reemaRaven](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/reemaraven/32/5239_2.png) [@reemaRaven](https://community.xibo.org.uk/u/reemaRaven)
#### Post date: [June 5, 2018, 12:43pm UTC](https://community.xibo.org.uk/t/creating-an-api/14502/7 "2018-06-05T12:43:46Z")

</div>

So far I have done below : (creating a new tag page)

1. added a route : $app-\>get(‘/tag/view’, ‘\Xibo\Controller\Tag:displayPage’)-\>name(‘tag.view’);
2. added a new controller tag.php
3. added a view tag-page.twig
4. modified authed-sidebar.twig to contain tags tab

 ![image](https://community.xibo.org.uk/uploads/default/original/2X/6/64227265f1d63c735c8dc40180ccabcd5ee7d20c.png)

clicking it is currently giving 500 error.

---

<div class="post-metadata">

### Author: ![Peter](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/peter/32/8154_2.png) [@Peter](https://community.xibo.org.uk/u/Peter)
#### Post date: [June 5, 2018, 12:59pm UTC](https://community.xibo.org.uk/t/creating-an-api/14502/8 "2018-06-05T12:59:40Z")

</div>

You could open Log page in another tab and see what exactly is causing the problem, at a guess not everything is connected as it should be.

It would be best to have all that custom code in the /custom folder otherwise there might be issues during CMS upgrade.

As such I’d highly recommend creating a custom theme [https://xibo.org.uk/manual/en/advanced\_theme.html](https://xibo.org.uk/manual/en/advanced_theme.html)

and then have a look here, this example should be helpful  
[https://xibo.org.uk/manual/en/advanced\_extending.html#examples](https://xibo.org.uk/manual/en/advanced_extending.html#examples)

---

<div class="post-metadata">

### Author: ![reemaRaven](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/reemaraven/32/5239_2.png) [@reemaRaven](https://community.xibo.org.uk/u/reemaRaven)
#### Post date: [June 6, 2018, 8:36am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/9 "2018-06-06T08:36:09Z")

</div>

Hi Peter,

I followed your references. And created config.php inside web/theme/custom/client/config.php as below :  
$config = array(  
‘theme\_name’ =\> ‘ClientTheme’,  
‘theme\_title’ =\> ‘Client CMS’,  
‘app\_name’ =\> ‘Client CMS’,  
‘theme\_url’ =\> ‘[http://client.com/](http://client.com/)’,  
‘cms\_source\_url’ =\> ‘[http://client.com/](http://client.com/)’,  
‘cms\_install\_url’ =\> ‘[http://client.com/](http://client.com/)’,  
‘cms\_release\_notes\_url’ =\> ‘[http://client.com/](http://client.com/)’,  
**‘view\_path’ =\> PROJECT\_ROOT . ‘/web/theme/custom/client/views’**  
);

as mentioned here :

 ![image](https://community.xibo.org.uk/uploads/default/original/2X/5/5074007b2be554e1eae3e41fb712fa5efa43e334.png)

Loading this said unexpected error.  
Its obviously because config array is not supposed to contain the last parameter. Which config.php should be edited. It seems I am editing the wrong config, wrongly.

---

<div class="post-metadata">

### Author: ![Peter](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/peter/32/8154_2.png) [@Peter](https://community.xibo.org.uk/u/Peter)
#### Post date: [June 6, 2018, 9:01am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/10 "2018-06-06T09:01:22Z")

</div>

That seems correct actually, assuming that `web/theme/custom/client/views` exists and that you have your custom .twig files in it.

> [@reemaRaven](#):
>
> Its obviously because config array is not supposed to contain the last parameter. Which config.php should be edited. It seems I am editing the wrong config, wrongly.

I believe you’re editing correct file ie you’ve created the config.php in your custom theme folder ie `web/theme/custom/client/`

the `$config` array can and in this case should have the `view_path` parameter as you will need to override some twig files.

If you remove the `view_path` then it loads the theme fine, but with it, you get the error after theme change/reload?

Could you perhaps open dev tools and see what the error is exactly please?

---

<div class="post-metadata">

### Author: ![reemaRaven](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/reemaraven/32/5239_2.png) [@reemaRaven](https://community.xibo.org.uk/u/reemaRaven)
#### Post date: [June 6, 2018, 9:43am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/11 "2018-06-06T09:43:05Z")

</div>

> [@Peter](#):
>
> the `$config` array can and in this case should have the `view_path` parameter as you will need to override some twig files.

Overriding would mean that I only need to keep the updated twig file here, and remaining it takes up from the original project root (…/…/views) (correct? or do I need to copy all the other twigs also even though they aren’t changed)  
So, I have copied the authed-sidebar.twig and added the tag param here :

 ![image](https://community.xibo.org.uk/uploads/default/original/2X/e/e9f6bf930b6c85ef5b671b3485c8d64a4f4fb3a5.png)

Ideally, it should work normally (like load the side bar ) and load other things normally. Just with Tag added. But that is not happening.

 ![image](https://community.xibo.org.uk/uploads/default/original/2X/5/55f061efbddba3f3db3dcdf5d6346b3b1d8af002.png)

There are no errors in the dev tool.

 ![image](https://community.xibo.org.uk/uploads/default/original/2X/0/0c086d45f2af148a018dcd2972ecc4d73c112460.png)

Here is my config.php

Earlier (without the “custom” method), when I was changing things directly, I had to add below (other than change in authed-sidebar.twig)

1. :\xampp\htdocs\xibo\lib\Controller\Tag.php:
2. C:\xampp\htdocs\xibo\lib\routes-web.php:  
//$app-\>get(‘/tag/view’, ‘\Xibo\Controller\Tag:displayPage’)-\>name(‘tag.view’);
3. C:\xampp\htdocs\xibo\views\tag-page.twig

Will the custom module already do these for me?  
If not, maybe thats why its not connecting them and doesnt have a route.

---

<div class="post-metadata">

### Author: ![Peter](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/peter/32/8154_2.png) [@Peter](https://community.xibo.org.uk/u/Peter)
#### Post date: [June 6, 2018, 11:35am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/12 "2018-06-06T11:35:23Z")

</div>

The change you made in Custom or to be more specific in the `authed-sidebar` would be enough if you would provide a link to some external webpage or something like that.

However, since you are linking that to a new twig view file, then that file would need to exist in the /custom/views  
and you’d need to have your controller and middleware in /custom as well

Then have the middleware “installed” in settings.php ie [https://xibo.org.uk/manual/en/advanced\_extending.html#examples](https://xibo.org.uk/manual/en/advanced_extending.html#examples)

---

<div class="post-metadata">

### Author: ![reemaRaven](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/reemaraven/32/5239_2.png) [@reemaRaven](https://community.xibo.org.uk/u/reemaRaven)
#### Post date: [June 7, 2018, 5:18am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/13 "2018-06-07T05:18:43Z")

</div>

Hi Peter,

With this in the config.php :  
$config = array(  
‘theme\_name’ =\> ‘client Theme’,  
‘theme\_title’ =\> ‘client solution’,  
‘app\_name’ =\> ‘client solution’,  
‘theme\_url’ =\> ‘[http://client.com/](http://client.com/)’,  
‘cms\_source\_url’ =\> ‘[http://client.com/](http://client.com/)’,  
‘cms\_install\_url’ =\> ‘[http://client.com/](http://client.com/)’,  
‘cms\_release\_notes\_url’ =\> ‘http:/client.com/’  
‘view\_path’ =\> PROJECT\_ROOT . ‘/web/theme/custom/client/views/’  
);  
It throws a 500 error and doesn’t even load the page. I dont think this is the way project root is to be set for a custom theme. When I only have a URL in the side bar, then also it doesnt load.

```
        {% if currentUser.routeViewable("/tag") %}
            <li class="sidebar-list"><a href="http://client.com/estore/">{% trans "Tags" %}</a></li>
        {% endif %}

```

Is this enough file structure when I only want to show a link in side bar that redirects to external page?

![image](https://community.xibo.org.uk/uploads/default/original/2X/d/db4e644771c41656bee41c9a66ee67172b05f141.png)

---

<div class="post-metadata">

### Author: ![Peter](https://community.xibo.org.uk/user_avatar/community.xibo.org.uk/peter/32/8154_2.png) [@Peter](https://community.xibo.org.uk/u/Peter)
#### Post date: [June 7, 2018, 11:55am UTC](https://community.xibo.org.uk/t/creating-an-api/14502/14 "2018-06-07T11:55:19Z")

</div>

I made a little archive for you with very brief example of the modifications you’ve mentioned and controller / middleware connections.

Please see Private Message for more details.
