首页 文章

在OctoberCMS中创建API(Web服务)

提问于
浏览
2

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

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

我在我的OctoberCMS项目(http://localhost/5p_front/)中使用了Static Pages插件,并且我使用了 Static Pages Plugin 创建了页眉和页脚菜单,这在我前十月项目中正常工作,因为我能够分别显示页眉和页脚菜单 .

我还使用builder plugin创建了自己的插件,我也可以在OctoberCMS前端显示数据 .

但现在我的要求是获取页眉,页脚菜单以及将我的插件数据提供给我的Cakephp项目http://localhost/5p_group/

我想获取两者的数据(Header footer菜单和存储在我的数据库表中的My plugin数据) .

所以我想知道OctoberCMS是否提供了在OctoberCMS中创建apis或webservices的能力以及使用 CURL 或像这样的http://localhost/5p_front/getHeaderMenuhttp://localhost/5p_front/getFooterMenuhttp://localhost/5p_front/getPluginData在我的Cakephp项目中调用它的能力,并以JSON或XML提供响应?

任何帮助或建议将受到高度赞赏 .

谢谢

2 回答

  • 3

    好的..最终这里是我的工作,从我的一个开发的插件获取其表记录的数据,并获得使用Static Pages插件创建的页眉或页脚菜单 .

    首先,如果您想在OctoberCMS中创建API或Web服务,您需要创建一个插件并创建一个名为 routes.php 的文件,或者您只需在其中一个插件中创建相同的文件即可 .

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

    首先,我想从我的插件中获取数据,该数据使用数据表来存储它..所以我刚刚完成了这个

    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;
    });
    

    而且我能够获得我想要的记录 .

    然后我玩Static Pages插件来获取我的 Header Menu ,这就是我想出的 .

    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项目中获取我创建的 Header Menu 的列表 .

    希望这有助于并感谢您的支持人员 .

    高度赞赏 .

  • 4

    最好的方法是直接从数据库中获取数据 .

    在您的插件中,您可以创建一个名为 routes.php 的文件来创建到您的应用程序的路由 .

    例如,您可以在 routes.php 中编写类似的代码

    <?php
    Route::get('api/fetchModel/{id}', function($id){
        $data = \Namespace\Pluginname\Models\Model::find($id);
        return $data;
    });
    ?>
    

    当然,您也可以将路线重定向到插件内的控制器 . 要做到这一点,你可以创建一个名为 http 的文件夹,在其中你可以创建一个名为 controllers 的文件夹,在其中你可以创建你的控制器 .

    重定向到控制器的路由示例 .

    <?php
         Route::get('/welcome/{id}', 'Namespace\Pluginname\Http\Controllers\WelcomeController@index');
    ?>
    

    而控制器就是这样的

    <?php namespace Namespace\Pluginname\Http\Controllers;
    use Illuminate\Routing\Controller;
    class WelcomeController extends Controller
    {
        /*
        |--------------------------------------------------------------------------
        | Welcome Controller
        |--------------------------------------------------------------------------
        |
        | This controller renders the "marketing page" for the application and
        | is configured to only allow guests. Like most of the other sample
        | controllers, you are free to modify or remove it as you desire.
        |
        */
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            // $this->middleware('guest');
        }
        /**
         * Show the application welcome screen to the user.
         *
         * @return Response
         */
        public function index($id)
        {
            $data = \Namespace\Pluginname\Models\Model::find($id);
            return $data;
        }
    }
    

    在这里你可以在octoberCMS中找到一个示例API插件:https://github.com/daftspunk/oc-laravelapi-plugin

相关问题