codekanzlei / cake-attachments
CakePHP 文件附件插件
Requires
- php: >=7.3.0
- ext-imagick: *
- cakephp/cakephp: >= 4.0
- codekanzlei/cake-frontend-bridge: ^4.0.0-RC2
Requires (Dev)
- phpstan/phpstan: ^0.11
- scherersoftware/coding-standard: ^4.0.0
This package is auto-updated.
Last update: 2024-09-07 14:15:36 UTC
README
要求
您可以在 composer.json
中找到要求。
- ImageMagick 用于调整图像大小
- cake-frontend-bridge 用于轻松访问由 URL 派生的当前控制器和操作
- ghostscript 用于预览 PDF。在 Mac OS X 上,您可以通过 homebrew 安装 ghostscript
`brew install ghostscript`
CakePHP 3 文件附件处理
注意:此插件依赖于 codekanzlei/cake-frontend-bridge 插件。
安装
1. 在您的 composer.json
中需要插件
"require": {
...
"codekanzlei/cake-attachments": "dev-master",
...
}
2. 使用 composer 包含插件
在您的项目目录中打开终端并运行以下命令
$ composer update
设置与配置
1. 在您的 config/bootstrap.php
中加载插件
Plugin::load('Attachments', ['bootstrap' => false, 'routes' => true]);
请确保也添加 cake-frontend-bridge,因为它是此插件正常工作的必要条件。
Plugin::load('FrontendBridge', ['bootstrap' => false, 'routes' => true, 'autoload' => true]);
2. 在您的项目数据库中创建一个 attachments
表
在您的项目数据库上运行以下 sql 查询。您可以在插件中的 config/schema.sql
文件中找到它。
CREATE TABLE `attachments` (
`id` char(36) NOT NULL,
`filepath` varchar(255) NOT NULL,
`filename` varchar(255) NOT NULL,
`filetype` varchar(45) NOT NULL,
`filesize` int(10) NOT NULL,
`model` varchar(255) NOT NULL,
`foreign_key` char(36) NOT NULL,
`tags` text,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3. 在您的项目文件夹中创建额外的文件夹
在您的项目目录中打开终端并运行以下命令
$ mkdir -p tmp/uploads
$ mkdir -p app_data/attachments
根据您的环境,您可能需要更改这些文件夹的权限。应用程序必须具有读取和写入这些文件夹的权限。
4. 将 JavaScript 文件添加到您的项目中
在您的 webroot/js/app/app_controller.js
中,向 baseComponents
数组添加以下键
'Attachments'
这授予项目使用插件 .js 文件的权限
5. 将附件添加到您的项目中
在您的 config/app.php
中添加以下键
'Attachments' => [
'tmpUploadsPath' => ROOT . '/tmp/uploads/',
'path' => ROOT . '/app_data/attachments/',
'acceptedFileTypes' => '/\.(jpe?g|png)$/i',
'autorotate' => false
],
您还可以在 'acceptedFileTypes' 字段中指定其他可能允许的文件类型,例如 gif|jpe?g|png|pdf|docx|doc|xls|xlsx|tif|tiff|zip
当设置 autorotate 为 true 时,图片附件的视图和预览将根据其 EXIF 数据旋转。
6. 将 AttachmentHelper 添加到您的项目中
在您的 /serc/Controller/AppController.php
中,向 public $helpers
数组添加以下键
'Attachments.Attachments',
由于 cake-frontend-bridge 插件是 Attachments 插件正常工作的必要条件,因此还需要进行一些其他配置。向 $helpers
数组添加以下键
'FrontendBridge' => ['className' => 'FrontendBridge.FrontendBridge'],
在 AppController extends Controller
中使用 FrontendBridge
use \FrontendBridge\Lib\FrontendBridgeTrait;
最后,将 FrontendBridge 键添加到 public $components
'FrontendBridge.FrontendBridge',
7. 在默认布局中包含附件
在您的 src/Template/Layout/default.ctp
中,您需要创建一个新的 div 元素,其中包含附件插件的 UI 元素。
<div class="<?php echo $this->FrontendBridge->getMainContentClasses() ?>">
</div>
注意:请确保包含 <?= $this->fetch('content') ?>
的行是此 <div>
元素的子元素。
用法
1. 设置模型
转到您想使用 Attachments 插件的表。例如,如果您想将文件附加到用户上,请转到 /Model/Table/UsersTable.php
并将其添加到其 initialize()
回调方法中。
$this->addBehavior('Attachments.Attachments');
2. 设置实体
在您的实体中(如果我们继续使用上面的用户示例,这将是在 Model/Entity/User.php
中),确保将 attachments
和 attachment_uploads
添加到您的 $_accessible
属性中。
protected $_accessible = [
'attachments' => true,
'attachment_uploads' => true
];
attachment_uploads
是默认表单字段名称,您可以通过助手和行为的选项进行更改。
3. 设置控制器
确保在您的控制器中包含使用此插件存储的附件。
如果我们继续使用上面的用户示例,您的 Controller/UsersController.php
可能看起来像这样
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => ['Attachments']
])
4. 设置视图
在您的表单中,使用附件助手创建一个附件区域
echo $this->Attachments->attachmentsArea($entity, [
'label' => 'File Attachments',
'formFieldName' => 'attachment_uploads'
]);
助手会自动将CSS和JS依赖项添加到您的 script
和 css
视图块中。如果您不希望这样做,可以在助手的配置中将 includeDependencies
设置为 false
来禁用此行为。
有关需要包含的JS/CSS依赖项,请参阅 AttachmentsHelper::addDependencies()
。
授权
如果您想根据自定义逻辑限制对附件的访问,可以将回调函数传递给行为配置。
$this->addBehavior('Attachments.Attachments', [
'downloadAuthorizeCallback' => function (Attachment $attachment, EntityInterface $relatedEntity, Request $request) {
return false;
}
]);
此回调阻止预览、查看、下载、删除和处理附件。