concrete5网站API [英] concrete5 website API
问题描述
我有我的concrete5文件管理器已经组织了一些分层数据。我想知道是否有可能从其他应用程序(东西API的方式)的concrete5网站外部访问文件管理器。
本网站让我充满希望,有可能是一个答案。不幸的是,没有后续的教程。
http://c5hub.com/learning/building使用-rest-API-concrete5部分-1 /
我的第二个问题是有很大关系:是有可能做同样的事情,通过作曲家视图访问的页面信息
感谢
好了,所以我会从我的给出一些基本的例子觉得的需要。随意给予任何反馈,如果你需要它做一些更具体的。
块引用>首要的事情。创建一个包(只是因为它看起来不错,一切都捆绑在一起很好。结果
在封装控制器,创建一个名为公共职能`on_start()'。现在决定一个URL结构。结果
我会做一个url preFIX,姑且称之为API
,这只是为了清楚地表明您正在访问的API。在
on_start()
功能,您将添加API网址,像这样:公共职能on_start(){
路线::注册('/ API /富,混凝土\\包\\ *包名* \\ *。*类名:: *功能-1 *');
路线::注册('/ API /酒吧,混凝土\\包\\ *包名* \\ *。*类名:: *功能-2 *');
}以上假设你已经在你的包名为
类名
与功能的其他类功能-1()
和功能-2()
。所以每当你访问
// domain.abc/api/foo
功能-1()
在类名
将被调用。结果
如果未启用pretty的URL,这将是// domain.abc/index.php/api/foo
,但我希望能够传递参数的URL的一部分
别担心!你只需要添加
{paramName配置}
某处的路径。像这样路线::注册('/ API /富/ {paramName配置},混凝土\\包\\ *软件包名* \\ *。*类名:: *功能-1 *');
然后添加在功能相同的参数,所以它会成为
功能-1($ paramName配置)
。 记住要保留名称相同的!结果
的参数(S)也可以在URL中,中间像/ API / {paramName配置} /富
。的
目前并不好像有一种方法可以通过直接在Concrete5可选参数。所以我建议您改为注册多个版本,有和没有可选参数。
然而,在这个开放的问题,在GitHub上:这里结果
的作为替代可选参数多个URL,你可以得到通过在GET
或POST
变量那些要求的
块引用>,但我想这样做性感REST的事情与
GET
,POST
,删除
等的我没有做过,所以这将只是我怎么想象我会做的
对于应采取行动的不同作为即
GET
和POST
,启动一个URL通过调用相同的功能。那么该功能将检查$ _ SERVER ['REQUEST_METHOD']
并重定向到准确的真正的功能。例如,让我们来看看
功能-2()
。函数function-2(){
开关($ _ SERVER ['REQUEST_METHOD']){
案把':
$这个 - >函数2_put();
打破;
案POST:
$这个 - >函数2_post();
打破;
案GET:
$这个 - >函数2_get();
打破;
案HEAD:
$这个 - >函数2_head();
打破;
情况下删除:
$这个 - >函数2_delete();
打破;
情况下的选项:
$这个 - >函数2_options();
打破;
默认:
$这个 - >函数2_error();
打破;
}
}当然,你只需要添加一个适用于具体案件的情况下,您可以默认为任何你想要的功能。
我希望这给了一些见解,你可以工作。让我知道如果你需要一些更具体的情况。
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?
Thanks
解决方案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.
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()´.Now decide for a URL structure.
I would make a url prefix, let's call itapi
, just to make it crystal clear that you are accessing the API.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*'); }
The above assumes that you have another class in your package named
ClassName
with the functionsfunction-1()
andfunction-2()
.So whenever you access
//domain.abc/api/foo
function-1()
inClassName
will be called.
If pretty URLs aren't enabled, it will be//domain.abc/index.php/api/foo
But I want to be able to pass parameters as part of the URL
Don't worry! You just add
{paramName}
somewhere in the path. Like thisRoute::register('/api/foo/{paramName}', 'Concrete\Package\*package-name*\*ClassName*::*function-1*');
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
.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 viaGET
orPOST
variables in the request
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
For a URL that should act differently for i.e.
GET
andPOST
, start of by calling the same function. This function would then check the$_SERVER['REQUEST_METHOD']
and redirect to accurate real function.For example, let's look at
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.
这篇关于concrete5网站API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!