Prestashop 1.7 Create new classes for modules and overrided controllers - class

I started with prestashop and I have an organizational problem in my code. I created several modules and new tables. I also overrided controllers in the myprestashop/override/ folder in which I want to use my tables, so I have to create new classes. But I don't know where to create classes in prestashop for as much access in my modules as in my overrided controllers.
For now I'm calling my classes create in my modules, in my overridded controllers. Although I know it's not the right solution.
require_once _PS_MODULE_DIR_.'my_module/classes/MyNewClass.php'
Would someone have the answer?
Thank you!!

Well your way is acceptable solution as PrestaShop doesn't have any autoloading capabilities for custom classes.
However what I like to do is use Composer in modules and use its autoloading capabilities for my classes and any libraries that the module might need.
Update
An example of composer usage in myexample module:
Module structure
modules/
myexample/
classes/
mynamespace/
myexample.php
composer.json
myexample.php
require_once __DIR__ . '/vendor/autoload.php';
class MyExample extends Module
{
// module code
}
composer.json
{
"autoload": {
"psr-4": {
"mynamespace\\": "classes/mynamespace"
}
}
}
Run composer install from module folder.
Now you can put classes under mynamespace folder (with proper namespace definition of course) and they get autoloaded anywhere your module is used (module controllers, models, hooks etc).

I noticed that below scheme works too in version 1.7.6, but I would ask experienced developers to point if this is correct approach.
I put my custom class file into /override/classes folder:
namespace CustomNamespace;
class CustomClass {
...
}
then in other places I just use the class:
use \CustomNamespace\CustomClass;
and my CustomClass works as expected

Related

TYPO3 10 Viewhelper class not found, Fluid error

There is some small detail I seem to be missing but I just can't find out what it is...
I get this error:
Error: The ViewHelper "<ugh:example>" could not be resolved. Based on your spelling, the system would load the class "TYPO3\Projectname\ViewHelpers\ExampleViewHelper", however this class does not exist.
I have my own extension called sitepackage, which works without issues.
In my ext_localconf.php I wrote
$GLOBALS['TYPO3_CONF_VARS']['SYS']['fluid']['namespaces']['ugh'] = ['TYPO3\\Projectname\\ViewHelpers'];
Then there's the directory ViewHelpers inside Classes with the file ExampleViewHelper.php
<?php
namespace TYPO3\Projectname\ViewHelpers;
use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
// etc ...
I expect it to use like <ugh:example />
What am I missing? Is there something outside of this that I have to do?
I'm freaking out about this...
Your global configuration works, yet PHP is not able to find your TYPO3\Projectname\ViewHelpers\ExampleViewHelper.
I suspect that you have a spelling error or your autoloading information is out of date (assuming a composer-based installation, composer dump-autoload updates it) or you forgot to add your namespace to composer.json (see composer docs or a TYPO3 extension example here).
That said, the usual approach is to add ViewHelper namespaces to the Fluid template directly. See https://docs.typo3.org/m/typo3/book-extbasefluid/10.4/en-us/8-Fluid/8-developing-a-custom-viewhelper.html#importing-namespaces
Without composer
Use TYPO3 InstallTool "rebuild autoload information" or similar to build the class autoloading information. I think ext_emconf.php needs to contain the autoloading information, too.

Custom Helper Class not found in Prestashop module

I created a helper class in prestashop 1.6 module, in the override directory. I included the file in main module file but the class is not found in tpl files. I need it there to call one static method from smarty template.
Any ideas with this?
Thanks.

Zend Framework 1.11: how to autoload a class that uses namespaces

I have a Zend Framework 1.11 application, and I want to use a package called RandomLib. The problem is, it doesn't have an autoloader, and I've tried reading the Zend documentation on using autoloaders, but I can't make sense of it.
I've placed the RandomLib folder in my library directory. What kind of code would I need in my Bootstrap.php file to autoload the class?
Starting in version 1.10.0, Zend Framework now allows loading classes from PHP namespaces. This support follows the same guidelines and implementation as that found in the ยป PHP Framework Interop Group PSR-0 reference implementation. Source
Put content of RandomLib/lib under library/RandomLib
In application.ini add autoloaderNamespaces[] = "RandomLib"
If you wish you can include namespace libraries directly in your Bootstrap.php file after you moved the library in "library/MyExternalLib"
protected function _initAutoLoader()
{
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('MyExternalLib');
return $loader;
}

Zend Framework: Can you do ACL without the plugins directory?

