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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
btw, here's the nginx configuration I eventually converted to:
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.