te7a-houdini/laravel-trix
Composer 安装命令:
composer require te7a-houdini/laravel-trix
包简介
trix editor for laravel inspired by ActionText for rails
README 文档
README
Configurable Basecamp Trix Editor delivered to your laravel application
inspired by Rails Action Text
Installation
You can install the package via composer:
composer require te7a-houdini/laravel-trix
Then publish the configurations and migrations:
php artisan vendor:publish --provider="Te7aHoudini\LaravelTrix\LaravelTrixServiceProvider"
After the migration has been published then run the migrations to create required tables:
php artisan migrate
then add @trixassets directive at the head tag of your html
<html> <head> @trixassets </head> </html>
Usage
let's assume we have Post model & want to add trix editor.
Using @trix($model, $field, $config = [])
you can use @trix directive inside any view to render trix editor.
<html> <head> @trixassets </head> <body> <!-- notice that content field isn't presented in Post model --> @trix(\App\Post::class, 'content') </body> </html>
Storing Rich Text Fields
now lets try to store content rich text field when hitting submit button.
<html> <head> @trixassets </head> <body> <form method="POST" action="{{ route('posts.store') }}"> @csrf @trix(\App\Post::class, 'content') <input type="submit"> </form> </body> </html>
first add HasTrixRichText trait to your model
namespace App; use Illuminate\Database\Eloquent\Model; use Te7aHoudini\LaravelTrix\Traits\HasTrixRichText; class Post extends Model { use HasTrixRichText; protected $guarded = []; }
then you can easily store any rich text fields by multiple ways:
Post::create(request()->all()); //storing must follow this convention (model lowered class name)-trixFields Post::create([ 'post-trixFields' => request('post-trixFields'), ]);
Render Trix For Existing Model
there's multiple ways to render trix for already existing model
<!-- inside view blade file --> @trix($post, 'content') {!! $post->trix('content') !!} //must use HasTrixRichText trait in order for $model->trix() method work {!! app('laravel-trix')->make($post, 'content') !!}
Render Html For Existing Model
You can render the html content for already existing model
<!-- inside view blade file -->
{!! $post->trixRender('content') !!} //must use HasTrixRichText trait in order for $model->trixRender() method work
Storing Attachment
when uploading a file to trix editor. an ajax request is sent to store_attachment_action in laravel-trix config file. which uses Laravel Storage and then this action returns url if upload is success according to Basecamp Trix api .
the uploaded file will be stored in trix_attachments table as pending attachment.
once model is saved . all pending attachments will have is_pending column = 0
so in order to make storing attachment very easy make sure to use HasTrixRichText trait in your model.
Post::create(request()->all()); //storing must follow this convention (model lowered class name)-trixFields //and for attachment must follow attachment-(model lowered class name)-trixFields Post::create([ 'post-trixFields' => request('post-trixFields'), 'attachment-post-trixFields' => request('attachment-post-trixFields') ]);
Changing Storage Disk
you can change attachment storage disk from laravel-trix config file .
return [ 'storage_disk' => env('LARAVEL_TRIX_STORAGE_DISK', 'public'), 'store_attachment_action' => Te7aHoudini\LaravelTrix\Http\Controllers\TrixAttachmentController::class . '@store', 'destroy_attachment_action' => Te7aHoudini\LaravelTrix\Http\Controllers\TrixAttachmentController::class . '@destroy', ];
or if you want to change the storage disk for specific rich text field you can do that
@trix($post, 'content', ['disk' => 'local'])
Deleting Rich Text Field and Attachments
you can remove related rich text fields and attachments on a model deleting:
class Post extends Model { use HasTrixRichText; protected $guarded = []; protected static function boot() { parent::boot(); static::deleted(function ($post) { $post->trixRichText->each->delete(); $post->trixAttachments->each->purge(); }); } }
Configuration Table
if you want to hide buttons or toolbar you can do this. for more configuration refer to the table below.
@trix($post, 'content', [ 'hideButtonIcons' => ['attach', 'bold'] ]) @trix($post, 'content', [ 'hideTools' => ['text-tools'] ])
| configuration | type | values | description |
|---|---|---|---|
| hideToolbar | Boolean | True or False | hides the the toolbar |
| hideTools | Array | ['text-tools', 'block-tools', 'file-tools', 'history-tools'] | hides group of buttons |
| hideButtonIcons | Array | ['attach', 'bold', 'italic', 'strike', 'link', 'heading-1', 'quote', 'code', 'bullet-list', 'number-list', 'decrease-nesting-level', 'increase-nesting-level'] | hides a single button |
| disk | String | 'local' or 's3' or 'any-disk' | sets the attachment storage per field |
| id | String | 'any-value-you-want' | the id of input which renders trix. check this link . current id follows this convention (model lowered class name)-field-modelId like post-content-1 or post-content-new-model |
| containerElement | String | valid html tag like span or div |
default container tag is set to span you can change it as you want |
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email ahmedabdelftah95165@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
te7a-houdini/laravel-trix 适用场景与选型建议
te7a-houdini/laravel-trix 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 180.3k 次下载、GitHub Stars 达 546, 最近一次更新时间为 2019 年 10 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「te7a-houdini」 「laravel-trix」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 te7a-houdini/laravel-trix 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 te7a-houdini/laravel-trix 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 te7a-houdini/laravel-trix 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
trix editor for laravel inspired by ActionText for rails
it will add generate:response command to help you in scaffolding response classes
Simple package to allow model applies and receives applications from other models
统计信息
- 总下载量: 180.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 546
- 点击次数: 19
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-26