Hello Reader's ,
Hope you are doing good today.
Today in my blog I am going to explain how you can upload the image using proffer component in CakePHP 3.
First, you need to install Proffer Component in your project directory .Open your terminal and go to your project directory then Run below command.
$ composer require 'davidyell/proffer:~0.6'
After installing Proffer Component you'll need to load the plugin in your config/bootstrap.php file.
Plugin::load('Proffer');
Ones the plugin is loaded in your bootstrap.php file now creates an upload field here we are using articles table which contains articles details with image.
articles table
CREATE TABLE IF NOT EXISTS `articles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) DEFAULT NULL,
`body` text,
`image` varchar(255) NOT NULL,
`image_dir` varchar(255) NOT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
);
Now you need to ensure that the fields are present in your entities $_accessible array.
src/Model/Entity/Article.php
// Make all fields mass assignable except for primary key field "id".
protected $_accessible = [
'*' => true,
'id' => false,
'image'=>true,
'image_dir'=>true,
];
Now Need to Configuring the behaviour in your Table Model.
<?php
// Add the behaviour and configure any options you want
$this->addBehavior('Proffer.Proffer', [
'image' => [ // The name of your upload field
'root' => WWW_ROOT . 'img', // Customise the root upload folder here, or omit to use the default
'dir' => 'image_dir', // The name of the field to store the folder
'thumbnailSizes' => [ // Declare your thumbnails
'square' => [ // Define the prefix of your thumbnail
'w' => 200, // Width
'h' => 200, // Height
'jpeg_quality' => 100
],
'portrait' => [ // Define a second thumbnail
'w' => 100,
'h' => 300
],
],
'thumbnailMethod' => 'gd' // Options are Imagick or Gd
]
]);
Configuring Input field in your templates.
echo $this->Form->create($article,array('type' => 'file');
echo $this->Form->input('title');
echo $this->Form->input('body', ['rows' => '4']);
echo $this->Form->input('image', ['type' => 'file']);
echo $this->Form->button(__('Save Article');
echo $this->Form->end();
I hope this will help you. Please feel free to give us your feedback in comments.
for more details https://github.com/davidyell/CakePHP3-Proffer/blob/master/README.md
0 Comment(s)