2016-11-15 79 views
1

我是10CMS新手,我發現CMS確實很好。在10月份創建API(Web Service)CMS

我在本地服務器中創建了2個項目。一個在Cakephp(http://localhost/5p_group/),另一個在OctoberCMS(http://localhost/5p_front/)。

我使用Static Pages插件在我OctoberCMS項目(http://localhost/5p_front/),我已經創造了它使用靜態頁面插件頁眉和頁腳的菜單,其在前端,因爲我能夠顯示標題和我的月項目工作正常頁腳菜單分別。

我也使用builder plugin創建了自己的插件,我也能夠在我的OctoberCMS前端顯示數據。

但現在我的要求是讓頁眉,頁腳的菜單,也讓我的插件的數據,以我的CakePHP的項目http://localhost/5p_group/

我想兩者(頁眉頁腳菜單的數據和我的插件數據,存儲在我的數據庫表中)。

,所以我想知道的是OctoberCMS提供無論是在JSON或XML來創建OctoberCMS並調用它使用CURL或這樣的事情http://localhost/5p_front/getHeaderMenuhttp://localhost/5p_front/getFooterMenuhttp://localhost/5p_front/getPluginData我CakePHP的項目,並給予迴應能力的API或Web服務的任何能力?

任何幫助或建議將不勝感激。

謝謝

回答

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插件得到我的標題菜單這裏是我所想出的。

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

此代碼工作,thanx mittul –

+1

@Manishsharma感謝Manish ..希望這可以幫助你:) –

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\[email protected]'); 
?> 

而且控制器將是這樣的

<?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

+0

謝謝你的回答。我也提出了我的解決方案,您可以在我提供的答案中查看。謝謝。 –