Работайте с автоматической валидацией и DI-инъекцией.
namespace Demo\Module\Controller;
use Bitrix\Main\Engine\Controller;
use Bitrix\Main\Validation\Rule\NotEmpty;
class Post extends Controller
{
public function getAction(
#[NotEmpty]
string $code,
PostService $service,
) {
$entity = $service->getByCode($code);
if (!$entity) {
return $this->renderComponent('demo:error', params: [
'message' => Loc::getMessage('POST_NOT_FOUND'),
]);
}
return $this->renderView('posts/view', [
'entity' => $entity,
]);
}
}



