In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to introduce third-party libraries in yii2, for this problem, this article details the corresponding analysis and solution, hoping to help more small partners who want to solve this problem find a simpler and easier way.
composer package
If the third-party code is a published composer package, this integration is often the easiest.
Take Mailer, which supports psr-4, for example, you only need to install it with the composer require command. Once installed, you can use it directly in your code (if necessary, click Mailer's link, and refer to github for instructions on how to use the rules).
Why is this? Why can you use it directly? Did you manually require it?
We know that composer is php package manager, it itself provides an automatic loader, can load all the class files downloaded by composer, that is, as long as we introduce this automatic loading class in the project, all the libraries installed by composer can be used directly.
I know you're trying to say you didn't do anything, and you didn't. This is because we have already required the composer's automatic loader in the application's entry file index.php. If you don't believe it, see if there is such a line of code.
require(__DIR__ . '/../../ vendor/autoload.php');
So, do you understand?
Official loader
However, in fact, there are a large proportion of people who always complain that the composer cannot be installed, and the use of domestic mirrors is also stuck. How can it be broken? Can I download it directly from github?
Of course you can. We also take Mailer above as an example, the naming of this class conforms to psr-4 standard, so we can use yii2's own loader to load it.
First we manually download from github, then unzip the downloaded package and put it under your vendor/bailangzhan/directory, and rename it yii2-swiftmailer. Now the directory structure under vendor/bailangzhan is like this.
├─yii2-swiftmailer│ composer.json│ LICENSE│ Mailer.php│ README.md
Note that we do not use composer installation oh.
We noticed that Mailer's namespace is bailangzhan, so we configured it in the config/main.php file of our application
'aliases' => [
'@bailangzhan' => '@vendor/bailangzhan',
],
Aliases are at the same level as components, so don't misplace them.
Now you just need to operate Mailer like this
$mailer = new \bailangzhan\Mailer(\bailangzhan\Mailer::TYPE_1, $message)
Leveraging third-party own autoloaders
Some students want to say again, oh yeah, what you said above are all class libraries with composer, Ali big fish wood has composer, only one of its own Autoloader class, how to operate this?
Autoloader automatic loading is very convenient, under normal circumstances, we only need to introduce this self-contained Autoloader class in the entry file, you can use it directly in the code. The method of use refers to the method of use of Ali that we want to say below.
Download the sdk of Ali, we put it under the vendor/alidayu directory of the application, i.e. frontend/vendor/alidayu/Autoloader.php, the relevant directory needs to be created by yourself.
Because the sdk of Ali Big Fish is special, we need to define some constants before introducing Autoloader, that is, defined in the TopSdk file and the file has require("Autoloader.php"), so we actually need to introduce this file.
In the index.php file, add the following line before the Yii.php file
require(__DIR__ . '/../ vendor/alidayu/TopSdk.php');
Because there is no specific namespace, so in the code when new Ali is greater than the class in the class library, add a backslash in front of the class name\
For example, Ali is greater than can be used as follows
$c = new \TopClient;
$c ->appkey = $appkey ;
$c ->secretKey = $secret ;
$req = new \AlibabaAliqinFcSmsNumSendRequest;
$req ->setExtend( "" );
$req ->setSmsType( "normal" );
$req ->setSmsFreeSignName( "" );
$req ->setSmsParam( "" );
$req ->setRecNum( "13000000000" );
$req ->setSmsTemplateCode( "" );
$resp = $c ->execute( $req );
Alipay, native require
Speaking of these, some students can't help but complain about the SDK paid by Alipay, what the hell, nothing. What to do, how to use it?
Composer provides us with a lot of convenience, but with composer we can't forget our roots. From the above example we can see that whether it is the automatic loading of the composer or the automatic loading of the yii official or the automatic loading of the third-party code, its essence is to require all relevant classes.
In this case, for Alipay this kind of "three no" third party without composer, does not meet psr-4, no Autoloader, we can only use "killer": manual require!
How to manually require? Take Alipay sdk as an example. The most we may use is alipay.config.php and alipay_submit.class.php below the sdk package. Suppose we download Alipay sdk and place it in the frontend\vendor\alipay directory. We can use it like this.
require_once(Yii::getAlias('@frontend'). "/vendor/alipay/alipay.config.php");
require_once(Yii::getAlias('@frontend'). "/vendor/alipay/lib/alipay_submit.class.php");
Similarly, we use alipay package below the class such as AlipaySubmit, still need to add slashes in front of the class\About yii2 how to introduce third-party class library questions to share here, I hope the above content can have some help for everyone, if you still have a lot of doubts, you can pay attention to the industry information channel to learn more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.