yii2框架添加扩展类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了yii2框架添加扩展类相关的知识,希望对你有一定的参考价值。

yii2-gird 插件是Yii2.0的一个扩展。它在官方的girdview基础上扩展了一些实用的功能。

比如:

把表格包装在bootstrap - panel标签下,使之更美观;
Float Header功能,实现滑动表格的时候,表字段至于屏幕上方,方便查看;
新增操作栏说明label;
页面统计功能;
新增重置表格功能;
新增导出表格功能,包括四种常用格式[html、CSV、txt、Excel]。

非常感谢Kartik团队带来的好用的插件。Kartik团队的其他插件也很好用的。推荐试用。

安装yii2-grid

使用Composer安装,windows下cmd执行命令:
参考技术A 使用composer搞定

Composer 无法安装 yii2 扩展

【中文标题】Composer 无法安装 yii2 扩展【英文标题】:Composer is not working for installing yii2 extensions 【发布时间】:2019-12-04 20:51:41 【问题描述】:

我想安装一些 yii2 扩展并想在 yii2 框架中使用它们。使用 Composer 时出现错误。

我也尝试手动添加扩展名并将其内容添加到 composer.json 中仍然没有任何效果。

一些命令包括 composer.phar 文件名,我的主项目存储库中没有这个文件。我尝试使用 composer 命令手动添加 composer.phar 但它仍然没有运行。

当我运行这个命令时,我得到了错误。

composer require dmstr/yii2-adminlte-asset "^2.1"

我收到了这个错误

Cannot create cache directory /home/kaushalendra/.composer/cache  /repo/https---asset-packagist.org/, or directory is not writable.     Proceeding without cache
Cannot create cache directory /home/kaushalendra/.composer/cache/repo/https---repo.packagist.org/, or directory is not writable. Proceeding without cache
Cannot create cache directory /home/kaushalendra/.composer/cache/files/, or directory is not writable. Proceeding without cache
./composer.json has been updated
Cannot create cache directory /home/kaushalendra/.composer/cache    /repo/https---asset-packagist.org/, or directory is not writable.     Proceeding without cache
Cannot create cache directory /home/kaushalendra/.composer/cache/repo/https---repo.packagist.org/, or directory is not writable. Proceeding without cache
Cannot create cache directory /home/kaushalendra/.composer/cache/files/, or directory is not writable. Proceeding without cache
 Loading composer repositories with package information
 Updating dependencies (including require-dev)
 Your requirements could not be resolved to an installable set of packages .
Problem 1- codeception/base 2.5.6 requires ext-curl * -> the requested    PHP extension curl is missing from your system.
- codeception/base 2.5.6 requires ext-curl * -> the requested PHP extension curl is missing from your system.
- codeception/base 2.5.6 requires ext-curl * -> the requested PHP extension curl is missing from your system.
- Installation request for codeception/base (locked at 2.5.6, required as ^2.4.0) -> satisfiable by codeception/base[2.5.6].
To enable extensions, verify that they are enabled in your .ini files:
- /etc/php/7.3/cli/php.ini
- /etc/php/7.3/cli/conf.d/10-opcache.ini
- /etc/php/7.3/cli/conf.d/10-pdo.ini
- /etc/php/7.3/cli/conf.d/20-calendar.ini
- /etc/php/7.3/cli/conf.d/20-ctype.ini
- /etc/php/7.3/cli/conf.d/20-exif.ini
- /etc/php/7.3/cli/conf.d/20-fileinfo.ini
- /etc/php/7.3/cli/conf.d/20-ftp.ini
- /etc/php/7.3/cli/conf.d/20-gettext.ini
- /etc/php/7.3/cli/conf.d/20-iconv.ini
- /etc/php/7.3/cli/conf.d/20-json.ini
- /etc/php/7.3/cli/conf.d/20-phar.ini
- /etc/php/7.3/cli/conf.d/20-posix.ini
- /etc/php/7.3/cli/conf.d/20-readline.ini
- /etc/php/7.3/cli/conf.d/20-shmop.ini
- /etc/php/7.3/cli/conf.d/20-sockets.ini
- /etc/php/7.3/cli/conf.d/20-sysvmsg.ini
- /etc/php/7.3/cli/conf.d/20-sysvsem.ini
- /etc/php/7.3/cli/conf.d/20-sysvshm.ini
- /etc/php/7.3/cli/conf.d/20-tokenizer.ini
  You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.
 Installation failed, reverting ./composer.json to its original content.
I've seen somewhere that using this command it will work but getting this error.
composer global require "fxp/composer-asset-plugin:1.0.0-beta4"

得到了这个错误

[ErrorException]
file_put_contents(./composer.json): failed to open stream: Permission denied 
require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...`

【问题讨论】:

错误明确指出您需要在 PHP 中安装 curl 库 ***.com/questions/19335305/… 的可能重复(或某种重复) Composer install error - requires ext_curl when it's actually enabled的可能重复 【参考方案1】:

    使用以下命令删除权限警告并确保您的用户对全局作曲家文件夹具有权限。

    sudo chown -R kaushalendra:<YOUR_GROUP> /home/kaushalendra/.composer
    

    注意:查找您的用户属于哪个组

    groups kaushalendra
    

    对我来说,它显示以下组

    omeraslam : omeraslam sudo www-data docker
    

    : 之后都是我的用户分配的组,我可以使用omeraslam 作为用户名和omeraslam 作为组所以对我来说命令将是sudo chown -R omeraslam:omeraslam /home/kaushalendra/.composer

    然后在终端上运行以下命令

    sudo apt-get install php-curl
    

    然后重启apache

    sudo service apache2 restart
    

【讨论】:

是的,我终于明白了,这是权限问题。我已经提供了对该文件的许可,但不知何故它没有被完全授予。执行这些步骤后,我能够解决此问题。再次感谢您的帮助。【参考方案2】:

显然,您没有写信给./composer.json的权限

您有两种方法可以解决此问题。通过运行chmod XXX composer.json 向用户或组授予写入权限,或者您可以将文件的所有者更改为作曲家正在使用的用户(他可能已经拥有这些权限)。

上面的XXX应该是数字。您可以阅读 here 这些数字应该是什么,但您可能想要 660 或 770 之类的东西。

有些人可能会使用 777 来解决这个问题,但这应该 !!NOT!!使用,因为您不希望其他人访问文件,除非他们确实需要。

您可以使用chown 更改文件的所有者。它应该类似于 chown user:group composer.json

如果有什么你不明白的地方,请告诉我,我会澄清,因为我知道作为一个新的 linux 用户很难理解权限。

请注意,这仅适用于最后一个错误,但处理其他错误是类似的。您只需要为用户提供对正确文件和/或文件夹使用正确权限的作曲家。您可以使用-R 标志来处理目录。

【讨论】:

是的,这是权限问题。最后我能够删除并能够运行 adminlte 面板。感谢您的帮助。

以上是关于yii2框架添加扩展类的主要内容,如果未能解决你的问题,请参考以下文章

yii2框架随笔35

yii2框架随笔36

yii2 框架怎么调用vendor下的第三方

yii2框架随笔37

不使用 Composer 手动安装 Yii2 扩展

Yii2框架bootstrap样式理解