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

问题描述

限时删除!!

我有我的concrete5文件管理器已经组织了一些分层数据。我想知道是否有可能从其他应用程序(东西API的方式)的concrete5网站外部访问文件管理器。

本网站让我充满希望,有可能是一个答案。不幸的是,没有后续的教程。

我的第二个问题是有很大关系:是有可能做同样的事情,通过作曲家视图访问的页面信息

感谢


解决方案

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 it api, 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 functions function-1() and function-2().

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


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 this

Route::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.


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 and POST, 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:49