Announcement

Collapse
No announcement yet.

Creating a Custom Controller in EspoCRM - Need Guidance

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

  • Creating a Custom Controller in EspoCRM - Need Guidance

    Hello everyone !

    My aim is to create fully custom controller and view select custom report data and display with same menu and layout as original website.

    I'm trying to create a custom controller in EspoCRM, and I'm facing some challenges.
    I'd appreciate any guidance or advice on the process.

    Step 1 - Controller
    I created controller in various locations
    Case 1) /var/www/espocrm/custom/CustomController.php​
    Case 2) /var/www/espocrm/custom/Espo/Custom/​CustomController.php​
    Case 3) ​/var/www/espocrm/custom/Espo/Custom/Controllers/CustomController.php​

    Controller content is:

    Code:
    <?php
    namespace Espo\Custom\Controllers;
    use Espo\Core\Utils\QueryBuilder;
    
    class CustomController extends \Espo\Core\Controllers\Base
    {
        public function indexAction()
        {
             $this->checkPermission('read');
             $queryBuilder = new QueryBuilder();
             $queryBuilder->select(['id', 'name', 'custom_field'])
                   ->from('YourEntityName') // Replace 'YourEntityName' with the actual entity name you want to query
                   ->where(['some_condition' => 'some_value']); // Add conditions as needed
             $records = $this->getEntityManager()->getEntityCollection($queryBuilder);
             $this->view->set('records', $records);
             $this->view->render('custom/Views/Custom/custom-view.php');
        }
    }​
    Step2 - added to nginx conf
    Code:
    location / {
       try_files $uri $uri/ /index.php?$query_string;
    }
    As a result - i was unable to see any content under this links
    /index.php/Custom/index
    /index.php/Custom/Custom/index
    /#Custom/index
    /#Custom/Custom/index

    it was or 404 not found or black page or blank layout

    PS: caches where cleared after each modifications
    Last edited by armen; 01-31-2024, 08:00 AM.

  • #2
    Please edit your post to use code tags. It is hard to read.

    You do not need to create multiple controller files and you do not need to edit the configuration of the webserver. Which version of Espo are you using? Go here to read the documentation for custom API actions and controllers.

    Remove the customizations to Nginx. Create one file: custom/Espo/Custom/Controllers/Test.php:
    Code:
    <?php
    
    namespace Espo\Custom\Controllers;
    
    use Espo\Core\Api\Response;
    use Espo\Core\Api\Request;​
    
    class Test extends \Espo\Core\Templates\Controllers\Base
    {
      public getActionHello(Request $request, Response $response) {
        return array("got" => "here");
        # You can also write to the response:
        # $response->writeBody("got here");
      }
    }
    Rebuild from the Administration menu. You should not need to do anything else.


    You must now access it through the API, which means sending an http GET request using curl (command line) or something like Postman (see below):

    Click image for larger version  Name:	Screenshot 2024-01-30 at 08.01.33.png Views:	0 Size:	61.7 KB ID:	102185


    Since I defined the endpoint as a GET request, it is visible from a browser, but this will not be the case with POST, PUT, etc.:

    Click image for larger version  Name:	Screenshot 2024-01-30 at 08.03.04.png Views:	0 Size:	10.7 KB ID:	102186


    You were trying to use /#Controller instead of /api/v1/.... The former is for entities, which are created in the Entity Manager. The latter is for API endpoints, which is what you are defining when you create a controller or action. You can make it much more complicated by adding custom routes, removing authentication, defining actions instead of controllers, and several other things, but start with the basics. If you put things in the right places and name things in the right ways, almost everything is automatically handled on your behalf.
    Last edited by bandtank; 01-30-2024, 04:30 PM.

    Comment


    • #3
      Thanks for help,
      i reproduced your steps and was able to create controller and access it via url and make some dumps.

      But i was not able to render template, my controller now looks like

      Code:
      //  custom/Espo/Custom/Controllers/Dcc.php
      <?php
      
      namespace Espo\Custom\Controllers;
      
      use Espo\Core\Api\Response;
      use Espo\Core\Api\Request;
      
      class Dcc extends \Espo\Core\Templates\Controllers\Base
      {
              public function getActionCaseList(Request $request, Response $response)
              {
                      $records = [['name'=>'aaa']];
                      $this->view->set('records', $records);
                      $this->view->render('custom/Views/Dcc/case-list.php');
              }
      }
      and view

      Code:
      // custom/Espo/Custom/Views/Dcc/case-list.php
      
      <div class="custom-view">
          <h2>Custom View</h2>
          <?php foreach ($records as $record): ?>
              <p><?php echo $record['name']; ?></p>
              <!-- Add any other fields you want to display -->
          <?php endforeach; ?>
      </div>
      i'm getting error
      WARNING: E_WARNING: Undefined property: Espo\Custom\Controllers\Dcc::$view

      PS: i used chatgpt for help but seems it provided me incorrect template rendering syntax
      PPS: my espocrm is Version 8.0.5

      Last edited by armen; 01-31-2024, 08:50 AM.

      Comment


      • #4
        It's just random code, obviously it won't work. I recommend to abstain from using AI and try to figure out by yourself. To be able to write some code that working and to save your time I recommend using an IDE.

        EspoCRM is a single page application. You can't just print HTML on the backend and have it being rendered. The backend API usually respond with a JSON, and a front-end javascript code handles rendering.

        Comment


        • #5
          Actually i'm posting to forum cause of leak of documentation and good/final result samples in google, and as alternative i was using chatgpt ..

          So now i'm at all not able to find way - how do i get logged user data php side

          Any help code or documentation sample would be helpful !
          Last edited by armen; 02-01-2024, 02:07 PM.

          Comment

          Working...
          X