定制 codekanzlei/cake-attachments 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codekanzlei/cake-attachments

Composer 安装命令:

composer require codekanzlei/cake-attachments

包简介

File Attachments plugin for CakePHP

README 文档

README

License

Requirements

You can find the requirements in composer.json.

  • ImageMagick for resizing images
  • cake-frontend-bridge for easy access to the current controller and action derived from the URL
  • ghostscript for pdf previews. On Mac OS X, you can install ghostscript via homebrew:
`brew install ghostscript`

CakePHP 3 File Attachments Handling

Note: This Plugin depends on the codekanzlei/cake-frontend-bridge Plugin.

Installation

1. require the plugin in your composer.json

	"require": {
		...
		"codekanzlei/cake-attachments": "dev-master",
		...
	}

2. Include the plugin using composer

Open a terminal in your project directory and run the following command:

$ composer update

Setup & Configuration

1. Load the plugin in your config/bootstrap.php

Plugin::load('Attachments', ['bootstrap' => false, 'routes' => true]);

Also be sure to add the cake-frontend-bridge since it is required for this plugin to work properly.

Plugin::load('FrontendBridge', ['bootstrap' => false, 'routes' => true, 'autoload' => true]);

2. Create a table attachments in your project database

Run the following sql-query on your project database. You can find it in the Plugin's config/schema.sql file.

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. Create additional folders in your project folder

Open a terminal in your project directory and run these commands:

$ mkdir -p tmp/uploads
$ mkdir -p app_data/attachments

You might have to change the folder permissions for these folders depending on your environment. The application must have permissions to read and write data into them.

4. Adding JavaScript files to your project

In your webroot/js/app/app_controller.js, add the following key to the baseComponents Array:

'Attachments'

This grants the project permission to use the plugin's .js files

5. Adding Attachments to your project

In your config/app.php, add the following key:

'Attachments' => [
    'tmpUploadsPath' => ROOT . '/tmp/uploads/',
    'path' => ROOT . '/app_data/attachments/',
    'acceptedFileTypes' => '/\.(jpe?g|png)$/i',
    'autorotate' => false
],

Further possible filetypes you want to allow can be specified in the 'acceptedFileTypes' filed, such as gif|jpe?g|png|pdf|docx|doc|xls|xlsx|tif|tiff|zip

When setting autorotate to true, views and previews of picture attachments will be rotated depending on their EXIF data.

6. Adding AttachmentHelper to your project

In your /serc/Controller/AppController.php, add the following keys to the public $helpers Array:

'Attachments.Attachments',

As the cake-frontend-bridge Plugin is required for the Attachments Plugin to work properly, some further configuarion is needed. Add the following key to the $helpers Array:

'FrontendBridge' => ['className' => 'FrontendBridge.FrontendBridge'],

Use the FrontendBridge in your AppController extends Controller:

use \FrontendBridge\Lib\FrontendBridgeTrait;

Lastly, add the FrontendBridge-key to public $components

'FrontendBridge.FrontendBridge',

7. Include Attachments in your default layout

In your src/Template/Layout/default.ctp, you need to create a new div element that contains the UI-elements of the Attachments Plugin.

<div class="<?php echo $this->FrontendBridge->getMainContentClasses() ?>">

</div>

Note: Make sure that the line containing <?= $this->fetch('content') ?> is a child-element of this <div>-Element.

Usage

1. Setting up a Model

Go to the table you want use the Attachments plugin in. For example, if you want to be able to attach files to your Users, go to /Model/Table/UsersTable.php and add the following line to its initialize() callback method:

$this->addBehavior('Attachments.Attachments');

2. Setting up an Entity

In your Entity (if we stick to the Users-example above this would be Model/Entity/User.php), make sure you add attachments and attachment_uploads to your $_accessible property like so:

protected $_accessible = [
    'attachments' => true,
    'attachment_uploads' => true
];

attachment_uploads is the default form field name, which you can change via the Helper's and Behavior's options.

3. Setting up a Controller

Be sure to contain Attachments stored with this plugin in your Controllers.

If we stick to the Users-example above, your Controller/UsersController.php might look something like this:

public function edit($id = null)
{
    $user = $this->Users->get($id, [
        'contain' => ['Attachments']
    ])

4. Setting up a view

In your Forms, use the AttachmentsHelper to create an attachments area:

echo $this->Attachments->attachmentsArea($entity, [
    'label' => 'File Attachments',
    'formFieldName' => 'attachment_uploads'
]);

The Helper will automatically add CSS and JS dependencies to your script and css view blocks. If you don't want that, you can disable this behavior by setting includeDependencies to false in the Helper's config.

See AttachmentsHelper::addDependencies() for the JS/CSS dependencies you need to include.

Authorization

If you would like to restrict access to Attachments based on custom logic, you can pass a callback function to the Behavior config.

$this->addBehavior('Attachments.Attachments', [
    'downloadAuthorizeCallback' => function (Attachment $attachment, EntityInterface $relatedEntity, Request $request) {
        return false;
    }
]);

This callback prevents previewing, viewing, downloading, deleting and manipulating attachments.

codekanzlei/cake-attachments 适用场景与选型建议

codekanzlei/cake-attachments 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 71.46k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2015 年 02 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 codekanzlei/cake-attachments 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codekanzlei/cake-attachments 我们能提供哪些服务?
定制开发 / 二次开发

基于 codekanzlei/cake-attachments 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 71.46k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 22
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-05