I am having trouble understanding the rules to ACL in ZF and the docs aren't clear. I am using a common Zend library for all websites. So far no problem but now every demo or example says that you should place the ACL class (acl.php) in the libraries directory as a plugin. Zend/Library/My/Controller/Plugin/.
I don't want to do this because it defeats the purpose for sharing a common framework directory.
Has anyone done or have any ideas about how to accomplish ACL using individual acl.php class files for each website/web application?
Thanks
You don't have to place the acl.php in the libraries directory as a plugin. The autoloader will load the class just fine, the trick to Zend_Acl is just priming an instance of the class with your roles and resources.
It's been a little while since I touched Zend Framwork but I'll try to steer you in the right direction.
In your bootstrap, create the Zend_Acl object
$acl = new Zend_Acl();
//see documentation on how to add roles and resources
Now create a Plugin folder inside your Controller directory, this will allow you authenticate with your acl.
Inside there create new class that extends Zend_Controller_Plugin_Abstract give it the correct class name to be picked up by the autoloader.
Store the acl you create in the registry and in your plugin override the preDispatch method, from here you have access to the request and the acl (from the zend registry) you can validate as needed. (Some people have controller/action as resources others models. It's quite freeform.
Register your plugin with the front controller.
$frontController->registerPlugin(new My_Controller_Plugin_Acl());
This is probably what the other tutorials are suggesting (or variants of this), it can just be a little confusing sometimes.
You should never add files to your Zend library directory - do you have any links to tutorials recommending this? The files should either go in the library directory under your application's namespace, giving you a structure like:
application/
library/
Zend/
(ZF files)
Foo/
Controller/
Plugin/
...
or in application/plugins, application/controller/helpers or somewhere else depending on the approach you are taking.
Edit: it sounds like a controller plugin is what the tutorial is recommending, in which case you'll want a class like Yourapp_Plugin_Acl (replace 'Yourapp' with your app's namespace) which would live at application/plugins/Acl.php.
Ultimately, you can place it anywhere you want as long as your autoloader is sufficiently configured to find it. And precisely how you use it depends upon what resources and privileges you are trying to protect.
But think you are confusing instantiating your ACL and querying your ACL.
You will most likely instantiate/populate your ACL object during bootstrap and store it in the Bootstrap registry or in the Zend_Registry singleton.
If your resources are controllers and your privileges are actions, then it is common to intercept the dispatch cycle with a preDispatch() plugin that queries your ACL object.
So, we are really looking at two different classes/objects:
One is the ACL itself, extending Zend_Acl. This one could be named Application_Model_Acl and placed in the file application/models/Acl.php.
The other is the front controller plugin. This one could be named Application_Plugin_Acl and stored in the file application/plugins/Acl.php
[Note that both of these presume that we are using an application namespace Application. Also, note that both of these are project-specific.]
Of course, the plugin as described needs to be given the ACL object in order to do its job, so your Bootstrap might have a method like this:
protected _initAclPlugin()
{
$acl = new Application_Model_Acl();
$plugin = new Application_Plugin_Acl($acl);
Zend_Controller_Front::getInstance()->registerPlugin($plugin);
}
But remember, this is only one way to use your ACL. In some cases, your ACL might not be limited to just controllers/actions. In that case, you might need to pass your ACL object to other models/services that query it, as well. In that case, you might have a separate method in your Bootstrap to create your ACL object and store it in the Bootstrap registry. Then your controllers - or even a dependency injection system - can grab it from there and pass it through to whatever downstream models/services might need it.
[You know, looking at my answer, it's not really different from that of #linead. Same idea, different words, but he totally got in first.]

how to load models from another module in zend framework?

i am developing an application using zend framework.
i have two modules, admin and default, and each of them has their specific model directory.
i want to know, if i can instantiate a model in admin module from within default module and if this approach has problem regarding to the MVC model.
thx in advance.
So long as youve set up the Zend_Application_Resource_Modules or something pretty equivalent all you models should be registerd with the autoloader via the Zend_Application_Module_Autoloader that the modules resources registers. In short, if you follow the default way of doing things, then Models from all modules will be set up for Autoloading in the bootstrap phase.
About Zend Application and Resources
You can call get_class(): http://us3.php.net/get_class
There's possibly a more zend like way to do it, but I don't know. Check the docs.
What about call via object?
Like inter-connect two model functions in controller.
$contacts = new Model_DbTable_Contactsmdl(); // Model file in contact module
$update_id = $contacts->updateContacts($cn_id', $responsearray);
This code inside my syncController.
So you can handle admin / model function in default / controller.