Adding 3rd party custom library in Yii2 - yii2

I have a tax library avalara. How can I add it using Yii2. I have tried in putting in Vendor folder.
used
require "/vendor/avalara/avalara.php"
it does include the file does wasn't able to call the functions inside it.

I have used the avalara library. Extract the file and put the AvaTax and autoload foler in vendor.
Add the a class file in model directory.
$base_path = str_replace("frontend", "", \Yii::$app->basePath);
require_once $base_path . 'vendor/avalara/vendor/autoload.php';
use Yii;
use AvaTax\Address;
use AvaTax\DetailLevel;
use AvaTax\GetTaxRequest;
use AvaTax\Line;
use AvaTax\TaxServiceSoap;
use AvaTax\ATConfig;
use AvaTax\CancelTaxRequest;
After that create custom functions and use them in controller.

Related

Yii2: differences between init() and bootstrap() and best place to add dynamical URLs on a module

I dont understand exactly what is the difference between use init() and bootstrap() on a class.
My case:
I want to add dynamical urls from my module by using Yii::$app->urlManager->addRules(...) but NOT loading the module in order to improve the performance.
So, I thought if bootstraping the module from the main configuration file like: 'bootstrap' => ['mymodule'], the Module::bootstrap() function will be executed ONLY and exclusively. But actually always runs Module::init() function, and then Module::bootstrap().
On this documentation http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#adding-rules say:
'Note that you should also list these modules in yii\web\Application::bootstrap() so that they can participate the bootstrapping process.'
But Module::bootstrap() is not executed if the module is listed on yii\web\Application::bootstrap()
I want to set only the dynamic rules with no module loading. How is it possible? What is the best place to set dynamical URLs with no impact to performance?
i decide resolve this issue(adding dynamic rules for module) by watching working extensions.
for example https://github.com/dmstr/yii2-pages-module extension uses bootstrap interface.
don`t forget write in composer.json "type" attribute as "yii2-extension".

How to have class loaded without using "use" but call it as if I used it

I have studied these 2 sources, but none of those works for me.
http://www.yiiframework.com/doc-2.0/guide-concept-autoloading.html
Yii2 - How do I AutoLoad a custom class?
I have created custom helper class which I want to include in every model, controller and view in my application. I am using Yii2 advanced app version 2.0.11, IDE I am using is PHPStorm
QUESTION:
What I want to achieve is to not use use keyword at the beggining of every class but still be able to simply call AppHelper::myMethod() in models, controllers and views.
How is that possible?
Closest I got it working was using this solution https://stackoverflow.com/a/35160997/5395463
All other solutions did not work for me. I am getting errors like:
PHP Fatal Error – yii\base\ErrorException
Class 'frontend\controllers\AppHelper' not found
when I simply dont include use commons\commands\AppHelper;
or when not using namespace as they suggest there with other settings:
Fatal error: Uncaught exception 'yii\base\UnknownClassException'
with message 'Unable to find 'common\commands\AppHelper'
in file: C:\xampp\htdocs\domain.com\web\common/commands/AppHelper.php.
Namespace missing?' in ...
SOLUTION:
Thanks for your responses, I got it working finaly. https://stackoverflow.com/a/42330631/5395463 solution works best for me.
So I removed namespace from that class, but left it in common\commands folder, added require to frontend/web/index.php and backend/web/index.php files (not sure if I should add it to yii file in root too, I didnt, so far it is working good anyways), and changed calls of class from AppHelper::myMethod() to \AppHelper::myMethod() looks like eveything is working now.
In Yii2 You can use an explicit way adding \ to full namespaced name
\frontend\controllers\AppHelper
so you can sue your method
\frontend\controllers\AppHelper::yourMethod();
Solution for not lazy coders:
create component with your class so you can use it like \Yii::$app->my_component
if even this is too much and your IDE is better than Windows Notepad prepare macro that will print this for you
Solution for lazy coders:
Save your class in single PHP file without namespace.
Modify you entry script to include this class - i.e. for Basic Project Template it's /web/index.php; add there
require(__DIR__ . '/path/to/MyClass.php');
For Advanced Project Template modify it properly.
Now you can use your class by calling it like \MyClass (yes, \ is required and yes, your IDE probably will modify it anyway to MyClass with use MyClass; added at the beginning.

Using Googlemaps in Yii2?

How do I use Googlemaps in Yii2 ?
I am not able to follow the Usage placed in http://www.yiiframework.com/extension/yii2-google-maps-library
Installation has been done, but I am getting error
Class 'dosamigos\google\maps\LatLng' not found
Could anyone provide a tutorial/examples where Yii uses Google Maps ?
Normally the use directive for third party extensione start with vendor
could be you must add the vendor to the use where you use these class.
try using
use vendor\dosamigos\google\maps\LatLng;
use vendor\dosamigos\google\maps\services\DirectionsWayPoint;
use vendor\dosamigos\google\maps\services\TravelMode;
use vendor\dosamigos\google\maps\overlays\PolylineOptions;
use vendor\dosamigos\google\maps\services\DirectionsRenderer;
use vendor\dosamigos\google\maps\services\DirectionsService;
use vendor\dosamigos\google\maps\overlays\InfoWindow;
use vendor\dosamigos\google\maps\overlays\Marker;
use vendor\dosamigos\google\maps\Map;
use vendor\dosamigos\google\maps\services\DirectionsRequest;
use vendor\dosamigos\google\maps\overlays\Polygon;
use vendor\dosamigos\google\maps\layers\BicyclingLayer;

How to use a model or module in both frontend and backend

Can any one guide me how to use for model in both frontend and backend in Yii2 advanced template.
Secondly what should I do if I want to use a module in both frontend and backend sides?
I have already tried to put model in common. But I am unable to access it. I am new to yii2 so detailed guide will be helpful.
In general it doesn't matter where you place it.
But in advanced template common folder exists exactly for these purposes.
For example, create User model and place it in common\models folder:
<?php
namespace common\models;
use yii\db\ActiveRecord;
class User extends ActiveRecord
{
...
}
No special configuration required.
Then you can use it like that:
use common\models\User;
User::find()->...
or
common\models\User::find()->...
The same with module, just place its content in common\modules\users for example. Common folder is for commonly used classes.
Also check official PHP documentation for namespaces.
You can make Models and Controllers and Views reusable via Modules approach by placing the module in common folder.
Step by step guide is below:
Make module "order" in any of the end (backend or frontend)
Create a new folder "modules" in common and cut/paste the order module from backend/frontend into this new "modules" folder
Open Module.php and rename the namespace to "common\modules\order" and rename the $controllerNamespace variable as well like this
public $controllerNamespace = 'common\modules\order\controllers';
Rename the namespace of DefaultController.php class to "common\modules\order"
Add this new module in config files (config/main.php) of both frontend and backend like this
'modules' => [
'order' => [
'class' => 'common\modules\order\Module',
],
],
'components' => [
.
.
.
Now you can access the reusable order module from both frontend and backend like this
mysite/frontend/web/index.php?r=order
mysite/backend/web/index.php?r=order

How can I use xxx.scala.html in Play2.0 framework Controller

I am a fresh man, using play2.0 framework. Now I have a trouble to use xxx.scala.html. In Eclipse I added a xxx.scala.html, but I cannot use "xxx.render()" function to render my html.
Now I create a form1.scala.html in view package. I want to render this html in controller like this "return ok(form1.render());". But it cannot. Why I cannot?
I have checked Play-Sample(example: 'form' application). In this application controller class, he used form1.scala.html, form2.scala.html, summary.scala.html and so on defend by himself.It's Ok. But I cannot use like this.
views is a regular Scala/Java package, but when the template compiler runs it adds a 'html' package under that which it places the compiled templates in. So the source app/views/myTemplate.scala.html results in the function views.html.myTemplate
The view templates are functions themselves, not classes or objects, the filename becomes the function name, there is no render() method.
So if you have the file app/views/myTemplate.scala.html you will be able to use it like this:
Ok(views.html.myTemplate())
or
import views.html.myTemplate
Ok(mytemplate())