Reputation: 103
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?
my structure is that :
Module
- foo
- config
services.yml
- src
- Repository
ProductRepository.php
- foo.php
my services.yml
# modules/foo/config/services.yml
services:
product_repository:
class: \Foo\Repository\ProductRepository
arguments: ['@doctrine.dbal.default_connection', '%database_prefix%']
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();
}
}
my 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
Upvotes: 2
Views: 3918
Reputation: 459
For solve this problem you must to use autoload and composer.
Composer:
Install composer if you don't have https://getcomposer.org/
Create composer.json
Create inside the module's folder the file named composer.json and insert the below code
{
"autoload": {
"psr-4": {
"Carbo\\": "classes/"
}
}
}
in this case carbo is my name space and classes is the folder where I will create my classes
Use terminal
open your terminal and go to your module folder and lunch this command:
php composer.phar dump-autoload -a
This will generate a vendor folder, with inside composer folder and autoload.php file.
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'),
);
How to use in app
Create your class in: classes/Helper/Display.php
<?php
namespace Carbo\Helper;
class Display
{
public static function hello($string){
return $string;
}
}
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
And finally use it
Display::hello("Hello there")
For more about this, you can follow this tutorial: https://thewebtier.com/php/psr4-autoloading-php-files-using-composer/
I hope will be useful for you
Upvotes: 4