use external libs in Yii 1 - yii

I have google lib - https://github.com/rapidwebltd/php-google-contacts-v3-api
and i need to implement it in YII framework
How, for example implement this code in YII:
require_once '../../../vendor/autoload.php';
use rapidweb\googlecontacts\helpers\GoogleHelper;
$client = GoogleHelper::getClient();
$authUrl = GoogleHelper::getAuthUrl($client);
How to use namespaces in my case ?

In order to use the composer autoloader, you have to unregister the Yii one first.
Example:
spl_autoload_unregister(array('YiiBase','autoload'));
require '../../../vendor/autoload.php';
spl_autoload_register(array('YiiBase','autoload'));
$client = rapidweb\googlecontacts\helpers\GoogleHelper::getClient();
$authUrl = rapidweb\googlecontacts\helpers\GoogleHelper::getAuthUrl($client);

Related

How to access services from a view script in Zend Framework 3?

I have a custom authentication service and in ZF2 I accessed this as follows:
Application/view/layout/layout.phtml
$authenticationService = $this->getHelperPluginManager()
->getServiceLocator()
->get('AuthenticationService');
$currentIdentity = $authenticationService->getIdentity();
Now the Zend\ServiceManager#getServiceLocator() is deprecated.
How to get a service available in a view script (or concrete in this case in the layout) in ZF3?
For this purpose there is already Identity View Helper
As documentation says
// Use it any .phtml file
// return user array you set in AuthenticationService or null
$user = $this->identity();
The solution is to assign a global view variable in the onBootstrap(...):
namespace Application;
use ...
class Module
{
public function onBootstrap(MvcEvent $e)
{
...
$serviceManager = $e->getApplication()->getServiceManager();
$viewModel = $e->getApplication()->getMvcEvent()->getViewModel();
$viewModel->authenticationService = $serviceManager->get('AuthenticationService');
}
...
}
Another (perhaps an even better/cleaner) solution is to use a ViewHelper. See also here.

Using Yii1 and Yii2 in the same project

I had a project in Yii1.x and now I am using Yii2 for the same projects
Project hierarchy is something like this
Project1(yii1)/all yii files + project2(yii2)
project2(yii2)/frontend + /common + /backend
Now I want to know if is it possible to use project2/common/models in project1/protected/controllers
How can I achieve this task?
Thank you
I wouldn't recommend doing it, instead it's better to completely rewrite old application in Yii2.
But in case of partial migrating, please read this paragraph in Special Topics Section in Official Guide.
Here are some important code snippets from there:
1) Modification of entry script:
// include the customized Yii class described below
require(__DIR__ . '/../components/Yii.php');
// configuration for Yii 2 application
$yii2Config = require(__DIR__ . '/../config/yii2/web.php');
new yii\web\Application($yii2Config); // Do NOT call run()
// configuration for Yii 1 application
$yii1Config = require(__DIR__ . '/../config/yii1/main.php');
Yii::createWebApplication($yii1Config)->run();
2) Combination of Yii classes:
$yii2path = '/path/to/yii2';
require($yii2path . '/BaseYii.php'); // Yii 2.x
$yii1path = '/path/to/yii1';
require($yii1path . '/YiiBase.php'); // Yii 1.x
class Yii extends \yii\BaseYii
{
// copy-paste the code from YiiBase (1.x) here
}
Yii::$classMap = include($yii2path . '/classes.php');
// register Yii 2 autoloader via Yii 1
Yii::registerAutoloader(['Yii', 'autoload']);
// create the dependency injection container
Yii::$container = new yii\di\Container;
Usage of Yii class:
echo get_class(Yii::app()); // outputs 'CWebApplication'
echo get_class(Yii::$app); // outputs 'yii\web\Application'

create_assignment function in moodle

I am working on integration of moodle with openerp.
Is there any function in moodle for create_assignment in moodle webservice. I found moodle_assign_get_assignment function in External service of moodle. But i wants create_assignmet function. Help me about this.
Using Moodle 2.8.x, have you tried assignment_add_instance?
I haven't yet but I use PHPStorm as an IDE and it hooks into all the Moodle API functions. So I just typed "assignment" and found that.
It is located in mod/assigment.lib.php line 36 and looks like this:
function assignment_add_instance($assignment, $mform = null) {
global $DB;
$assignment->timemodified = time();
$assignment->courseid = $assignment->course;
$returnid = $DB->insert_record("assignment", $assignment);
$assignment->id = $returnid;
return $returnid;
}
What I tend to do is I create an assignment on a test version of my Moodle, intercept the form before it submits and var dump the submitted form so you can see what you have to send into the function for it to work.
Good luck.
Of course, it may be:
function assign_add_instance(stdClass $data, mod_assign_mod_form $form = null) {
global $CFG;
require_once($CFG->dirroot . '/mod/assign/locallib.php');
$assignment = new assign(context_module::instance($data->coursemodule), null, null);
return $assignment->add_instance($data, true);
}
That you have to call instead - cant remember which one superseded the other...

