call member function out of Yii? - php

I know it's not legal, and looking odd. But it's necessary for me.
I have two projects, one is custom Open-Cart, and the second is in Yii. My main project is opencart. My Yii project is kept in the Main project root.
Now I want to call a Yii function in my open cart.
please anyone help me and tell How to call the Yii function in my main project?
this is my Yii function:-
$sm=Yii::app()->getSecurityManager();
if ($salt === null)
$salt = Yii::app()->params['password_security_salt'];
if($salt==null)
$salt=md5 (mt_rand ().mt_rand ().mt_rand ().mt_rand ());
$pass=sha1($salt.$pass.$salt);
return $sm->hashData($pass,$key).':'.$salt;
.....................
I want to create a new function manually for my opencart project.
please help me for creating a new project for the same functionality as Yii (upper function) function.

You can use Yii functionality outside of the Yii project by initialising the application like so:
// this is in someotherfile.php outside of the yii project
require_once('framework/yii.php');
$config = require_once('protected/config/main.php');
Yii::createWebApplication($config);
// call your function
Yii::app()->getSecurityManager();
Obviously the paths to the yii.php file and the application config (main.php) will need to change to fit your project structure

Related

Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for init

I'm maintaining a PHP Zend application. I'm attempting to add functionality to it.
I'm trying to call a Controller through a phtml file. I'm thinking I'm approaching this the wrong way, but I'm not sure what the correct way is.
I've modified three files and added another. I've added code to FileController.php.
public function getModifiedBy($filename) {
$groupFiles =$this->getServiceLocator()->get('qatools\Model \GroupFilesTable');
$modified = $groupFiles->fetch($filename);
return $modified;
}
I've also added code to job-wizard.phtml.
<?php
use qatools\Controller\FileController;
$fileControl = new FileController;
$fileControl->init();
$modified =$fileControl->getModifiedBy("addresscleaningservice.xlsx");
?>
The new file is 'GroupFileTable.php' which extend AbstractModelTable and queries a MySQL database. I added the following lines to module.config.php.
'qatools\Model\GroupFilesTable' => function($sm) {
return defModelTable($sm, 'GroupFilesTable');
},
'GroupFilesTableGateway' => function($sm) {
return defModelTableGateway($sm, 'group_files', 'GroupFiles');
},
The code is failing on $fileControl->init() in job-wizard.phtml. I tried taking out that line, but then the code failing on the getServiceLocator call.
#0 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library /Zend/ServiceManager/AbstractPluginManager.php(103): Zend\ServiceManager\ServiceManager->get('init', true)
#1 /mnt/c/git-repos/qatools/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/PluginManager.php(82): Zend\ServiceManager\AbstractPluginManager->get('init', NULL, true)
You didn't specify what version of Zend Framework are you using, anyway whatever version is, you should not create an istance of a controller within a view script.
In ZF, if you need to call a view functionality, a better approach is to create a view helper, the logic between ZF1 and ZF2/3 is the same, only the implementation changes, I'll leave to you google searches to see examples, your final view script code should be something like the follow
<?php echo $this->getModifiedBy("addresscleaningservice.xlsx"); ?>
The viewHelper will contain the logic. If at some point you'll need to reuse this logic within a controller or in whatever other place, my advice is to create a component with the logic, then create a service that returns the instance of this component and eventually create an action helper ( $this->myComponent in any controller ) and a view helper ( $this->myComponent in any view ) and inject the same component instance.

Best way to load modules in CodeIgniter MVC

I am new to the CodeIgniter fw of PHP and generally have medium experience with MVC. I am working on a project where using modules will make my life a lot easier.
There is something regarding the way you call the modules that is a bit unclear in my head. The solution I had was to call the full module in the controller and pass the code as a variable in the view and then just echo it. Here is a sample of that logic
// Controller
$var['login'] = // code that calls the module
$this->load->view('index', $var);
// View
<div class='navbar'>
<?= $login; ?>
</div>
However after a small search, I found this solution Creating Block/Modules in Code Igniter
(String path of the module (view2) in the controller, pass it to view1 and then call it in the main view): .
Which of the 2 logics is the one that follows the MVC standards in a better way?
UPDATE: I have found the answer on how to do both here: how to load view into another view codeigniter 2.1?
but my question remains the same. Which is the best logic that follows the MVC standards better?
first create model object
like
$model = new mymodel
where u want to call do this
$this->model->your_function_name($parameter);

create wordpress plugin with zend framework 2

