-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Yii2 Froala Plugin, Update in accordance with Froala PHP SDK #20
base: master
Are you sure you want to change the base?
Changes from 9 commits
62e0ff9
eb18901
2d5eeff
a60b4a3
6e9f38e
0019b08
9900452
e963fd4
be3f288
87d30e1
2793f4a
e3eabfa
45cc758
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,18 +7,25 @@ | |
|
||
## Installation | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). yii2-froala editor depends on Froala PHP SDK | ||
To use and Install the editor, one must install PHP SDK also. | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require --prefer-dist froala/yii2-froala-editor | ||
php composer.phar require --prefer-dist froala/wysiwyg-editor-php-sdk | ||
php composer.phar require --prefer-dist bower-asset/froala-wysiwyg-editor | ||
|
||
``` | ||
|
||
or add | ||
|
||
``` | ||
"froala/yii2-froala-editor": "^2.6.0" | ||
"froala/yii2-froala-editor": "^2.6.0", | ||
"froala/wysiwyg-editor-php-sdk" : "*", | ||
"bower-asset/froala-wysiwyg-editor": "^2.6.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, there is a compose package: https://packagist.org/packages/froala/wysiwyg-editor. |
||
|
||
``` | ||
|
||
to the require section of your `composer.json` file. | ||
|
@@ -63,65 +70,19 @@ or use with a model: | |
|
||
## Upload example | ||
|
||
Using the basic Yii template make a new folder under /web/ called uploads. | ||
|
||
For controler: | ||
|
||
```php | ||
public function actionUpload() { | ||
$base_path = Yii::getAlias('@app'); | ||
$web_path = Yii::getAlias('@web'); | ||
$model = new UploadForm(); | ||
|
||
if (Yii::$app->request->isPost) { | ||
$model->file = UploadedFile::getInstanceByName('file'); | ||
|
||
if ($model->validate()) { | ||
$model->file->saveAs($base_path . '/web/uploads/' . $model->file->baseName . '.' . $model->file->extension); | ||
} | ||
} | ||
|
||
// Get file link | ||
$res = [ | ||
'link' => $web_path . '/uploads/' . $model->file->baseName . '.' . $model->file->extension, | ||
]; | ||
|
||
// Response data | ||
Yii::$app->response->format = Yii::$app->response->format = Response::FORMAT_JSON; | ||
return $res; | ||
} | ||
``` | ||
|
||
For model: | ||
Using the Froala PHP SDK with Froala Editor widget, the first step would be to add the configuration in web.php config file, make an entry for Froala Module | ||
in the Config Array. | ||
|
||
```php | ||
namespace app\models; | ||
use yii\base\Model; | ||
use yii\web\UploadedFile; | ||
|
||
/** | ||
* UploadForm is the model behind the upload form. | ||
*/ | ||
class UploadForm extends Model | ||
{ | ||
/** | ||
* @var UploadedFile|Null file attribute | ||
*/ | ||
public $file; | ||
|
||
/** | ||
* @return array the validation rules. | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
[['file'], 'file'] | ||
]; | ||
} | ||
} | ||
'modules' => [ | ||
'froala' => [ | ||
'class' => '\froala\froalaeditor\Module', | ||
'uploadFolder' => '/uploads/' | ||
] | ||
], | ||
``` | ||
|
||
For the view: | ||
Make sure you have a folder called "uploads" in your web root directory,Now to use the Froala Widget on any view just use the following code in the view: | ||
|
||
```php | ||
<?= \froala\froalaeditor\FroalaEditorWidget::widget([ | ||
|
@@ -132,8 +93,7 @@ For the view: | |
'theme' => 'royal',//optional: dark, red, gray, royal | ||
'language' => 'en_gb' , | ||
'toolbarButtons' => ['fullscreen', 'bold', 'italic', 'underline', '|', 'paragraphFormat', 'insertImage'], | ||
'imageUploadParam' => 'file', | ||
'imageUploadURL' => \yii\helpers\Url::to(['site/upload/']) | ||
'imageUploadParam' => 'file' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shouldn't be required. |
||
], | ||
'clientPlugins'=> ['fullscreen', 'paragraph_format', 'image'] | ||
]); ?> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,8 +29,9 @@ | |
], | ||
"require": { | ||
"yiisoft/yii2": "^2.0", | ||
"froala/wysiwyg-editor": "^2.6.0", | ||
"rmrevin/yii2-fontawesome": "^2.0" | ||
"rmrevin/yii2-fontawesome": "^2.0", | ||
"froala/wysiwyg-editor-php-sdk": "*", | ||
"bower-asset/froala-wysiwyg-editor": "^2.6.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the composer package. |
||
}, | ||
"autoload": { | ||
"psr-4": { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace froala\froalaeditor; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's user Froala\Editor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it would be better to use |
||
|
||
class Module extends \yii\base\Module | ||
{ | ||
public $controllerNamespace = 'froala\froalaeditor\controllers'; | ||
|
||
// Without false it will give "Bad Request (#400) Unable to verify your data submission." | ||
public $enableCsrfValidation = false; | ||
|
||
public $uploadFolder; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace froala\froalaeditor\controllers; | ||
|
||
use yii\web\Controller; | ||
|
||
class DefaultController extends Controller | ||
{ | ||
// Without false it will give "Bad Request (#400) Unable to verify your data submission." | ||
public $enableCsrfValidation = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we set CSRF and pass it along with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or through the |
||
|
||
public function actionUpload() | ||
{ | ||
// Store the image. | ||
try { | ||
$uploadFolder = $this->module->uploadFolder; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use 4 spaces indentation. |
||
|
||
$response = \FroalaEditor_Image::upload($uploadFolder); | ||
echo stripslashes(json_encode($response)); | ||
} | ||
catch (Exception $e) { | ||
http_response_code(404); | ||
} | ||
|
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a composer package as well: https://packagist.org/packages/froala/wysiwyg-editor.