Details of the installation and use of PHP in the YII framework, YII framework
Extensions are specially designed packages that can be used at any time in Yii applications and can be re-released. For example, the yiisoft/yii2-debug extension adds a handy toolbar for debugging at the bottom of each page of your app to help you simply crawl the page generation. You can use extensions to speed up your development process.
Information: The term "extensions" we use in this article refers specifically to YII packages. The term "package" and "library" refer to the usual software packages that are not dedicated to Yii.
Using the extension
To use an extension, you need to install it first. Most extensions are released as Composer packages, and such extensions can be installed in the following two steps:
Modify your app's Composer.json file to indicate which extension (composer package) you want to install.
Run composer install to mount the specified extension.
Note If you have not installed Composer, you need to install it first.
By default, Composer installs the package that is registered in Packagist-the largest open source Composer code base. You can find the extension in packageist. You can also create your own code base and configure Composer to use it. This is useful if you are developing a private extension and want to share it only in your other projects.
Extensions installed through Composer are stored in the Basepath/vendor directory, where BasePath refers to the base path of your app. Because Composer is still a dependency manager, when it installs a package, it also installs all the packages that the package relies on.
For example, to install the yiisoft/yii2-imagine extension, you can modify your Composer.json file as shown in the following example:
{ // ... " Require ": { //... other dependencies " Yiisoft/yii2-imagine ":" * " }}
After the installation is complete, you should be able to see the Yiisoft/yii2-imagine directory in the Basepath/vendor directory. You should also see another imagine/imagine directory where the dependent packages are installed.
Information: Yiisoft/yii2-imagine is Yii maintained by the development team a core extension, all core extensions are centrally managed by Packagist, named Yiisoft/yii2-xyz, where XYZ, different extensions have different names.
Now you can use the installed extensions as part of the app. The following example shows how to use the Yii\imagine\image class provided by the Yiisoft/yii2-imagine extension:
Use Yii;use yii\imagine\image;//to generate a thumbnail image::thumbnail (' @webroot/img/test-image.jpg ', +) ->save (Yii :: Getalias (' @runtime/thumb-test-image.jpg '), [' quality ' = 50]);
Info: Extension classes are automatically loaded by Yii class Autoloader.
Install extensions manually
In rare cases, you may need to manually install some or all of the extensions instead of relying on Composer. To do this, you should:
Download the extended Zip file and extract it to the vendor directory.
If there is, install the Auto loader provided by the extension.
Follow the instructions to download and install all dependent extensions.
If the extension does not provide an autoloader for classes, but also follows the PSR-4 standard, you can use the class Autoloader provided by YII to load the extension class. All you need to do is declare a root alias for the root directory of the extension. For example, suppose you install an extension in the Vendor/mycompany/myext directory and the extension class has a namespace of myext, you can include the following code in the app configuration file:
[' aliases ' = [ ' @myext ' and ' = ' @vendor/mycompany/myext ', ],]
Create an extension
When you need to share your masterpiece with others, you might consider creating an extension. Extensions can include any code you like, such as helper classes, pendants, modules, and so on.
It is recommended that you create extensions according to the terms of the Composer package so that others are easier to install and use.
Following are the basic steps you need to follow to create an extension as a Composer package.
Build a project for your extension and store it in the version control code base, such as github.com. The development and maintenance of the extension should be done in this code base.
In the root directory of the project, build a file named Composer.json that is required for Composer.
Register your extensions in a Composer code base, such as in packagist, so that other users can find and install your extensions with Composer.
Composer.json
Each Composer package must have a Composer.json file in the root directory. The file contains the metadata for the package. You can find the full specifications for this document in the composer manual. The following example shows the Composer.json file for the yiisoft/yii2-imagine extension.
{ //package name ' name ': ' Yiisoft/yii2-imagine ', //package Type ' type ': ' yii2-extension ', ' Description ":" The Imagine integration for the YII framework ", " keywords ": [" Yii2 "," Imagine "," image "," helper "],
"license": "Bsd-3-clause", "support": { "issues": "https://github.com/yiisoft/yii2/issues?labels=ext% 3Aimagine ", " forum ":" http://www.yiiframework.com/forum/", " wiki ":" http://www.yiiframework.com/wiki/", " IRC": "Irc://irc.freenode.net/yii", " source": "Https://github.com/yiisoft/yii2" }, " Authors ": [ { " name ":" Antonio Ramirez ", " email ":" amigo.cobos@gmail.com " } ], // Package dependencies "require": { "yiisoft/yii2": "*", "Imagine/imagine": "v0.5.0" }, // Class autoloading Specs "AutoLoad": { "psr-4": { "yii\\imagine\\": ""}} }
Package Name
Each Composer package should have a unique package name that can be identified from other packages. The format of the package name is Vendorname/projectname. For example, in package name Yiisoft/yii2-imagine, the vendor name and project name are Yiisoft and Yii2-imagine, respectively.
Do not use Yiisoft as your vendor name, as it is reserved for use by Yii's core code.
We recommend that you use yii2-as the prefix for your package name to indicate that it is an extension of Yii 2, for example, Myname/yii2-mywidget. This makes it easier for users to tell if they are an extension of Yii 2.
Package Type
It is important to indicate your extension as a yii2-extension type so that it can be identified as a Yii extension when it is installed.
When the user runs composer install an extension, the vendor/yiisoft/extensions.php file is automatically updated to contain the information for the new extension. From this file, the YII application will know which extensions are installed (this information is accessible through yii\base\application::extensions).
Depend on
Your extension relies on Yii (as a matter of course). So you should list it in the Composer.json file (YIISOFT/YII2). If your extension also relies on other extensions or third-party libraries, you need to tie them up. Make sure you also list the appropriate version constraints (such as 1.*, @stable) for each dependent package. When you publish a stable version, the package you rely on should also use a stable version.
Most JAVASCRIPT/CSS packages are managed by Bower, not Composer. You can use the Composer asset plugin to enable this type of package to be managed by Composer. If your extension relies on the Bower package, you can simply list it in the dependencies of the Composer.json file as shown in the following example.
{ //package dependencies "require": { "bower-asset/jquery": ">=1.11.*" }}
The code above indicates that the extension relies on the jquery Bower package. In general, you can specify the Bower package with Bower-asset/packagename in Composer.json and the NPM package with Npm-asset/packagename. When Compower installs the Bower and NPM packages, the contents of the package are installed by default to @vendor/bower/packagename and @vendor/npm/packages respectively. The two directories can also be pointed to by @bower/PackageName and @npm/packagename aliases, respectively.
Automatic loading of classes
In order for your class to be loaded automatically by the Yii class Autoloader or the Composer class autoloader, you should specify the AutoLoad entry in the Composer.json as follows:
{ // .... " AutoLoad ": { " psr-4 ": { " yii\\imagine\\ ":" " }} }
You can list one or more root namespaces and their file directories.
When the extension is installed into the app, Yii creates an alias for each of the listed root namespaces that points to the directory that corresponds to the namespace. For example, the AutoLoad entry declaration above will correspond to the alias @yii/imagine.
Recommended Practice
Extensions mean that they will be used by others, and you usually need extra effort in development. Below we introduce some common and recommended practices to create high-quality extensions.
Name space
To avoid conflicts and to enable classes in your extension to be loaded automatically, your class should use namespaces and make the class name conform to the PSR-4 standard or PSR-0 standards.
The namespace of your class should start with Vendorname\extensionname, where ExtensionName and the project name are the same except that it does not have a yii2-prefix. For example, for a yiisoft/yii2-imagine extension, we use Yii\imagine as the namespace for its class.
Do not use Yii, Yii2, or Yiisoft as your vendor name. These names have been reserved for use by Yii kernel code.
Bootstrap boot for Class
Sometimes, you might want your extension to execute some code in the app's bootstrap process. For example, your extension might want to respond to an app's BeginRequest event and do some setup work for the environment. Although you can instruct the extended consumer to explicitly append (BIND) the event handle in your extension to the BeginRequest event, a better approach is to do it automatically.
To achieve this goal, you can create a so-called bootstrapping class (bootstrap) Implementation Yii\base\bootstrapinterface interface. For example
namespace Myname\mywidget;use yii\base\bootstrapinterface;use Yii\base\application;class MyBootstrapClass Implements bootstrapinterface{public function Bootstrap ($app) { $app->on (application::event_ Before_request, function () { //do something here });} }
Then you list this class in the Composer.json file, as shown below,
{ // ... " Extra ": { " bootstrap ":" Myname\\mywidget\\mybootstrapclass " }}
When this extension is installed into the application, Yii automatically instantiates the bootstrap class and calls its Yii\base\bootstrapinterface::bootstrap () method during each request's bootstrap process.
manipulating databases
Your extension may want to access the database. Do not assume that applications that use your extension always use Yii:: $db as a database connection. You should declare a DB attribute in the class that requires access to the database. This property allows your extended users to customize which DB connection your extension uses. For example, you can refer to the Yii\caching\dbcache class to see how it declares and uses the DB attribute.
If your extension needs to create a specific database table, or modify the database structure, you should
Provide data migration to manipulate the database's structural modifications, rather than using SQL text files;
Try to make the migrated file suitable for different DBMS;
Avoid using the Active Record in the migration file.
Using Assets
If your extension is a pendant or a module type, it may be necessary to use some assets. For example, a module might want to display some pages that contain images, JavaScript, and CSS. Since the extended files are placed under the same directory and the Web cannot be read after installation, you have two choices to make these asset file directories available for reading through the Web:
Let the extended user manually copy these asset files to a specific Web folder that can be read;
Declare a asset bundle and rely on the asset publishing mechanism to automatically copy these files (the files listed in the asset bundle) to a Web-readable folder.
We recommend that you use the second method so that other people can use your extension more easily.
Internationalization and Localization
Your extension may be used in apps that support different languages! So, if your extension is going to display content to end users, you should try to internationalize and localize, in particular,
If the extension displays information for the end user, the information should be wrapped in yii::t () so that it can be translated. Information that is only given to developers (such as internal exception information) does not need to be translated.
If the extension displays numbers, dates, and so on, you should format them with the appropriate formatting rules in Yii\i18n\formatter.
Test
You must want your extension to run flawlessly without causing problems and trouble to others. To do this, you should do the testing before the public release.
It is recommended that you create test cases and do full coverage testing of your extensions, rather than relying solely on manual testing. Each time you publish a new version, simply run these test cases to make sure everything is intact. YII provides test support to make it easier for you to write unit, acceptance, and functional tests.
Version control
You should set a version number (such as 1.0.1) for each extension. We recommend that you name the version number when referring to semantic versioning decide what version number to use.
Release
To make others aware of your extensions, you should publish them publicly.
If you are releasing an extension for the first time, you should register it in the Composer code base, such as Packagist. After that, all you need to do is create a tag (such as v1.0.1) in the version Management library and then notify the Composer code base. Others will be able to find the new release and install and update the extension through the Composer code base.
In addition to the code files when you publish your extension, you should also consider including the following to help others understand and use your extensions:
The Readme file in the root directory: it describes what your extension is and how it is installed and used. We recommend that you write in Markdown format and name the file readme.md.
Modify log file under the root directory: It lists what changes have been made to each version of the publication. The file can be written with the Markdown radicals and named Changelog.md.
Upgrade files under the root directory: It gives guidance on how to upgrade the extension from other versions. The file can be written with the Markdown radicals and named Changelog.md.
Getting Started Guide, demo code, screenshot diagram, etc.: If your extension provides a number of features that are not fully described in the Readme file, you will need to use these files.
API Documentation: Your code should be well documented, making it easier for others to read and understand. You can learn how to document your code by referencing Object class file.
Info: Your code comments can be written in Markdown format. The Yiisoft/yii2-apidoc extension provides you with a beautiful API document generated from your code comments.
Info: Although not required, we recommend that your extension follow one of the coding specifications. You can refer to the core framework code style.
Core extensions
Yii provides the following core extensions, developed and maintained by the Yii development team. These extensions are all registered in Packagist:
- Yiisoft/yii2-apidoc: Provides an extensible, efficient API documentation generator. The API documentation for the core framework is also generated using it.
- Yiisoft/yii2-authclient: Provides a common set of authentication clients, such as the Facebook OAuth2 client, GitHub OAuth2 client.
- Yiisoft/yii2-bootstrap: Provides a set of pendants that encapsulate bootstrap components and plug-ins.
- Yiisoft/yii2-codeception: Provides codeception-based test support.
- Yiisoft/yii2-debug: Provides debugging support for YII applications. When the extension is used, a debug toolbar is displayed at the bottom of each page. The extension also provides a separate page to display more detailed debugging information.
- Yiisoft/yii2-elasticsearch: Provides support for the use of Elasticsearch. It contains basic query/search support and implements the active record mode so that you can store activity records in Elasticsearch.
- Yiisoft/yii2-faker: Provides support for using Faker to generate simulation data for you.
- Yiisoft/yii2-gii: Provides a page-based code generator that is highly scalable and can be used to quickly generate models, forms, modules, crud, and more.
- Yiisoft/yii2-imagine: Provides a common image processing function based on imagine.
- Yiisoft/yii2-jui: Provides a set of widgets that encapsulate the JQuery UI and their interaction.
- YIISOFT/YII2-MONGODB: Provides support for the use of MongoDB. It contains basic queries, activity records, data migration, caching, code generation and other features.
- Yiisoft/yii2-redis: Provides support for the use of Redis. It contains basic queries, activity records, caches, and other features.
- Yiisoft/yii2-smarty: Provides a template engine based on the smarty.
- Yiisoft/yii2-sphinx: Provides support for the use of Sphinx. It contains basic queries, activity records, code generation and other features.
- Yiisoft/yii2-swiftmailer: Provides a swiftmailer-based mail delivery feature.
- Yiisoft/yii2-twig: Provides a template engine based on the twig.
Articles you may be interested in:
- A detailed description of the use of the front-end resource bundle in PHP's YII framework
- Introduction to some advanced usage of caching in the YII framework of PHP
- In-depth parsing of PHP's caching capabilities in the YII framework
- The use of view view in the Yii framework of PHP is advanced
- A detailed approach to creating views and rendering views in the PHP yii framework
- A tutorial on model models in the YII framework of PHP
- Controller controllers in the YII framework of PHP
- The method of removing the binding behavior of a component in PHP's YII framework
- The definition and binding methods of behavior in the YII framework of PHP
- In-depth explanation of properties in the Yii framework of PHP
http://www.bkjia.com/PHPjc/1117042.html www.bkjia.com true http://www.bkjia.com/PHPjc/1117042.html techarticle in detail, the installation and use of the extensions in the YII framework of PHP, the YII framework extension is a specially designed software package that can be used at any time in yii applications and can be re-released. For example, Yiisoft/yi ...