I want to develop a membership plugin under wordpress and for this I want to use zend framework 2.
Does anyone managed to create a wordpress plugin using zend framework 2?
I'm new to zf and I do not know how and where to start from.
I tried to start from zend skeleton application but got stuck at add_menu_pages and displaying a simple dashboard.
Can anyone give me some ideas or links.
Thanks!
Updated!
I managed to get this working! I just needed to use a PhpRenderer. For those who need a little more help here is how I did:
I created a class that manages all admin area. On class init I called a method that created menu pages( in this method simply add_menu_pages() and instead of callback_function I called a new method, manage_pages, that, wel... manages pages, but you can do it as you desire) and then I initiated the view, like this:
$this->view = new PhpRenderer();
$this->map = new Resolver\TemplateMapResolver(array(
'template_name' => 'template_path',
'template2_name'=> 'template2_path')); //this is for handling view templates a little easier
$this->resolver = new Resolver\TemplateMapResolver($this->map);
$this->view->setResolver($this->resolver);
$this->model = new ViewModel();
Further, in manage_pages method, for each page I have, I added its own template and variables I needed
$this->model->setTemplate('template_name');
$this->model->setVariable('variable_name', value);
As for displaying template, you just have to write this piece of code:
echo $this->view->render($this->model);
In the template files you can access variables using $this->variable_name
Also you can insert another template using $this->partial( 'template2_name', assoc_arrray_of_variables_to_be_passed_to_template ).
And this is it! If you have any questions, please let me know!
There is a wordpress plugin you can use, search "wopzen2" or "wordpress and zend framework 2 integration" on Google, with this solution, you can use the following code inside the php wordpress code:
global $wpzf2plugin;
$render=$wpzf2plugin->render('/application/index/contactform');
echo $render;
This code calls the contactform action, if you are familiar with zendframework I think you are going to understand it.
This plugin is dedicated to developers.
You can get a free version of the plugin via support center.
I hope this answer can be helpful for you
reference link:
Example codes

Changing the behaviour of view in Codeigniter

I am using codeigniter for a project that is used by a variety of companies.
The default version of our software is up and running and works fine - however some of our customers want slightly different view files for their instance of the system.
Ideally what I would like to do is set a variable (for example VIEW_SUFFIX) and whenever a view file is loaded it would first check if there was a suffix version available if there was use that instead.
For example if the system had a standard view file called 'my_view.php' but one client had a VIEW_SUFFIX of 'client_1' - whenever I called $this->load->view('my_view') if the VIEW_SUFFIX was set it would first check if my_view_client_1 existed (and if it did use that) or if not use the default my_view.php.
I hope that my question is clear enough... If anyone has done this before or can think of a way to do it I would really appreciate it.
EDIT:
Ideally I would like a solution that works without me changing every place that I am calling the view files. Firstly because there are a few files that may want different client versions and also because the view files are called from a lot of controllers
I had a similar requirement for which I created a helper function. Among other things, this function can check for a suffix before loading the specified view file. This function can check for the suffix and check if the file exists before loading it.
Unfortunately, the file checking logic would be a bit brittle. As an alternative, you can implement a MY_Loader class that will override the basic CI_Loader class.
Something like this in your application/core/MY_Loader.php:
class MY_Loader extends CI_Loader {
protected function _ci_load($_ci_data)
{
// Copy paste code from CI with your modifications to check prefix.
}
}
Could you not do this
// some method of creating $client
// probably created at login
$_SESSION['client'] = 'client_1';
$client = (isset($_SESSION['client'])) ? $_SESSION['client'] : '';
$this->load->view("your_view{$client}", $data);

widget within module in Yii

I'm trying to create a widget within the module and then load that widget from 'outside' of the module. More particularly I'm using user module written by someone else. I don't want to have a separate page for displaying a login form, therefore I tried to make a CPortlet/widget (confusion) displaying the login form. Basically, I've moved the code from LoginController into that widget. Then I try to display the widget on some random page by
<?php $this->widget('user.components.LoginForm'); ?>
However, I get an error
CWebApplication does not have a method named "encrypting".
in UserIdentity class in this line:
else if(Yii::app()->controller->module->encrypting($this->password)!==$user->password)
This happens, because I'm basically trying to execute this code within context of the app and not the module. Thus the "Yii::app()->controller->module" trick doesn't really work as expected.
What am I doing wrong:-\
Is there a better way to achieve this. I.e. display that login form in some other page, which is normally displayed by accessing login controller within user module (user/login) or is a widget the right way of doing it?
Thanks.
The quick solution
Ok, so I simply ended up doing
Yii::app()->getModule('user')->encrypting($this->password)
instead of
Yii::app()->controller->module->encrypting($this->password)
Notice that now the module must be called 'user' in the main config, but I think this allows for more flexibility. I.e. we're not bound to only use module functionality within the module.
Additional insight on displaying widget outside of the module scope
After playing more with it that's what I did. In the UserModule.php I've created a method
public static function id() {
return 'user';
}
Then everywhere where I need the module I use
Yii::app()->getModule(UserModule::id())->encrypting($this->password)
I don't like having many imports related to the module like:
'application.modules.user.models.*',
'application.modules.user.components.*',
Because we already have those imports in the UserModule.php:
public function init()
{
// this method is called when the module is being created
// you may place code here to customize the module or the application
// import the module-level models and components
$this->setImport(array(
'user.models.*',
'user.components.*',
));
}
Therefore whenever you know that some piece of functionality will be used outside of the module it's important to make sure the module is loaded. For example, in the LoginForm widget that I am trying to display NOT in one of the module controllers, I have this line of code:
$model = new UserLogin;
However, UserLogin is a model inside of the User module, and in order to be able to autoload this model we first have to make sure the module was initialised:
$module = Yii::app()->getModule(UserModule::id());
$model = new UserLogin;
I hope this will be helpful if you were stuck with the whole modules concept the way I was.
http://www.yiiframework.com/forum/index.php?/topic/6449-access-another-modules-model/ was useful but hard to find =)
You better move that encrypting() into a MyUserIdentiy class which extends CUserIdentity. Whatever the code you take to use, they putting the method in controller is a bad idea and as a result you cannot reuse that code.
The login form should still post to User/Login controller but I guess they use Yii's standard login code and you might want to modify it to use the MyUserIdentity.

Categories