CakeDC / cakephp-api

API Plugin for CakePHP
Other
61 stars 33 forks source link

Missing \ Wrong documentation on routing #90

Closed rudy1976s closed 2 years ago

rudy1976s commented 3 years ago

The plugin is great but the documentation really lacks and somewhere refers to older api. I am trying to build a basic api in a plugin: i have tried to follow the docs but was unable to make it work. Here there is what I have done so far using cake 4.2.9 :

In my application I have a plugin called GrapeJs : I have created a Service called EditorService under the GrapeJs\Service namespace.

The initialize method maps an action "blocks" for a CustomBlocksAction :

public function initialize(): void
    {
        parent::initialize();
        $this->mapAction('blocks', CustomBlocksAction::class, ['method' => ['GET'], 'mapCors' => true]);
    }

I have created the action under GrapeJs\Api\Service\Action namespace which extends the CakeDC action. Inside the action there is some entity logic to retrieve some data which is returned like the following :

return [
            'blocks' => $result
        ];

The documentation in the overview section ( and not for example in the service ) says that to define a custom route you must implement a "loadRoutes" like the following in the service ( taken from the doc ) :

public function loadRoutes()
    {
        ApiRouter::scope('/', $this->routerDefaultOptions(), function (RouteBuilder $routes) {
            $routes->extensions($this->_extensions);
            $options = [
                'map' => [
                    'publish' => ['action' => 'publish', 'method' => 'POST', 'path' => ''],
                ]
            ];
            $routes->resources($this->name(), $options);
        });
    }

The first problem encountered is that RouteBuilder no longer have a ->extensions. "$this->routerDefaultOptions()" is not defined: should I define it somewhere ? What is the correct way to define this function ? I would expect then to be able to check the api at /grape_js/api/editor/blocks url isn'it ?

Could you please provide a full detailed example of the full flow from the beginning to the end ? There is also some doc link broken, like the "routes" one.

Thank you

Rudy

skie commented 3 years ago

It is possible to map service to specific class which does not follow default naming conventions using api.php config. So this way /blog2/demo mapped to External\Service\BlogsService which loads Demo action classic way using mapAction.

  'Service' => [
      'blog2' => [
          'options' => [
              'className' => \External\Service\BlogsService::class,
              'table' => 'Blogs',
          ],
      ],
  ],

demo is here https://github.com/CakeDC/cakephp-api-cake4-demo

skie commented 2 years ago

Closing as no action.