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
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.
The best way to do that is to get the data directly from the database.
Within your plugin you can create a file called routes.php
to make routes to your application.
For example you may code something like that in routes.php
<?php
Route::get('api/fetchModel/{id}', function($id){
$data = \Namespace\Pluginname\Models\Model::find($id);
return $data;
});
?>
And for sure you can also redirect your route to a controller inside your plugin. To do that you can create a folder called http
and inside it you can create a folder called controllers
and inside it you can create your controllers.
Example for a route redirecting to a controller.
<?php
Route::get('/welcome/{id}', 'Namespace\Pluginname\Http\Controllers\WelcomeController@index');
?>
And the controller would be like that
<?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;
}
}
And here you can find an example API plugin in octoberCMS: https://github.com/daftspunk/oc-laravelapi-plugin
来源:https://stackoverflow.com/questions/40610006/create-api-web-service-in-octobercms