Create API (Web Service) in OctoberCMS

微笑、不失礼 提交于 2019-12-02 00:53:31

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!