Announcement

Collapse
No announcement yet.

Help getting started with custom API endpoint?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help getting started with custom API endpoint?

    Sorry,

    I'm stuggling to get a custom API endpoint to work. I was wondering if anyone could advise where I'm going wrong?

    I have the following at:
    custom/Espo/Modules/VenueFundraisers/Resources/routes.json

    PHP Code:
    [
    {
    "route""VenueFundraisers/:venueId",
    "method""get",
    "params": {
    "controller""VenueFundraisers",
    "action""getFundraisersForVenue",
    "venueId"":venueId"
    }
    }

    I've created a controller at:
    custom/Espo/Modules/VenueFundraisers/Controllers/VenueFundraisers.php

    (also tried having controller at: custom/Espo/Custom/Controllers/VenueFundraisers.php)

    PHP Code:
    <?php

    namespace Espo\Modules\VenueFundraisers\Controllers;

    use 
    Espo\Core\Exceptions\NotFoundHttpException;
    use 
    Espo\Core\Exceptions\BadRequestException;
    use 
    Espo\Core\Api\Request;
    use 
    Espo\Core\Api\Response;
    use 
    Espo\Core\Api\ResponseComposer;

    class 
    VenueFundraisers
    {
    public function 
    getFundraisersForVenue(Request $requestResponse $response): void
    {
    $venueId $request->getRouteParam('venueId');
    $response->setBody(ResponseComposer::json($venueId));
    }
    }
    Trying to hit endpoint at:
    https://espocrm-domain.uk/api/v1/VenueFundraisers/65c39997dad3a58a1

    I get 404.

    Any help would be much appreciated.



  • #2
    Hi there,

    First, did you cleared you cache after adding/editing the route inside the .json ?
    If no, execute the
    Code:
    php command.php clear-cache
    and try to reach your endpoint again.

    Then, you should add a "/" at the beginning of your "route" attribute (as shown in the doc https://docs.espocrm.com/development...ction/#example).

    Regards,
    Firyo.

    Comment


    • #3
      try this instead

      custom/Espo/Modules/VenueFundraisers/Resources/routes.json

      PHP Code:
      [
          {
              
      "route""/VenueFundraisers/:venueId",
              
      "method""get",
              
      "params": {
                  
      "controller""VenueFundraisers",
                  
      "action""fundraisersForVenue"
              
      }
          }
      ]
      ​ 

      Then under custom/Espo/Modules/VenueFundraisers/Controllers/VenueFundraisers.php add this
      PHP Code:
      <?php
      namespace Espo\Modules\VenueFundraisers\Controllers;
      use 
      Espo\Core\Exceptions\NotFoundHttpException;
      use 
      Espo\Core\Exceptions\BadRequestException;
      use 
      Espo\Core\Api\Request;
      use 
      Espo\Core\Api\Response;
      use 
      Espo\Core\Api\ResponseComposer;
      class 
      VenueFundraisers
      {
          public function 
      getActionFundraisersForVenue(Request $requestResponse $response): void
          
      {
              
      $venueId $request->getRouteParam('venueId');
              
      // Get collection of your data or whatever
              
      $response->setBody(
                  
      Json::encode(['list' => $collection->getValueMapList()])
              );
              
      // Or return the whole result
              
      return $result->toStdClass();
          }
      }

      Remember you have to set the module in your entity scope e.g if VenueFundraisers is your entity then Create a file custom/Espo/Modules/VenueFundraisers/Resources/metadata/scopes/VenueFundraisers.json
      PHP Code:
      {
          
      "module""VenueFundraisers"
      }​ 

      Not this is a call to a controller, you could achieve same result using api endpoint.

      Hope this helps
      Rabii
      Web Dev

      Comment


      • #4
        Do not use controllers. Use actions. https://docs.espocrm.com/development/api-action/#action. Controllers is an obsolete concept.

        Comment

        Working...
        X