daccess1/yii2-tilda-api
Composer 安装命令:
composer require daccess1/yii2-tilda-api
包简介
Tilda platform api extension for Yii2
关键字:
README 文档
README
Read in other languages: Русский
Tilda platform api extension for Yii2
This is a fork of nariman924/yii2-tilda-api project, which seems to be abandoned for a couple of years.
Installation
The preferred way to install this extension is through composer.
Either run
composer require daccess1/yii2-tilda-api
or add
"daccess1/yii2-tilda-api": "^1.0"
to the require section of your composer.json file.
Once the extension is installed, include this in components section of your common/config/main-local.php file:
'components' => [ ... 'tilda' => [ 'class' => 'daccess1\tilda\TildaApi', 'publicKey' => '**********', 'secretKey' => '**********', // Change URL if needed. Don't forget to set protocol // to https:// when moving to production. 'assetsUrl' => 'http://'.$_SERVER['HTTP_HOST'].'/tilda', // Change 'public_html' to the frontend web directory if needed // (default Yii2 directory is 'frontend/web') 'assetsPath' => dirname(dirname(__DIR__)).'/public_html/tilda', //Optional default project ID (integer) //'defaultProjectID' => ***** ], ],
Apply required migrations
php yii migrate --migrationPath=@vendor/daccess1/yii2-tilda-api/migrations
Usage
The two most common goals of this extension are saving Tilda pages to lhe local database and then showing them to the end user. The following instruction shows some common usage patterns for this extension. All examples assume that you are using Gii-generated CRUD.
Saving pages
To render the list of the pages from Tilda project, you can use the renderPageSelect method. It's designed to be used with the ActiveForm generated with Gii, and requires no additional setup by default. Simply replace your form field that stores Tilda page id with this code:
<?= Yii::$app->tilda->renderPageSelect($model,'your_field_id'); ?>
This widget takes all the same values as default ActiveForm input field. You can also set the project ID (integer) as a third parameter. If you don't, the defaultProjectID setting will be used. As the result, the HTML select with listed Tilda pages will be rendered into your form, and it's input will be treated as any other model field.
After selecting the page, you now want to save it to the local storage. In order to do so you should update actionCreate and actionUpdate functions of your Controller like this:
if ($model->load(Yii::$app->request->post()) && $model->save()) { //insert this line Yii::$app->tilda->getPage($model->your_field_id); return $this->redirect(['view', 'id' => $model->id]); }
This will save page body HTML code and assets to your local server.
Rendering pages
After your page was saved to the local storage, you can render it with the registerAssets and renderHtml methods of the extension. This will require two steps to implement:
In your controller action register the required assets:
Yii::$app->tilda->registerAssets($this,$model->tilda_page_id);
The first parameter is the current \yii\web\Controller instance, and the second one is the Tilda page ID.
Then in your view you can render HTML like this
<?= Yii::$app->tilda->renderHtml($model->tilda_page_id) ?>
Webhook
Tilda has an option to notify your app about any pages published in your projects via a webhook. However, it has some restrictions on it's usage (see more in Tilda API docs). One of the ways to implement Webhook would be like this:
public function actionWebhook() { $get = \Yii::$app->request->get(); // Check request public key if (!\Yii::$app->tilda->verifyPublicKey($get['publickey'])) throw new \yii\web\ForbiddenHttpException("PublicKey dosen't match"); if (!isset($get['save'])) { // If this is actual Tilda webhook request, // create cURL request client // Set 'baseUrl' to your current webhook Url $client = new Client([ 'transport' => 'yii\httpclient\CurlTransport', 'baseUrl' => 'http://domain.com/index.php?r=tilda/webhook' ]); try { $client->createRequest() ->setMethod('get') ->setData(['save' => 1, 'page' => $get['pageid'], 'publickey' => $get['publickey']]) ->setOptions([ // Setting timeout and data return transfer // options of request to prevent waiting until // page download completes CURLOPT_CONNECTTIMEOUT => 1, CURLOPT_TIMEOUT => 1, CURLOPT_RETURNTRANSFER => false, CURLOPT_FORBID_REUSE => true, CURLOPT_DNS_CACHE_TIMEOUT => 10, CURLOPT_FRESH_CONNECT => true, ]) ->send(); } catch (\yii\httpclient\Exception $e) { // Returning success result to Tilda webhook \Yii::$app->response->format = \yii\web\Response::FORMAT_HTML; return "ok"; } } else { // Else if this is cURL recursive request, perform // page download \Yii::$app->tilda->getPage($get['page']); } \Yii::$app->response->format = \yii\web\Response::FORMAT_HTML; return "ok"; }
Methods
After registering the component and applying required migrations, you can access it's methods anywhere in your code. However, the most common usage case was already described above.
renderPageSelect
Returns ActiveForm-compatible HTML select input, which lists all pages from selected project. If no $projectID is provided the defaultProjectID setting is used instead.
Yii::$app->tilda->renderPageSelect($model,'your_field_id',$projectID);
getPage
Saves tilda page and it's assets to the local database. Note: Tilda's default jQuery (1.10.2) is not downloaded (to prevent conflicts with your Yii2 jQuery asset).
Yii::$app->tilda->getPage($pageID)
registerAssets
Registers page's assets (CSS and JS). The first parameter is the current \yii\web\Controller instance, and the second one is the Tilda page ID.
Yii::$app->tilda->registerAssets($this,$pageID);
renderHtml
Returns HTML code of the selected page.
Yii::$app->tilda->renderHtml($pageID)
listPages
Returns list of pages in project as array of ['id' => 'title'] arrays. If no $projectID is provided the defaultProjectID setting is used instead.
Yii::$app->tilda->listPages($projectID)
loadPage
Returns the array of data form the local copy of the page.
$page = Yii::$app->tilda->loadPage($pageID);
Notes
You should consider that current Tilda core css settings conflict with Bootstrap (provided with Yii2 by default). To avoid conflicts, you could either don't use bootstrap.css and Tilda at the same page, or fix box-sizing property of elements in your own stylesheet.
License
This software is licensed under MIT license. For more information see LICENSE.
daccess1/yii2-tilda-api 适用场景与选型建议
daccess1/yii2-tilda-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 07 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「extension」 「platform」 「yii2」 「tilda」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 daccess1/yii2-tilda-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 daccess1/yii2-tilda-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 daccess1/yii2-tilda-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A custom URL rule class for Yii 2 which allows to create translated URL rules
LinkedIn API PHP SDK with OAuth 2.0 & CSRF support. Can be used for social sign in or sharing on LinkedIn. Examples. Documentation.
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
UI Kit 3 Extension for Yii2
Adds more BBCode
统计信息
- 总下载量: 73
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-02