Skip to content

Add custom endpoints #546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Add custom endpoints #546

wants to merge 1 commit into from

Conversation

sebj54
Copy link
Contributor

@sebj54 sebj54 commented May 31, 2019

Hi there, me again 😁

I find your project very flexible but I missed just one feature: custom endpoints.

I made a very few changes to allow the adding of custom endpoints very easily. It can be done by following a few simple steps.

First, create a controller for your endpoint, like this one:

<?php

namespace MyNamespace; // optional

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Tqdev\PhpCrudApi\Controller\CustomController;
use Tqdev\PhpCrudApi\Middleware\Router\Router;


class MyCustomController extends CustomController
{
    protected function registerRoutes(Router $router)
    {
        $router->register('POST', '/custom/endpoint', [$this, 'customEndpoint']);
    }

    public function customEndpoint(ServerRequestInterface $request): ResponseInterface
    {
        // Insert logic for this endpoint

        return $this->responder->success(1);
    }
}

The most important for this controller is that it should extends CustomController.

Then, the only thing left is to register this controller in the config:

$config = new Config([
    'username' => 'xxx',
    'password' => 'xxx',
    'database' => 'xxx',
    'controllers' => 'records,openapi,custom:\\MyNamespace\\MyCustomController ',
]);

Tell me what you think!

@mevdschee
Copy link
Owner

mevdschee commented Jun 6, 2019

This is a nice addition, that deserves it's own config parameter. One extra question: Can we somehow support openapi specification for custom endpoints?

@sebj54
Copy link
Contributor Author

sebj54 commented Jun 6, 2019

That is a good question, I have actually no knowledge at all about openapi. I assume it describes every API route, especially parameters, right?

You're right about the dedicated config parameter, I will update that!

@mevdschee
Copy link
Owner

@sebj54 I would be very thankful if you would review/test the changes.

mevdschee added a commit that referenced this pull request Sep 3, 2019
@mevdschee
Copy link
Owner

I added customOpenApiBuilders as a config parameter.

@sebj54
Copy link
Contributor Author

sebj54 commented Sep 3, 2019

I confirm it works! Did not test the customOpenApiBuildersthough.

Sorry for the late answer, I spent a few hours trying to debug the issue I just posted (#573). I thought the "Route not found" error was related to the custom endpoints… 🤦‍♂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants