【问题标题】:Create API (Web Service) in OctoberCMS在 OctoberCMS 中创建 API(Web 服务)
【发布时间】:2017-03-29 08:17:18
【问题描述】:

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

我正在本地服务器中创建 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/

我想获取两者的数据(页眉页脚菜单和存储在我的数据库表中的我的插件数据)。

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

任何帮助或建议将不胜感激。

谢谢

【问题讨论】:

    标签: api octobercms


    【解决方案1】:

    好的,伙计们 .. 最终,我的工作是从我开发的插件及其表记录中获取数据,并使用使用 Static Pages 插件创建的页眉或页脚菜单。

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

    所以我现在只是在我开发的插件之一中创建了 routes.php 文件来测试并让我的网络服务暂时运行。

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

    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 插件来获取我的标题菜单,这就是我想出的。

    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 项目中创建的 标题菜单 的列表。

    希望这对您有所帮助并感谢您的支持。

    高度赞赏。

    【讨论】:

      【解决方案2】:

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

      在您的插件中,您可以创建一个名为 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

      【讨论】:

      • 感谢您的回答。我还提出了我的解决方案,您可以查看我提供的答案。谢谢。
      猜你喜欢
      • 1970-01-01
      • 2018-07-09
      • 1970-01-01
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      相关资源
      最近更新 更多