Prestashop 1.7.4-命名空间模块错误 [英] Prestashop 1.7.4 - Module error with namespace

查看:138
本文介绍了Prestashop 1.7.4-命名空间模块错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个未来的项目留学prestashop.我按照文档创建模块

i'm studding prestashop for a futur project. I follow the documentation for create a module

https://devdocs. prestashop.com/1.7/modules/concepts/hooks/use-hooks-on-modern-pages/

但是当我按照所有步骤操作时,会出现此错误:

But when i follow all steps i have this error :

Attempted to load class "ProductRepository" from namespace "Foo\Repository". Did you forget a "use" statement for another namespace?

我的结构是:

模块

- foo
    - config 
        services.yml
    - src
        - Repository
            ProductRepository.php
    - foo.php

我的services.yml

my services.yml

# modules/foo/config/services.yml

services:
    product_repository:
        class: \Foo\Repository\ProductRepository
        arguments: ['@doctrine.dbal.default_connection', '%database_prefix%']

我的ProductRepository.php

my ProductRepository.php

<?php

// src/Repository/ProductRepository.php
namespace Foo\Repository;

use Doctrine\DBAL\Connection;

class ProductRepository
{
    /**
     * @var Connection the Database connection.
     */
    private $connection;

    /**
     * @var string the Database prefix.
     */
    private $databasePrefix;

    public function __construct(Connection $connection, $databasePrefix)
    {
        $this->connection = $connection;
        $this->databasePrefix = $databasePrefix;
        dump('ok');
    }

    /**
     * @param int $langId the lang id
     * @return array the list of products
     */
    public function findAllbyLangId($langId)
    {
        $prefix = $this->databasePrefix;
        $productTable = "${prefix}product";
        $productLangTable = "${prefix}product_lang";

        $query = "SELECT p.* FROM ${productTable} p LEFT JOIN ${productLangTable} pl ON (p.`id_product` = pl.`id_product`) WHERE pl.`id_lang` = :langId";
        $statement = $this->connection->prepare($query);
        $statement->bindValue('langId', $langId);
        $statement->execute();

        return $statement->fetchAll();
    }
}

我的foo.php

<?php

if (!defined('_PS_VERSION_')) {
    exit;
}

class Foo extends Module
{
    public function __construct()
    {
        $this->name = 'foo';
        $this->tab = 'front_office_features';
        $this->version = '1.0.0';
        $this->author = 'Jordan NativeWeb';
        $this->need_instance = 0;
        $this->ps_versions_compliancy = [
            'min' => '1.6',
            'max' => _PS_VERSION_
        ];
        $this->bootstrap = true;

        parent::__construct();

        $this->displayName = $this->l('Foo');
        $this->description = $this->l('2eme module');

        $this->confirmUninstall = $this->l('Etes vous sûr de vouloir supprimer ce module ?');

        if(!Configuration::get('MYMODULE_NAME')) {
            $this->warning = $this->l('Aucun nom trouvé');
        }
    }

    /**
     * Module installation.
     *
     * @return bool Success of the installation
     */
    public function install()
    {
        return parent::install() && $this->registerHook('displayDashboardToolbarIcons');
    }

    /**
     * Add an "XML export" action in Product Catalog page.
     *
     */
    public function hookDisplayDashboardToolbarIcons($hookParams)
    {
        if ($this->isSymfonyContext() && $hookParams['route'] === 'admin_product_catalog') {
            $products = $this->get('product_repository')->findAllByLangId(1);
            dump($products);
        }
    }

    public function uninstall()
    {
        if (!parent::uninstall() ||
            !Configuration::deleteByName('MYMODULE_NAME')
        ) {
            return false;
        }

        return true;
    }

}

您看到不好的东西并且可以解释该错误吗?我尝试过但是什么都没找到... 我先谢谢你

Do you see something who is bad and can explain the error ? I try but i don't find anything... I thank you by advance

推荐答案

要解决此问题,必须使用自动加载和作曲器.

For solve this problem you must to use autoload and composer.

作曲家:

如果没有,请安装作曲家 https://getcomposer.org/

Install composer if you don't have https://getcomposer.org/

创建composer.json

在模块的文件夹中创建名为composer.json的文件,并插入以下代码

Create inside the module's folder the file named composer.json and insert the below code

{
  "autoload": {
    "psr-4": {
      "Carbo\\": "classes/"
    }
  }
}

在这种情况下, carbo 是我的名字空间, classes 是我将在其中创建课程的文件夹

in this case carbo is my name space and classes is the folder where I will create my classes

使用终端

打开终端并转到模块文件夹,然后用以下命令吃午餐:

open your terminal and go to your module folder and lunch this command:

php composer.phar dump-autoload -a

这将生成一个供应商文件夹,其中包含composer文件夹和autoload.php文件.

This will generate a vendor folder, with inside composer folder and autoload.php file.

在composer文件夹内的autoload_psr4.php中

in autoload_psr4.php inside the composer folder

<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'Carbo\\' => array($baseDir . '/classes'),
);

如何在应用中使用

在以下位置创建您的课程:classes/Helper/Display.php

Create your class in: classes/Helper/Display.php

<?php

namespace Carbo\Helper;

class Display
{
    public static function hello($string){
        return $string;
    }
}

  • 命名空间:Carbo
  • 文件夹:助手
  • 类别名称:显示
  • 打开您的主文件,并在类声明之前包含autoload.php

    open your main file and include autoload.php before the class declaration

    require_once __DIR__.'/vendor/autoload.php';
    

    现在您可以加入您的课程

    now you can include your classes

    use Carbo\Helper\Display; // Namespace - folder - class name
    

    最后使用它

    Display::hello("Hello there")
    

    有关此的更多信息,您可以按照本教程进行操作: > https://thewebtier.com/php/psr4-autoloading-php -files-using-composer/

    For more about this, you can follow this tutorial: https://thewebtier.com/php/psr4-autoloading-php-files-using-composer/

    希望对您有用

    这篇关于Prestashop 1.7.4-命名空间模块错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