Created
April 9, 2012 21:48
-
-
Save splittingred/2346752 to your computer and use it in GitHub Desktop.
Example of how to use new REST server class in MODX 2.3+
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteCond %{REQUEST_FILENAME} !-s | |
RewriteRule ^(.*)$ rest/index.php?_rest=$1 [QSA,NC,L] | |
RewriteCond %{REQUEST_FILENAME} -d | |
RewriteRule ^(.*)$ rest/index.php [QSA,NC,L] | |
</IfModule> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Found at: Controllers/Box.php | |
* | |
* Handle requests to [URL]/Controllers/Box. Automagically handles CRUD (GET/POST/PUT/DELETE) for the xPDOObject class myBox. | |
*/ | |
class myControllersBox extends modRestController { | |
public $classKey = 'myBox'; | |
public $defaultSortField = 'name'; | |
public $defaultSortDirection = 'DESC'; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Place in /rest/ directory of site. Add .htaccess file above. Note that our REST controllers are in /rest/Controllers/; they can be anywhere though - just change the basePath config option. | |
*/ | |
/* first load MODX externally */ | |
require_once '/path/to/modx/config.core.php'; | |
require_once MODX_CORE_PATH . 'model/modx/modx.class.php'; | |
$modx = modX::getInstance('rest'); | |
$modx->addPackage('myboxes','/path/to/myboxes/model'); | |
/* now load the REST service */ | |
$rest = $modx->getService('rest','rest.modRestService','',array( | |
'basePath' => dirname(__FILE__).'/Controllers/', | |
'controllerClassSeparator' => '', | |
'controllerClassPrefix' => 'myControllers', | |
'xmlRootNode' => 'response', | |
)); | |
$rest->prepare(); | |
if (!$rest->checkPermissions()) { | |
$rest->sendUnauthorized(true); | |
} | |
$rest->process(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A GET request to our controller at /rest/box.js will output something like this: | |
*/ | |
{ | |
results: [{ | |
id: 1, | |
name: 'A Blue Box', | |
width: 20, | |
height: 40 | |
},{ | |
/* etc */ | |
}] | |
,total: 23 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
A GET request to our controller at /rest/box.xml, however, will output something like this - the class automatically handles XML/JSON output types: | |
--> | |
<response> | |
<results> | |
<result> | |
<id>1</id> | |
<name>A Blue Box</name> | |
<width>20</width> | |
<height>40</height> | |
</result> | |
<result> | |
<!-- etc --> | |
</result> | |
</results> | |
<total>23</total> | |
</response> |
Hi, I was hoping someone would be able to help me, I've followed @BobRay 's tutorial for creating a package and made the amends to my files that @pixelchutes listed and I still can't seem to get it to work. I get the error code when I visit www.domain.com/rest.
{"success":false,"message":"Method not allowed","object":[],"code":405}
But it returns nothing when I visit www.domain.com/rest/box/1
btw, here's the nginx configuration I eventually converted to:
location /rest/ {
try_files $uri @modx_rest;
}
location @modx_rest {
rewrite ^/rest/(.*)$ /rest/index.php?_rest=$1&$args last;
}
EDIT: Updated MODX REST docs
@eveningcoffee, did you ever get it straightened out? It could be a matter of where your .htaccess
rule is placed (assuming you're on Apache), as in my example it was placed inside the /rest/ folder.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Guys, I would like to say thank you for the precious insight on how to implement a custom api. It has been very usefull since I am currently working on angularjs and a restful api is very useful if not necessary for it.
I have managed to make make my way pretty fine so far however I have noticed some few details that maybe someone could help me figure out or provide a fix for it.
First, I have noticed that none of my static snippet reference works.
Fatal error: Call to a member function checkPolicy() on a non-object in C:\wamp\www\pictographix\core\model\modx\modx.class.php on line 1880
This would be very useful for me as when I change environement with git, I don't want to resynchronize everytime my database.
Second I am unable to use some shorthand methods available normally with the modResource class and the modChunk. Therefore, getTVValue() , and getChunk() doesn't work. I have been able to countereact this problem but just to ensure compatibility in my code when I request snippets in my chunks it would be better for those to work.
Cheers
Mark