在 OctoberCMS 中创建 API(Web 服务) [英] Create API (Web Service) in OctoberCMS

查看:16
本文介绍了在 OctoberCMS 中创建 API(Web 服务)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OctoberCMS 的新手,我发现它确实是非常好的 CMS.

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

我正在本地服务器中创建 2 个项目.一个在 Cakephp (http://localhost/5p_group/) 中,另一个在 OctoberCMS (http://localhost/5p_front/) .

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/) .

我在我的 OctoberCMS 项目中使用 Static Pages 插件href="http://localhost/5p_front/" rel="nofollow noreferrer">http://localhost/5p_front/) 并且我使用 静态页面插件 在我的 10 月项目前端工作正常,因为我能够分别显示页眉和页脚菜单.

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.

我还使用 builder 插件 创建了自己的插件,并且我也能够显示我的 OctoberCMS 前端中的数据.

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

但现在我的要求是获取页眉、页脚菜单以及将我的插件数据获取到我的 Cakephp 项目 http://本地主机/5p_group/

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).

所以我想知道十月CMS是否提供了在OctoberCMS中创建apis或webservices的任何能力,以及使用CURL或类似的东西在我的Cakephp项目中调用它的能力http://localhost/5p_front/getHeaderMenu, http://localhost/5p_front/getFooterMenuhttp://localhost/5p_front/getPluginData 和以 JSON 或 XML 形式给出响应?

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.

谢谢

推荐答案

好的伙计们 .. 最终我的工作是从我开发的插件之一及其表格记录中获取数据,并使用它来获取页眉或页脚菜单使用 静态页面 插件创建.

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.

首先,如果您想在 OctoberCMS 中创建 API 或 webservice,您需要创建一个插件并创建一个名为 routes.php 的文件,或者您可以简单地在一个文件中创建相同的文件你的插件.

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.

所以我现在只是在我开发的插件之一中创建了 routes.php 文件,以测试并使我的 Web 服务暂时运行.

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.

然后我使用 Static Pages 插件来获取我的标题菜单 这是我想出的.

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;

});

这将简单地获取我在我的 OctoberCMS 项目中创建的标题菜单的列表.

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

希望这会有所帮助并感谢您的支持.

Hope this helps and thanks for your support guys.

高度赞赏.

这篇关于在 OctoberCMS 中创建 API(Web 服务)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