www.hubleto.eu Reddit User Guide Stack Overflow LinkedIn Report an issue

Controller

A controller handles one URL. Hubleto has three kinds of controllers. All extend Hubleto\Erp\Controller and differ in what they return.

Kind Folder Returns Base class
CRUD / page controller Controllers/ HTML (a Twig view) Hubleto\Erp\Controller
API controller Controllers/Api/ JSON Hubleto\Erp\Controllers\ApiController
Board controller Controllers/Boards/ HTML of a dashboard panel Hubleto\Erp\Controller

Kinds of controllers.

Properties

Property Default Meaning
$permission full class name with / The permission checked before the controller runs.
$requiresAuthenticatedUser true Set false for public pages (the sign-in page).
$permittedForAllUsers false true for API controllers: any signed-in user.
$hideDefaultDesktop false Render only the view, without the desktop around it.
$view "" The Twig view to render. Set it with setView('@Hubleto:App:Community:Customers/Customers.twig').
$viewParams URL parameters Variables of the view (getViewParams()).

Properties.

Methods

Method Purpose
init() Called once after the controller is created.
prepareView() Fills $viewParams. Call parent::prepareView() first.
setView($view) Chooses a different view.
getBreadcrumbs() The breadcrumbs shown above the page.
renderJson() API controllers: returns the array that is encoded to JSON.
render() Renders the view. You rarely override it.

Methods.

Examples

A page controller:

apps/Customers/Controllers/Customers.php
<?php

namespace Hubleto\App\Community\Customers\Controllers;

class Customers extends \Hubleto\Erp\Controller
{

  public function prepareView(): void
  {
    parent::prepareView();
    $this->setView('@Hubleto:App:Community:Customers/Customers.twig');
  }

}

An API controller. The base class turns exceptions into { "status": "error", ... }:

apps/Deals/Controllers/Api/LogActivity.php
<?php

namespace Hubleto\App\Community\Deals\Controllers\Api;


use Hubleto\App\Community\Deals\Models\Deal;
use Hubleto\App\Community\Deals\Models\DealActivity;

class LogActivity extends \Hubleto\Erp\Controllers\ApiController
{
  public function renderJson(): array
  {
    $idDeal = $this->router()->urlParamAsInteger("idDeal");
    $activity = $this->router()->urlParamAsString("activity");
    if ($idDeal > 0 && $activity != '') {
      /** @var Deal */
      $mDeal = $this->getModel(Deal::class);

      $deal = $mDeal->record->find($idDeal)->first()?->toArray();

      if ($deal && $deal['id'] > 0) {
        /** @var DealActivity */
        $mDealActivity = $this->getModel(DealActivity::class);
        $mDealActivity->record->recordCreate([
          'id_deal' => $idDeal,
          'subject' => $activity,
          'date_start' => date('Y-m-d'),
          'time_start' => date('H:i:s'),
          'all_day' => true,
          'completed' => true,
          'id_owner' => $this->authProvider()->getUserId(),
        ]);
      }
    }

    return [
      "status" => "success",
      "idDeal" => $idDeal,
    ];
  }

}

A board controller:

apps/Deals/Controllers/Boards/MostValuableDeals.php
<?php

namespace Hubleto\App\Community\Deals\Controllers\Boards;

use Hubleto\App\Community\Deals\Models\Deal;

class MostValuableDeals extends \Hubleto\Erp\Controller
{
  public bool $hideDefaultDesktop = true;

  public function prepareView(): void
  {
    parent::prepareView();

    /** @var Deal */
    $mDeal = $this->getModel(Deal::class);

    $mostValuableDeals = $mDeal->record->prepareReadQuery()
      ->with('CURRENCY')
      ->orderBy("price", "desc")
      ->offset(0)
      ->limit(5)
      ->get()
      ->toArray()
    ;

    $this->viewParams['mostValuableDeals'] = $mostValuableDeals;

    $this->setView('@Hubleto:App:Community:Deals/Boards/MostValuableDeals.twig');
  }

}

Reading the request

$idCustomer = $this->router()->urlParamAsInteger('idCustomer');
$search     = $this->router()->urlParamAsString('search');
$isAdd      = $this->router()->isUrlParam('add');

URL parameters, variables of the route ((?<recordId>\d+)) and the data of a POST request are all read with urlParamAs...().

Related pages