Prestashop 1.7:模块主 PHP 文件中的 “Use” 语句不起作用

huangapple go评论106阅读模式
英文:

Prestashop 1.7: Use statement not working in module main php file

问题

I would like to edit the kpi section on the admin orders page, in Prestashop 1.7, so I created a module, i tried to use a custom module class, in the module's main php file, but i got error

so I created a new CmCustomKpi class, in modules/module_name/src/Kpi/CmCustomKpi.php

namespace Prestashop\Module\CommissionEmployee\Kpi;

use Context;
use HelperKpi;
use PrestaShop\PrestaShop\Kpi\KpiInterface;

/**
* @internal
*/
final class CmCustomKpi implements KpiInterface
{
/**
* {@inheritdoc}
*/
public function render()
{
$translator = Context::getContext()->getTranslator();

$helper = new HelperKpi();
$helper->id = 'box-total-user';
$helper->icon = 'account_box';
$helper->color = 'color1';
$helper->title = $translator->trans('Total users', [], 'Admin.Global');
$helper->subtitle = $translator->trans('30 days', [], 'Admin.Global');
$helper->value = 20;
return $helper->generate();
}
}

In the beginning of my module main php file I added

use Prestashop\Module\CommissionEmployee\Kpi\CmCustomKpi;

And in hook function

public function hookActionOrdersKpiRowModifier(array $params)
{

$params['kpis'][] = new CmCustomKpi();
}

I got this error

Attempted to load class "CmCustomKpi" from namespace "Prestashop\Module\CommissionEmployee\Kpi".
Did you forget a "use" statement for another namespace?
英文:

I would like to edit the kpi section on the admin orders page,in Prestashop 1.7, so i created a module, i tried to use a custom module class, in the module's main php file, but i got error

so I created a new CmCustomKpi class, in modules/module_name/src/Kpi/CmCustomKpi.php

namespace Prestashop\Module\CommissionEmployee\Kpi;

use Context;
use HelperKpi;
use PrestaShop\PrestaShop\Kpi\KpiInterface;

/**
* @internal
*/
final class CmCustomKpi implements KpiInterface
{
/**
* {@inheritdoc}
*/
public function render()
{
$translator = Context::getContext()->getTranslator();

$helper = new HelperKpi();
$helper->id = 'box-total-user';
$helper->icon = 'account_box';
$helper->color = 'color1';
$helper->title = $translator->trans('Total users', [], 'Admin.Global');
$helper->subtitle = $translator->trans('30 days', [], 'Admin.Global');
$helper->value = 20;
return $helper->generate();
}
}

In the beginning of my module main php file I added

**use Prestashop\Module\CommissionEmployee\Kpi\CmCustomKpi;**

And in hook function

public function hookActionOrdersKpiRowModifier(array $params)
{

$params['kpis'][] = new **CmCustomKpi**();
}

I got this error

Attempted to load class "CmCustomKpi" from namespace "Prestashop\Module\CommissionEmployee\Kpi".
Did you forget a "use" statement for another namespace?

答案1

得分: 1

Easy way: 不要使用命名空间。包含您的文件并使用它。

Hard way: 使用Composer创建您的命名空间(You\ModuleName),然后使用您的自定义命名空间。

来自PrestaShop的示例:

{
  "name": "prestashop/ps_mbo",
  "description": "PrestaShop模块ps_mbo",
  "homepage": "https://github.com/PrestaShop/ps_mbo",
  "license": "AFL-3.0",
  "type": "prestashop-module",
  "authors": [
    {
      "name": "PrestaShop SA",
      "email": "contact@prestashop.com"
    }
  ],
  "config": {
    "platform": {
      "php": "5.6.0"
    },
    "preferred-install": "dist",
    "optimize-autoloader": true,
    "prepend-autoloader": false
  },
  "require": {
    "php": ">=5.6.0",
    "ext-simplexml": "*",
    "prestashop/circuit-breaker": "^3.0.0"
  },
  "require-dev": {
    "prestashop/php-dev-tools": "^2.2"
  },
  "autoload": {
    "psr-4": {
      "PrestaShop\\Module\\Mbo\\": "src/"
    },
    "classmap": ["ps_mbo.php"]
  }
}
英文:

Easy way: don't use namespace. Include your file and use it.

Hard way: Create your namespace using composer(You\ModuleName), then use your custom namespace.

Sample from PrestaShop:

{
  "name": "prestashop/ps_mbo",
  "description": "PrestaShop module ps_mbo",
  "homepage": "https://github.com/PrestaShop/ps_mbo",
  "license": "AFL-3.0",
  "type": "prestashop-module",
  "authors": [
    {
      "name": "PrestaShop SA",
      "email": "contact@prestashop.com"
    }
  ],
  "config": {
    "platform": {
      "php": "5.6.0"
    },
    "preferred-install": "dist",
    "optimize-autoloader": true,
    "prepend-autoloader": false
  },
  "require": {
    "php": ">=5.6.0",
    "ext-simplexml": "*",
    "prestashop/circuit-breaker": "^3.0.0"
  },
  "require-dev": {
    "prestashop/php-dev-tools": "^2.2"
  },
  "autoload": {
    "psr-4": {
      "PrestaShop\\Module\\Mbo\\": "src/"
    },
    "classmap": ["ps_mbo.php"]
  }
}

答案2

得分: 1

有很多示例模块在 PrestaShop 的 GitHub 上使用命名空间:
https://github.com/PrestaShop/example-modules

英文:

There are bunch of examples modules on the PrestaShop GitHub using namespaces:
https://github.com/PrestaShop/example-modules

huangapple
  • 本文由 发表于 2023年2月23日 19:51:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定