Hello,
I’m trying to setup my first API in Modx, I’ve followed all the steps in the documentation. When I access the /rest/ folder in the browser the error indicates the API is working fine. However, when I try to access /rest/resources nothing happens and the main homepage loads instead.
Something tells me its related to the .htaccess configuration. I see different information across the web, some say the main root .htaccess is to be modified, others say to create a new .htaccess file inside the /rest/ folder. I’ve tried both but nothing seems to work.
Can someone please point me in the right direction? Here’s what my code looks like
/rest/index.php
<?php
// Boot up MODX
require_once dirname(dirname(__FILE__)) . '/config.core.php';
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error', 'error.modError', '', '');
// Boot up any service classes or packages (models) you will need
$path = $modx->getOption(
'mypackage.core_path',
null,
$modx->getOption('core_path') . 'components/mypackage/'
) . 'model/mypackage/';
$modx->getService('mypackage', 'myPackage', $path);
// Load the modRestService class and pass it some basic configuration
$rest = $modx->getService('rest', 'rest.modRestService', '', array(
'basePath' => dirname(__FILE__) . '/Controllers/',
'controllerClassSeparator' => '',
'controllerClassPrefix' => 'MyController',
'xmlRootNode' => 'response',
));
// Prepare the request
$rest->prepare();
// Make sure the user has the proper permissions, send the user a 401 error if not
if (!$rest->checkPermissions()) {
$rest->sendUnauthorized(true);
}
// Run the request
$rest->process();
/rest/Controllers/Resources.php
<?php
class MyControllerResources extends modRestController
{
public $classKey = 'modResource';
public $defaultSortField = 'sortorder';
public $defaultSortDirection = 'ASC';
}
root .htaccess
#Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# avoid IO on subdir: extras, media, themes, images, assets, connectors and manager
RewriteCond %{REQUEST_URI} !^(/core/|/images/|/assets/|/connectors/|/manager/) [NC]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
#REST server
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]