Create API (Web Service) in OctoberCMS

- 1 answer

Ad

I am new to OctoberCMS and i found it very good CMS indeed.

I am creating 2 projects in my local server. One is in Cakephp (http://localhost/5p_group/) and the other is in OctoberCMS (http://localhost/5p_front/) .

I am using Static Pages plugin in my OctoberCMS project (http://localhost/5p_front/) and i have created header and footer menus using Static Pages Plugin in it which works fine in my October Project at front end as i am able to display header and footer menus respectively.

I have also created my own plugin using builder plugin and i am also able to display data in my OctoberCMS front end.

But now my requirement is to get the header, footer menus and also to get data of my plugin to my Cakephp project http://localhost/5p_group/

I want to get the data of both (Header footer menus and My plugin data which stored in my database table).

So i wanted to know is OctoberCMS provides any ability to create apis or webservices in OctoberCMS and ability to call it in my Cakephp project using CURL or something like this http://localhost/5p_front/getHeaderMenu, http://localhost/5p_front/getFooterMenu, http://localhost/5p_front/getPluginData and give response either in JSON or XML ?

Any help or suggestions will be highly appreciated.

Thanks

Ad

Answer

Ad

Ok guys .. eventually here is my work around to get data from one of my developed plugin with its table records and to get Header or footer menus using which is created using Static Pages plugin.

First thing first, If you want to create an API or webservice in OctoberCMS, you need to create a plugin and create a file called routes.php or you can simply create the same file in one of your plugins.

So i simply created routes.php file in one of my developed plugins for now to test and make my web services running for now.

First i wanted to get data from my plugin which is using datatable table to store it .. so i have just done this

routes.php

use technobrave\sociallinks\Models\Sociallink;

Route::post('/getSocialLinks', function () {

    $social_links_data = Sociallink::all();

    $arr = array();
    foreach($social_links_data as $current_social_links_data)
    {       
        $arr[] = array(
                'id'=> $current_social_links_data['id'],
                'social_logo'=> $current_social_links_data->social_logo->getPath()
                );
    }
    return $arr;
});

And i am able to get records which i wanted.

Then i played with Static Pages plugin to get my Header Menu and here is what i have come up with.

routes.php

/* Code to get menu item starts */ 
use Cms\Classes\ComponentBase;
use RainLab\Pages\Classes\Router;
use Cms\Classes\Theme;
use RainLab\Pages\Classes\Menu as PagesMenu;
/* Code to get menu item ends */ 

Route::post('/getHeaderMenu', function () 
{


    $menuCode = 'main-menu'; // menu code 
    $theme = Theme::getActiveTheme();


    $menu = PagesMenu::loadCached($theme, $menuCode);

    $header_menu_list = array();
    if ($menu) 
    {
        $menu_list = $menu->attributes['items'];
        if($menu_list)
        {
            $i=0;
            foreach ($menu_list as $current_menu_list) 
            {

                if($current_menu_list->reference == '')
                {
                    $current_menu_list->reference = "#";
                }
                $header_menu_list[$i] = array(
                                            'title'=>$current_menu_list->title,
                                            'url'=>$current_menu_list->reference,
                                        );

                $header_menu_list[$i]['submenu_list'] = array();


                if($current_menu_list->items)
                {

                    $sub_menu_list = $current_menu_list->items;
                    foreach ($sub_menu_list as $current_submenu_list) 
                    {
                        if($current_submenu_list->reference == '')
                        {
                            $current_submenu_list->reference = "#";
                        }


                        $header_menu_list[$i]['submenu_list'][] = array(
                                                                'title'=>$current_submenu_list->title,
                                                                'url'=>$current_submenu_list->reference,
                                                            );    
                    }

                }
                $i++;
            }
        }

    }    
    return $header_menu_list;

});

This will simply get the list of my created Header Menu in my OctoberCMS project.

Hope this helps and thanks for your support guys.

Highly Appreciated.

Ad
source: stackoverflow.com
Ad