腾讯云新客专属福利
  1. PHP+MySQL数据库教程
  2. PHP进阶教程
  3. ThinkPHP 3.0/3.1 教程

PHP Composer require:获取并安装一个包(Package)

最后更新:2020-01-10 16:12阅读:147

我们使用 PHP Composer,最常用的功能当然是获取并安装第三方库包在我们项目里了。我们这里用一个例子来学习怎么使用 PHP Composer 获取并安装第三方库包phpword。

环境准备

1、假设我们的项目是在:D:\study文件夹下,并且通过了虚拟主机配置,可以在浏览器通过http://localhost/或者其他配置的虚拟域名可以访问该文件夹下的php文件。

2、按照上一篇文章《安装PHP Composer》的介绍,已经安装了PHP Composer。

composer require

win+r 快捷键打开cmd命令对话框,在命令行模式下切换到D:\study文件夹下:

D:
cd study

使用 composer require 命令来获取 phpword:

composer require phpoffice/phpword

执行该命令的显示结果:

D:\study>composer require phpoffice/phpword
Using version ^0.17.0 for phpoffice/phpword
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 4 installs, 0 updates, 0 removals
  - Installing pclzip/pclzip (2.8.2): Downloading (100%)
  - Installing phpoffice/common (0.2.9): Downloading (100%)
  - Installing zendframework/zend-escaper (2.6.1): Downloading (100%)
  - Installing phpoffice/phpword (0.17.0): Downloading (100%)
phpoffice/phpword suggests installing ext-gd2 (Allows adding images)
phpoffice/phpword suggests installing dompdf/dompdf (Allows writing PDF)
Package zendframework/zend-escaper is abandoned, you should avoid using it. Use
laminas/laminas-escaper instead.
Writing lock file
Generating autoload files

打开E:\study文件夹,发现多了一个vendor文件夹,以及composer.json和composer.lock两个文件。

  • vendor:第三方包目录
  • composer.json:包配置文件
  • composer.lock:锁文件

上面各文件及文件夹具体内容本文不再深究,继续我们的例子。

phpword实例

在D:\study文件夹下新建一个word.php文件,内容是phpword自带的例子:

<?php
// require_once 'bootstrap.php'; 替换成下面一句
require_once __DIR__.'/vendor/autoload.php';

// Creating the new document...
$phpWord = new \PhpOffice\PhpWord\PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */

// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)'
);

/*
 * Note: it's possible to customize font style of the Text element you add in three ways:
 * - inline;
 * - using named font style (new font style object will be implicitly created);
 * - using explicitly created font style object.
 */

// Adding Text element with font customized inline...
$section->addText(
    '"Great achievement is usually born of great sacrifice, '
        . 'and is never the result of selfishness." '
        . '(Napoleon Hill)',
    array('name' => 'Tahoma', 'size' => 10)
);

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
    $fontStyleName,
    array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
    '"The greatest accomplishment is not in never falling, '
        . 'but in rising again after you fall." '
        . '(Vince Lombardi)',
    $fontStyleName
);

// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
$objWriter->save('helloWorld.odt');

// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');

这个例子代码在phpword下的MD文件(E:\study\vendor\phpoffice\phpword\README.md)里有,只是在第一行,我们把require_once做了替换。

在浏览器运行该PHP文件:http://localhost/word.php

执行后如果正常,浏览器页面没有任何输出,但是在E:\study文件夹下,生成了3个文件:helloWorld.docx、helloWorld.odt和helloWorld.html。这3个文件就是通过phpword这个第三方库包生成的,更多使用phpword的内容我们就不展开了。

通过这个例子说明,使用PHP Composer来下载并安装第三方库包到项目中,还是很简单的。


本章节内容共分 3 部分:
  1. 1. PHP Composer 是什么?有什么用?
  2. 2. 安装 PHP Composer
  3. 3. PHP Composer require:获取并安装一个包(Package)