How to make reusable code in yii 1.1 between controller and command? - yii

What is the best way to reuse code between controller and command?
I have some use where the very same logic should be executed inside controller and in command as well. What is the best approach to share code? To create a component and the call this component methods from controller and command?

Create a class (model, service, etc), and use that in the different areas.
Within the config/main.php or config/console.php there is an import section:
'import' => array(
'application.models.*',
'application.components.*',
You can add your class anywhere really, as long as its location is referenced within the import section in the config, as that is how Yii1 autoloads files.

Related

Yii2 call action from another module

i have a module called 'elite' which render articles and another module called 'inet'.
Inet->controllerX need to get the HTML only from an article(elite->article->actionView).
so my question is:
should i call elite->article->actionView from another module or use components and inheritance and how?
Thank you!
You can easily render a view in any module, just need view path.
return $this->render('#app/modules/elite/views/name_controller/name_view');
You must replace the name of the controller and the name of the view.

What is the hook for the list of products?

I need a hook that I can't find in the documentation, but logically it should exist.
In my module, I need to add some html in category page (or search results page) after the tile with the products but before pagination.
Which hook is best to use?
There is no default hook for your goal, but you can create your own one and use it within your module. The inconvenience is that you will be able to use it only in modified themes or you will need to add it manually to all new ones. To create the desirable hook you need to put
{hook h='displayYourHook' info=$someData}
in the top of your_theme/templates/catalog/_partials/products-top.tpl
and then just use it like a default hook within your module
public function hookDisplayYourHook($params)
{
// $params can be some information. ID of category for example
do all necessary stuff here
}
and also do not forget to register your hook during the module installation
public function install()
{
....
&& $this->registerHook('addproduct')
....
}
Also, I presume that you use prestashop 1.7.* if not - some code can be different

Dynamically provide Aurelia view model instance

I'm looking for a way to dynamically provide at runtime view models for any views. Question is if there is hook in the ViewEngine or a view model loader / factory that I can use to do this.
I'm looking into bridging into WebAssembly and allowing view models to be written in other languages and I want to create automatic interop view models on the Javascript side for Aurelia to just work.
This is totally doable, and many have successfully delivered their ultra-dynamic application using Aurelia, as pretty much everything in Aurelia is dynamic.
For example:
dynamic, instance based inline view: https://codesandbox.io/s/r0no20706p (Based on this discourse topic https://discourse.aurelia.io/t/template-literals-in-class-instance/1829/6)
dynamic, remote view strategy: https://codesandbox.io/s/8ljmrq3vql (Based on a github question)
dynamic, route based view model: https://codesandbox.io/s/ywyj60p9qj (based on this discourse https://discourse.aurelia.io/t/ideas-on-data-driven-ui-composition-vs-previous-angularjs-approach/2269/3
Basically it boils down to is to use .compose method from CompositionEngine:
compositionEngine.compose({
viewModel:
string // as module path
| Function // as constructor
| object // as instance
})
And make sure the view is resolvable from the view model.

When i can find Yii::app()->user->checkAccess

I am new in Yii. And some things in this framework i am understand well. But i am can't understand how work Yii::app() and where i can find Yii::app()->user->checkAccess method?
Should you are explain me it. Thanks!
Yii::app()->user is the user component, which is defined in your config file (usually /protected/config/main.php). In the components array you will find a 'user' component. The default class for this is CWebUser, so probably 'checkAccess' is defined in CWebUser (did not check this though).
You can write your own class extending CWebUser if you want to override this property (it's not a method).
See your /protected/config/main.php file, and then you may find authManager section in components. In my case, I set the class of authManager to CDbAuthManager. In that class, checkAccess method is defined.

Extend products with custom module in Prestashop

I am writing a module, I know how to build a global module, but I would like to do the following:
Have global properties (already covered)
Have the same properties in a tab in the products interface in the admin where the user can override the global properties (still to do)
Now my question:
How do I add an additional tab to the products interface in the prestashop admin?
I guess I can easily call these in my module templates and check if global is overridden?
Thanks for the feedback,
Eric
PS. Using prestashop 1.5.8, will worry later to extend to 1.6
To add an additional tab to product edit page, you can use hookDisplayAdminProductsExtra.
First, you will have to register this hook inside install() method:
public function install() {
...
$this->registerHook('displayAdminProductsExtra')...
...
}
after that you define this:
public function hookDisplayAdminProductsExtra($params) {
...
return $this->display(__FILE__, 'views/admin/yourtemplatefile.tpl');
}
File yourtemplatefile.tpl defines the contents of your extra tab. Extra variables used in this file can be assigned inside hookDisplayAdminProductsExtra function
Here you can find additional information about creating a module:
http://doc.prestashop.com/display/PS15/Creating+a+PrestaShop+module
And here you can find information about hooks:
http://doc.prestashop.com/display/PS15/Hooks+in+PrestaShop+1.5