I recently decided to develop a new website with drupal 8 (i never used drupal 7 or other version).
I had to create a module and i need a custom class in this module and i create a service to use it. But i've an error, my custom class is not find.
When i had a require of my class in the autoload.php, it's work. So my service is correct but my class is not include in my autoload.
Anyone have a solution for my problem ?
Do you "use" the class? Like that:
use Drupal\your_namespace\yourClass;
Of course your class needs a namespace to make this work.
you need follow PSR4 rule, if your class is defined in module, you should put your class file into the correct folder structure , for example if your class namespace defined as "Drupal\your_module_name\yourClass" , the file folder structure should be: modules\your module name\src\ (your Class file)
Related
I have installed a custom module in my Magento website but now, when I try to reach the frontend of my website I receive an error. Class 'ShopgateConfig' not found.
PHP Fatal error: Class 'ShopgateConfig' not found in C:\\xampp\\htdocs\\blugento\\.modman\\shopgate_magento_integration\\src\\app\\code\\community\\Shopgate\\Framework\\Model\\Config.php on line 42, referer: blugento.local/index.php/admin/system_config/index/key/....
But the class exists because I found it in one of the module files. This is the code where the class is called:
class Shopgate_Framework_Model_Config extends ShopgateConfig{...}
And there is the implementatin of the class:
class ShopgateConfig extends ShopgateContainer implements ShopgateConfigInterface{...}
Which could be the problem? Do I have to change something in the config.xml file or somewhere to find my class?
If is necessary I'll post more of my code if you'll tell me!
Thank you very much!
Do do refresh the cache ?
Do you see the module enabled in your configuration (system > configuration > advanced > Advanced) ?
What is the exact error you get in your log ?
Well you are on M1 so you need to confirm ShopgateConfig class is on right directory. Magento will show that error when they don't find right path for ShopgateConfig.php file
Can you tell me path of ShopgateConfig file? And from below syntax
class Shopgate_Framework_Model_Config extends ShopgateConfig
I assume if you are calling this file then it must be inside lib folder
I have installed a plug-in called tet. I copied tet.so in the plug in folder and included it in php.ini
I have checked phpinfo and says that the plugin is enabled and running.
I have created a php page to test it.
<?php
new tet();
?>
And works perfectly.
However when I try this on my laravel it gives me this error:
FatalThrowableError in mycontroller.php line 16: Class
'App\Http\Controllers\tet' not found
tet is not a controller. Tet is a class created by the php plugin. How do I include this in laravel?
You should try
new \tet();
because you are using it inside a namespace. Inside a namespace any reference to a class is considered as relative to the namespace you are in. With the "\" you can "escape" the namespace and so you can access classes outside.
I am abslolutly new in PHP and moreover in Laravel framework (I came from Java).
I am following this tutorial to create a custom authentication driver:
http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver
I have a very newbye doubt: at the beginning of this tutorial it show that I have to create a class that implements UserProviderInterface.
It only show the code but not where this class have to be put into my Laravel project. The only clue about its positioning is the namespace:
namespace MyApp\Extensions;
But exactly where have I to put it?
I have the following structure:
It say to put it into MyApp\Extensions but I have not MyApp and Extension folder in my project? (or maybe the nampespaces name doesn't reflect a directory structure into the project tree?)
So where have I to create this class?
if you want it to go inside MyApp\Extensions, consider MyApp as the app folder.
Then only thing to do is create a folder named Extensions inside app folder and create your UserProviderInterface.php there.
But If I were you, I'd create it under app\Auth\Providers\UserProviderInterface.php
I believe what the page author meant was to create a folder named Extensions and create the provider file under app/Extensions folder. MyApp is just a custom namespace that the author chose, that in default laravel app, it should be App.
Which means, if you create a folder Extensions under app folder, the DummyAuthProvider should then be in the namespace of namespace App\Extensions;
How to avoid "Cannot redeclare class" fatal error when two modules use the same class name in prestashop?
For example, I have module which declares and uses a helper class Foo.
I install a new module which has a different helper class but with the same name Foo. So that causes "Cannot redeclare class" fatal error when I go to modules page. I cannot even uninstall that new module.
When creating modules that use custom classes / object models, you need to namespace them. In PrestaShop you cannot use PHP's namespace feature (because the PrestaShop core is not adapted to work with it), but you can do it the old way by prefixing your class name. For example:
// Your module:
class MyModule extends Module
// Your custom ObjectModel:
class Message extends ObjectModel
Class name Message is very generic and will mostly like come to conflict with some other module that has poorly chosen to name it's classes.
To prevent this, you must prefix your class names:
class MM_Message extends ObjectModel
In this case MM_ is short for MyModule. This is much less likely to conflict with other modules or classes. Event a better way would be to prefix the whole module name:
class MyModule_Message extends ObjectModule
Also, name your database table according: ps_my_module_message. This also makes easy to navigate the database table. Prefixing class names is very good practice, in fact, I do it all the time. The downside might be longer class names.
P.S If you want to uninstall a module that is conflicting, you need to temporarily disable on of them. A good way would to temporarily rename the module folder to something else (folder of the module that you want to leave), then uninstall the other module. After that, restore the original folder name. Renaming module folder will prevent it from loading.
Technically your could try to disable it in back-office, if it's not loaded in BO
Well you'll have to edit one of those two modules and change its class declaration and every occurrence of that class in its other php files.
In the next version of Prestashop (1.7) the notion of namespaces will be introduced with the use of Symfony 2 Framework.
I'm using zend framework2 skeleton, and I'm trying to add my own classes behind vendor folder. The problem is: I don't know how to prepare the classes to be loaded on demand (autoloaded) only when I'll instantiate one of them from any module.
I tried with the solution explained at: How to load a custom library in Zend Framework 2? , but I'm getting the same error:
Fatal error: Class 'myNamespace\myClass' not found in path\IndexController.php on line x
My question is: Where and how should I insert my own classes to be later used from my modules?
Firstly, if they are your own classes and directly related to the modules (i.e not some custom library you have written) then you are better off having them in the actual module, not in the vendor folder.
However, if you are trying to include your own library that you have written I'd suggest you consider storing this elsewhere and adding it as a dependancy via composer.
I don't recommend this but you should be able to get the autoloading working by using the psr0 autoloader built in to composer (assuming your classes follow psr0). Example composer.json config:
{
[...]
"autoload": {
"psr-0": {
"MyNameSpace": "path/to/root/src/directory"
}
}
[...]
}
I know its a bit old but i was facing the same issue so what i did was:
inside vendor folder i created a new folder and gave it the name of Custom. Inside that folder i added my class file (MyClass.php).
inside MyClass.php i added this line (namespace Zend\Custom;).
And inside my Controller at the top
use Zend\Custom\MyClass;
and within a method of that controller
$someVar = new MyClass();
$someVar->someClassMethod();
I hope this helps.