How to create a controller, routing and views/template in html ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • IMDC
    Junior Member
    • Nov 2015
    • 2

    How to create a controller, routing and views/template in html ?

    Hi ! I am new to EspoCRM and I find it very cool with a very high potential with good structure and good technologies used.

    But I have many questions to be able for develop extensions.

    For example if i create a entity named "Quote" in the admin with fields and relations needed

    File : custom/Espo/Custom/Controllers/Quote.php
    Code:
    namespace Espo\Custom\Controllers;
    
    class Quote extends \Espo\Core\Controllers\Record  {
    
        public function actionInvoice($params) {
    
             die('Hey test dude');
    
        }
    
    }

    File : custom/Espo/Custom/Resources/routes.json
    Code:
    [
      {
          "route":"/Quote/invoice/:id",
           "method":"get",
           "params":{
               "controller":"Quote",
               "action":"invoice",
               "id":":id"
           }
      }
    
    ]


    When I try to access to my die page "hey test dude" with url : /Quote/invoice/[id_of_invoice] I see error 404 .. I tried to flush all cache and rebuild but it does not work .. Cannot access the page that I have created.

    And it is possible too to create HTML template in Resources/layouts/ ? Like for example if I create Quote.html.twig or something ?

    Thank you very much and have a great day !!
  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    Hi

    You can create method

    PHP Code:
    function public getActionInvoice($params, $data, $request) 
    
    in your Controller

    It will be available by GET request api/v1/Quote/actions/invoice

    Get parameters will be available by
    PHP Code:
    $request->get('parameterName'); 
    
    If you need post then you can use postActionInvoice method.

    If you want to use routes you will need to clear cache after changes in routes file.


    You can't use layouts for html templates.






    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment


    • stevengpn
      stevengpn commented
      Editing a comment
      no need to add "s" for "action": api/v1/Quote/actions/invoice
  • bandtank
    Active Community Member
    • Mar 2017
    • 379

    #3
    Is this still the right way to do it? I tried, but it isn't working.

    Here's my /custom/Espo/Custom/Controllers/Timesheet.php file:

    PHP Code:
    <?php
    
    namespace Espo\Custom\Controllers;
    
    class Timesheet extends \Espo\Core\Templates\Controllers\BasePlus
    {
      function public getActionTstest($params, $data, $request) {
        return {"asdf": 5};
      }
    }
    Nothing I've tried has worked. I actually get a status 200 from the API call regardless of which endpoint I try even when it doesn't exist:

    GET /api/v1/timesheet/action/tstest
    GET /api/v1/timesheet/actions/tstest
    GET /api/v1/timesheet/action/not-a-real-endpoint

    The body is empty, the status code is 200, and the headers look normal. What's going on?

    Comment

    • bandtank
      Active Community Member
      • Mar 2017
      • 379

      #4
      After thinking for a few more minutes, I'm wondering if I need to extend another class or if somehow the automatically generated class definition is wrong. Thoughts?

      It looks like the controller should already be setup to handle this, so I don't know what else to try:

      Code:
      116   {
      117     "route":"/:controller/action/:action",
      118     "method":"get",
      119     "params":{
      120         "controller":":controller",
      121         "action":":action"
      122     }
      123   },
      Edit: I found this in the log:

      Code:
      [2017-11-13 16:41:16] Espo.ERROR: Uncaught Exception ParseError: "syntax error, unexpected 'getActionTstest' (T_STRING), expecting '('" at /var/www/crm/custom/Espo/Custom/Controllers/Timesheet.php line 7 {"exception":"[object] (ParseError(code: 0): syntax error, unexpected 'getActionTstest' (T_STRING), expecting '(' at /var/www/crm/custom/Espo/Custom/Controllers/Timesheet.php:7)"} []
      Edit2: Got it. I copied the code from this thread without looking at it closely. 'public' and 'function' were reversed. This worked:

      PHP Code:
      public function getActionTstest($params, $data, $request) {
          return 1234;
      } 
      
      Last edited by bandtank; 11-13-2017, 04:46 PM.

      Comment

      Working...