0

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

pnuts
  • 58,317
  • 11
  • 87
  • 139
Suemayah Eldursi
  • 969
  • 4
  • 14
  • 36
  • As far as I know, you will have to make your own API within C5 for this. You can't make a direct connection to the file manager from outside C5, but you can expose it through an interface you make yourself. – Nicolai Krüger Oct 26 '15 at 14:28
  • I can give you a few lines to show how to build an API. It won't be secure, nor will it be very complicated :) – Nicolai Krüger Oct 26 '15 at 14:29
  • @NicolaiDitlevKroghKrüger First of all thank you very much for answering. Yes please I'd really like to take a look at what you have in mind. – Suemayah Eldursi Oct 27 '15 at 02:38
  • Do you think the same thing can be done to the website pages. I'm working on a project where the website is being managed by people who can't deal with any back end. So they want any changes made by them to the website to be stored and accessible by third parties.. SO in a way its like they want to use the website to manipulate a database – Suemayah Eldursi Oct 27 '15 at 02:40
  • For example they have a bunch of pages organized in a hierarchical manner each page talks about something specific and has a specific image and other specific content. They want to be able to add pages and page content and that this be reflected in a DB so that external webapps can have access as well . I'm not sure if it's very clear but I'd appreciate any feedback from you – Suemayah Eldursi Oct 27 '15 at 02:40

1 Answers1

3

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.

Nicolai Krüger
  • 416
  • 4
  • 17
  • Happy to help, and welcome to Stack Overflow. If this answer your issue, please mark it as accepted. If you still have questions, just ask :) – Nicolai Krüger Oct 29 '15 at 12:18