concrete5 website API

此生再无相见时 提交于 2019-12-10 12:08:23

问题


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


回答1:


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

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


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.



来源:https://stackoverflow.com/questions/33336561/concrete5-website-api

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