2016-08-18 2 views
0

저는 Cakephp 3.2 및 proffer 플러그인을 사용하여 이미지를 업로드하고 있습니다.cakephp 이벤트 리스너를 찾을 수 없습니다.

/media/files/<tablename>/<primary_key>/<filename> 

새로운 행은 새로운 폴더 기본 키에 의해 생성되는 동일한 테이블에 삽입 될 때마다 다음과 같이

기본적으로 화상의 경로이다.

테이블의 모든 이미지를 같은 디렉토리에 업로드하려고합니다.

/media/files/<tablename>/<filename> 

나는 proffer 설명서에서 주어진대로 이벤트 수신기를 사용하고 있습니다.

SellersTable.php

<?php 
namespace App\Model\Table; 

use Cake\ORM\Query; 
use Cake\ORM\RulesChecker; 
use Cake\ORM\Table; 
use Cake\Validation\Validator; 
use Cake\Event\Event; 

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 App\Event\UploadFileNameListener(); // 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 \Cake\Validation\Validator $validator Validator instance. 
    * @return \Cake\Validation\Validator 
    */ 
    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', 'Proffer\Model\Validation\ProfferRules'); 

     $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; 
    } 
} 

하고

<?php 
namespace App\Event; 

use Cake\Event\Event; 
use Cake\Event\EventListenerInterface; 
use Cake\Utility\Inflector; 
use Proffer\Lib\ProfferPath; 

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; 
    } 
} 

src/Event/에게 UploadFileNameListener.php를 생성하지만이

Error: Uncaught Error: Class 'App\Model\Table\App\Event\UploadFileNameListener' not found in /var/www/html/projects/admin/src/Model/Table/SellersTable.php:23

로 치명적인 오류를주고있다

답변

1

오류 메시지에서 현재 클래스의 네임 스페이스를 기준으로 네임 스페이스가있는 클래스를로드하려고 시도하는 것이 확실합니다. 시도해보십시오.

$listener = new \App\Event\UploadFileNameListener();