Poor-man's web service in C5?

Permalink
I'm VERY new to Concrete5. I'm trying to figure out how to implement a simple service that uses regular old HTTP GET or POST to invoke a method on a controller. There needn't be a view in this case, since no actual page is being constructed, as long as the method can return a simple string value in the HTTP response. I've done this sort of thing in other MVC frameworks, and it's usually possible to formulate the request as a URL, like "http://myserver.com/mycontroller/some_method/methodArg1/methodArg2" etc.

thanks in advance!

-Lane Wimberley
Austin, TX

 
jordanlev replied on at Permalink Reply
jordanlev
I think the best way to do this is to create a "single page" that responds to the HTTP GET or POST request. There actually is a view -- but the view is just the string value you want to respond with (no html, head, body, etc. etc. tags in it).
The controller for this single page can respond to the request, pass the return value to the "view", and the view just outputs that return value (like literally the only code in the view file is <?php echo $myResponseString; ?>).

Here's the documentation for creating single pages:
http://www.concrete5.org/help/building_with_concrete5/developers/mv...

Note the part where it talks about customizing the output so it isn't wrapped in the standard header and footer -- that's what you want to do in this case.

Hope that makes sense!

-Jordan
bikelane replied on at Permalink Reply
Thanks, Jordan. This is very helpful.

It had occurred to me that a view would be a simple way to return a value. And\
, conceptually, it makes sense: this is the "view" that the client would have o\
f the service. The only thing I wasn't sure about was how to avoid the view re\
turning all the header and footer info, and you've answered that.

I also figured out (I think) that a method can be simply invoked on a controlle\
r via an HTTP GET request of the form,http://myserver/mycontroller/-/mymethod/\...
arg1/arg2/

Again, thanks for the helpful reply.

-Lane Wimberley
Austin, TX
ScottC replied on at Permalink Reply
ScottC
here's what you do.

create the php file you want to make get requests to, say rss.php, and place that in siteroot/tools/

open up your rss.php, and just put in something to get it started like.

$db = Loader::db();  //make sure we are running through the dispatcher and concrete5 stuff is available to me.
print_r($_GET);  //your args here will print out


so now you have some code that does something, but how the heck do you link to it? Easy.

Loader::helper('concrete/urls');
$link = ConcreteUrlsHelper::getToolsUrl('rss'); //note no .php
?>
<a href="<?php echo $link ?>">click for magic</a>


that's it!
bikelane replied on at Permalink Reply
Thanks, Scott.

Out of curiosity, I tried this out. But, the code in my "tools" implementation crashes. None of the C5 stuff is defined by default. For example, C5_EXECUTE is not defined.

As I said, I'm pretty new to C5. Am I missing something? What do I need to do to make sure that any code inside something in the tools directory has access to the C5 environment?

Thanks.
ScottC replied on at Permalink Reply
ScottC
make sure you access the tools through the path provided by the url helper? I didn't explicitly check for C5_EXECUTE on my tools(not block tools) since i intend for this to be used without being logged in.

I used Loader::model('class'); loading and it worked.

I only used this for a quote engine basically which just returns json.
jordanlev replied on at Permalink Reply
jordanlev
Looking at this thread:
https://www.concrete5.org/index.php?cID=27801...

Seems like you can just put this at the top of your script:
<?php
define('C5_ENVIRONMENT_ONLY', true);
include("../index.php");


Unfortunately I can't test whether or not that works right now, but it's worth a shot.