Skip to content

Commit

Permalink
Create api/ajax/v2 route
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis Igoumenos committed Sep 13, 2023
1 parent 2d0577a commit beff6aa
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
47 changes: 45 additions & 2 deletions app/config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,52 @@

//// API routes
$routes->scope('/api/v2', 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]+']);
});


//// API Ajax routes
$routes->scope('/api/ajax/v2',
['_namePrefix' => 'apiAjaxV2:'],
function (RouteBuilder $builder) {
$builder->registerMiddleware('csrf', new CsrfProtectionMiddleware(['httponly' => true]));
// 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());
Expand Down Expand Up @@ -93,7 +137,6 @@
->setPatterns(['id' => '[0-9]+']);
});


// Main application routes
$routes->scope('/', function (RouteBuilder $builder) {
// Register scoped middleware for in scopes.
Expand Down
2 changes: 1 addition & 1 deletion app/templates/element/mveaJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
getMveas(mveaType,entityType) {
var entityTypeIdRef = entityType + '_id';
let url = '<?=
$this->Url->build(['controller' => 'api', 'action' => 'v2'])
$this->Url->build(['controller' => 'api/ajax', 'action' => 'v2'])
?>/' + mveaType + '?' + entityTypeIdRef + '=<?php print $parentId ?>&extended';
let xhr = callRegistryAPI(
url,
Expand Down

0 comments on commit beff6aa

Please sign in to comment.