diff --git a/app/config/routes.php b/app/config/routes.php index bbebfdf17..03661d043 100644 --- a/app/config/routes.php +++ b/app/config/routes.php @@ -94,6 +94,50 @@ }); +//// API Ajax routes +$routes->scope('/api/ajax/v2', + ['_namePrefix' => 'apiAjaxV2:'], + function (RouteBuilder $builder) { + // Register scoped middleware for in scopes. + // BodyParserMiddleware will automatically parse JSON bodies, but we only + // want that for API transactions, so we only apply it to the /api scope. + $builder->registerMiddleware('bodyparser', new BodyParserMiddleware()); + /* + * Apply a middleware to the current route scope. + * Requires middleware to be registered through `Application::routes()` with `registerMiddleware()` + */ + $builder->setExtensions(['json']); + $builder->applyMiddleware('bodyparser'); + // Use setPass to make parameter show up as function parameter + // Model specific actions, which will usually have more specific URLs: + $builder->post( + '/api_users/generate/{id}', + ['controller' => 'ApiV2', 'action' => 'generateApiKey', 'model' => 'api_users']) + ->setPass(['id']) + ->setPatterns(['id' => '[0-9]+']); + // These establish the usual CRUD options on all models: + $builder->delete( + '/{model}/{id}', ['controller' => 'ApiV2', 'action' => 'delete']) + ->setPass(['id']) + ->setPatterns(['id' => '[0-9]+']); + $builder->get( + '/{model}', + ['controller' => 'ApiV2', 'action' => 'index']); + $builder->get( + '/{model}/{id}', + ['controller' => 'ApiV2', 'action' => 'view']) + ->setPass(['id']) + ->setPatterns(['id' => '[0-9]+']); + $builder->post( + '/{model}', + ['controller' => 'ApiV2', 'action' => 'add']); + $builder->put( + '/{model}/{id}', + ['controller' => 'ApiV2', 'action' => 'edit']) + ->setPass(['id']) + ->setPatterns(['id' => '[0-9]+']); +}); + // Main application routes $routes->scope('/', function (RouteBuilder $builder) { // Register scoped middleware for in scopes. diff --git a/app/templates/element/mveaJs.php b/app/templates/element/mveaJs.php index a0cac1fa8..23533eda9 100644 --- a/app/templates/element/mveaJs.php +++ b/app/templates/element/mveaJs.php @@ -76,7 +76,7 @@ getMveas(mveaType,entityType) { var entityTypeIdRef = entityType + '_id'; let url = 'Url->build(['controller' => 'api', 'action' => 'v2']) + $this->Url->build(['controller' => 'api/ajax', 'action' => 'v2']) ?>/' + mveaType + '?' + entityTypeIdRef + '=&extended'; let xhr = callRegistryAPI( url,