我使用Cakephp 3.2和proffer插件上传图像。
默认情况下,图像的路径如下
/media/files/<tablename>/<primary_key>/<filename>
每当在同一个表中插入新行时,就会根据其主键创建一个新文件夹。
我想上传一个表的所有图像到同一目录。表示像
这样的路径/media/files/<tablename>/<filename>
我正在使用事件监听器,按照提供的文档。
这是我的SellersTable.php
<?php
namespace AppModelTable;
use CakeORMQuery;
use CakeORMRulesChecker;
use CakeORMTable;
use CakeValidationValidator;
use CakeEventEvent;
class SellersTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$listener = new AppEventUploadFileNameListener(); // line 23
$this->eventManager()->on($listener);
$this->table('sellers');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Proffer.Proffer', [
'profile_picture' => [
'root' => Configure::read('ArgoSystems.media.upload') . DS . 'files',
'dir' => 'dir'
]
]);
}
/**
* Default validation rules.
*
* @param CakeValidationValidator $validator Validator instance.
* @return CakeValidationValidator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->requirePresence('first_name', 'create')
->notEmpty('first_name');
$validator
->requirePresence('last_name', 'create')
->notEmpty('last_name');
$validator
->email('email')
->requirePresence('email', 'create')
->notEmpty('email')
->add('email', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator->provider('proffer', 'ProfferModelValidationProfferRules');
$validator
->add('profile_picture', 'proffer', [
'rule' => ['dimensions', [
'min' => ['w' => 100, 'h' => 500],
'max' => ['w' => 100, 'h' => 500],
]],
'message' => 'Image must be of 100 x 500 resolution',
'provider' => 'proffer'
])
->requirePresence('profile_picture', 'create')
->allowEmpty('profile_picture','update');
$validator
->requirePresence('password', 'create')
->notEmpty('password');
return $validator;
}
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['email']));
return $rules;
}
}
在src/Event/
<?php
namespace AppEvent;
use CakeEventEvent;
use CakeEventEventListenerInterface;
use CakeUtilityInflector;
use ProfferLibProfferPath;
class UploadFileNameListener implements EventListenerInterface
{
public function implementedEvents()
{
return [
'Proffer.afterPath' => 'change',
];
}
/**
* Rename a file and change it's upload folder before it's processed
*
* @param Event $event The event class with a subject of the entity
* @param ProfferPath $path
* @return ProfferPath $path
*/
public function change(Event $event, ProfferPath $path)
{
// Detect and select the right file extension
switch ($event->subject()->get('image')['type']) {
default:
case "image/jpeg":
$ext = '.jpg';
break;
case "image/png":
$ext = '.png';
break;
case "image/gif":
$ext = '.gif';
break;
}
// Create a new filename using the id and the name of the entity
$newFilename = $event->subject()->get('id') . '_' . Inflector::slug($event->subject()->get('name')) . $ext;
// set seed
$path->setSeed('profile_picture');
// Change the filename in both the path to be saved, and in the entity data for saving to the db
$path->setFilename($newFilename);
$event->subject('image')['name'] = $newFilename;
// Must return the modified path instance, so that things are saved in the right place
return $path;
}
}
但是这会给出致命错误
错误:未捕获的错误:类'AppModelTableAppEventUploadFileNameListener'未找到/var/www/html/projects/admin/src/Model/Table/SellersTable.php: 23日
从错误消息中可以清楚地看出,它试图用相对于当前类的名称空间的名称空间加载类。试着
$listener = new AppEventUploadFileNameListener();