具体5网站API [英] concrete5 website API

查看:35
本文介绍了具体5网站API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在crete5文件管理器中组织了一些分层数据.我想知道是否可以通过其他应用程序(以 API 的方式)从crete5 网站外部访问文件管理器.

I have some hierarchical data which I have organized in the concrete5 filemanager. I'd like to know if it is possible to access the filemanager from outside the concrete5 website by other apps (something in the manner of an API).

这个网站让我希望能有一个答案.不幸的是,没有后续教程.

This website made me hopeful that there could be an answer to this. Unfortunately, there was no followup tutorial.

http://c5hub.com/learning/building-rest-api-using-concrete5-part-1/

我的第二个问题非常相关:是否可以通过作曲家视图做同样的事情来访问页面信息?

My second question is very much related: is it possible to do the same thing to access page information through the composer view?

谢谢

推荐答案

好的,我将根据我认为您需要的内容给出一些基本示例.如果您需要更具体的反馈,请随时提供任何反馈.

Okay, so I'm going to give some basic examples from what I think you need. Feel free to give any feedback if you need it to do some more specific.

第一件事.创建一个包(只是因为它看起来不错,并将所有东西很好地捆绑在一起.
在包控制器中,创建一个名为on_start()"的公共函数.

First things first. Create a package (just because it looks good, and bundles everything nicely together.
In the package controller, create a public function named `on_start()´.

现在决定一个 URL 结构.
我会做一个 url 前缀,我们称之为 api,只是为了清楚地表明您正在访问 API.

Now decide for a URL structure.
I would make a url prefix, let's call it api, just to make it crystal clear that you are accessing the API.

on_start() 函数中,您将添加 API URL,如下所示:

In the on_start() function, you will add the API URLs, like so:

public function on_start() {
    Route::register('/api/foo', 'Concrete\Package\*package-name*\*ClassName*::*function-1*');
    Route::register('/api/bar', 'Concrete\Package\*package-name*\*ClassName*::*function-2*');
}

以上假设您的包中有另一个名为 ClassName 的类,其函数为 function-1()function-2()>.

The above assumes that you have another class in your package named ClassName with the functions function-1() and function-2().

因此,每当您访问 //domain.abc/api/foo 时,ClassName 中的 function-1() 都会被调用.
如果没有启用漂亮的 URL,它将是 //domain.abc/index.php/api/foo

So whenever you access //domain.abc/api/foo function-1() in ClassName will be called.
If pretty URLs aren't enabled, it will be //domain.abc/index.php/api/foo

别担心!您只需在路径中的某处添加 {paramName} 即可.像这样

Don't worry! You just add {paramName} somewhere in the path. Like this

Route::register('/api/foo/{paramName}', 'Concrete\Package\*package-name*\*ClassName*::*function-1*');

然后在函数中加入相同的参数,就变成了function-1($paramName).记住保持名称相同!
参数也可以在url的中间,比如/api/{paramName}/foo.

And then add the same parameter in the function, so it would become function-1($paramName). Remember to keep the names the same!
The parameter(s) can also be in the middle of the url, like /api/{paramName}/foo.

目前似乎没有办法直接在 Concrete5 中传递可选参数.所以我建议你注册几个版本,有和没有可选参数.但是,GitHub 上有一个未解决的问题:此处
作为可选参数的多个 URL 的替代方法,您可以通过请求中的 GETPOST 变量获取它们

Currently it doesn't seems like there is a way to pass optional parameters directly in Concrete5. So I would suggest that you instead register several versions, with and without the optional parameters. However, there is an open issue on this on GitHub: Here
As an alternative to multiple URLs for optional parameters, you could get those via GET or POST variables in the request

<小时>

但我想用 GETPOSTDELETE 等来做那个性感的 REST 事情

我以前没有这样做过,所以这就是我想象中的做法


But I want to do that sexy REST thing with GET, POST, DELETE, etc.

I haven't done this before, so this will just be how I imagine I would do it

对于应该不同作用的 URL,即 GETPOST,首先调用相同的函数.然后,此函数将检查 $_SERVER['REQUEST_METHOD'] 并重定向到准确的 real 函数.

For a URL that should act differently for i.e. GET and POST, start of by calling the same function. This function would then check the $_SERVER['REQUEST_METHOD'] and redirect to accurate real function.

function-2()为例.

function function-2() {
  switch ($_SERVER['REQUEST_METHOD']) {
    case 'PUT':
      $this->function-2_put();  
    break;
    case 'POST':
      $this->function-2_post();  
    break;
    case 'GET':
      $this->function-2_get();  
    break;
    case 'HEAD':
      $this->function-2_head();  
    break;
    case 'DELETE':
      $this->function-2_delete();  
    break;
    case 'OPTIONS':
      $this->function-2_options();    
    break;
    default:
      $this->function-2_error();  
    break;
  }
}

当然你只需要添加适用于特定案例的案例,你可以默认任何你想要的功能.

Of course you only need to add the cases that applies to the specific case, and you can default to any function you want.

我希望这提供了一些见解,您可以与之合作.如果您需要一些更具体的案例,请告诉我.

I hope this gave some insight, that you can work with. Let me know if you need some more specific cases.

这篇关于具体5网站API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