zend_db standalone

i want to use zend_db standalone cos zend framework is too much for my project but i'm new with it,
is it correct to do this:
$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES
UTF8;');
$params = array(
'host' => 'localhost',
'username' => 'ss_fraat',
'password' => 'jos10',
'dbname' => '_a2hDB',
'driver_options' => $pdoParams
);
try {
$db = Zend_Db::factory('PDO_MYSQL', $params);
//set default adapter
Zend_Db_Table_Abstract::setDefaultAdapter($db);
} catch (Exception $e) {
exit($e->getMessage());
}
//save Db in registry for later use
Zend_Registry::set('dbAdapter', $db);
then in any class do this:
$db = Zend_Registry::get('db');
/** quote to avoid sql injection */
$date = $db->quote('1980-01-01');
$sql = 'SELECT * FROM product WHERE name = ' . $date;
$result = $db->query($sql);
$db->query(); //run a query
i really need to do this
Zend_Db_Table_Abstract::setDefaultAdapter($db);
i get this code from a website,
is it necessary to use Zend_Db_Table_Abstract if i'm not using the full zend framework,
or it is better for example to use this:
$db = Zend_Db::factory( ...options... );
$select = new Zend_Db_Select($db);
what i want is to setup a pdo/mysql connexion in my bootstrap php page and be able to get that db instance in any class without starting a new connexion to execute queries but i'm not sure how to do that use Zend_Db_Table_Abstract or Zend_Db_Select use the registry Zend_Registry::set('dbAdapter', $db) or not
thanks a lot
The purpose of Zend_Db_Table_Abstract is so you can create your own model classes based around the Table Data Gateway design pattern. The idea of that pattern is that you have a class that encapsulates all the sql you would need for interfacing with a table. So the assumption is that you will be creating model classes that extend Zend_Db_Table_Abstract for each table. If you are going to do that, then you will want to call Zend_Db_Table_Abstract::setDefaultAdapter($db) in your setup/bootstrap. Recent versions of ZF provide as an alternative a quick way of getting basic functionality without having to create a custom class definition by just instantiating Zend_Db_Table:
$userTable = new Zend_Db_Table('users');
In summary, none of this particularly has to do with the MVC part of the framework, although some people choose to use Zend_db as the basis for db connections and models, instead of using a more fully featured ORM like Doctrine or Propel.
The other code you provided simply illustrates that you do not need to use Zend_Db_Table_Abstract either -- you can simply setup an instance of a Zend_Db_Adapter and use that instance to call query() or its other methods.

Zend_Rest_Route for a specific controller

I'm trying to make a Zend_Rest_Route for a specific controller. I want the rest of my site to behave normally, except when a particular Controller (UploadAPI) is requested. I think the sytnax should be as follow, but having a hard time verifying. The examples all have to do with modules, but I don't have a module.
Is this correct?
protected function _initRestRoute() {
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
$restRoute = new Zend_Rest_Route($frontController, array(), array('default' => array('UploadAPI'),));
$frontController->getRouter()->addRoute('rest', $restRoute);
}
The link here
http://weierophinney.net/matthew/archives/228-Building-RESTful-Services-with-Zend-Framework.html
gives examples with modules, but I have no modules, and am assuming "default" is the module name.
So I have the API functionality working , this is how it looks. You have to add this function in Bootstrap class to initilize Zend_Rest_Route.
This will do the Zend Rest API routing only for the controllers listed in the array, the rest of the site should work as expected
protected function _initRestRoute() {
$this->bootstrap('frontController');
$frontController = Zend_Controller_Front::getInstance();
$restRouteUL = new Zend_Rest_Route($frontController, array(), array('default' => array('UploadAPI', 'LocationMatchesAPI', 'GetMatchesByIdAPI', 'AuthAPIController') ));
$frontController->getRouter()->addRoute('rest', $restRouteUL);
}