itcig/sagecontroller
Composer 安装命令:
composer require itcig/sagecontroller
包简介
WordPress package to enable a basic controller when using Timber/Twig with Sage 9
关键字:
README 文档
README
WordPress package to enable a controller when using Timber/Twig with Sage 9. This also requires heavy modification of Sage 9 to use.
Installation
Composer:
Please note that Controller is no longer an mu-plugin and is now a Composer theme depedency.
Browse into the Sage theme directory and run;
$ composer require itcig/sagecontroller:2.0.2
Requirements:
- PHP >= 7.0
Setup
By default, create folder app/Controllers/ within your theme directory.
Alternatively, you can define a custom path using the filter below within your themes resources/functions.php file;
add_filter('sober/controller/path', function () { return dirname(get_template_directory()) . '/app/Custom-folder'; });
The controller will autoload PHP files within the above path and its subdirectories.
Usage
Creating a basic Controller:
- Controller files follow the same hierarchy as WordPress.
- Extend the Controller Class— it is recommended that the class name matches the filename.
- Create methods within the Controller Class;
- Use
public functionto expose the returned values to the Twig views/s. - Use
public static functionto use the function within your Twig view/s. - Use
protected functionfor internal controller methods as only public methods are exposed to the view. You can run them within__construct.
- Use
- Return a value from the public methods which will be passed onto the Twig view.
- Important: The method name is converted to snake case and becomes the variable name in the Twig view.
- Important: If the same method name is declared twice, the latest instance will override the previous.
Examples:
The following example will expose $images to resources/views/single.twig
app/controllers/Single.php
<?php namespace App; use Cig\Sage\Controller\Controller; class Single extends Controller { /** * Return images from Advanced Custom Fields * * @return array */ public function images() { return get_field('images'); } }
resources/views/single.twig
{% if(images|length) %}
<ul>
{% for image in images %}
<li><img src="{{ image.sizes.thumbnail }}" alt="{{ image.alt }}"></li>
{% endfor %}
</ul>
{% endif %}
Creating Components;
You can also create reusable components and include them in a view using PHP traits.
app/controllers/partials/Images.php
<?php namespace App; trait Images { public function images() { return get_field('images'); } }
You can now include the Images trait into any view to pass on variable $images;
app/controllers/Single.php
<?php namespace App; use Cig\Sage\Controller\Controller; class Single extends Controller { use Images; }
Using Static Methods;
You can use static methods as a pass-thru method that returns content from your controller.
This is useful if you are within the loop and want to return data for each post item individually by passing in a $post_id.
app/controllers/Archive.php
<?php namespace App; use Cig\Sage\Controller\Controller; class Archive extends Controller { public static function callback_method($arg = null) { return my_callback($arg); } }
resources/views/archive.php
{% extends "base.twig" %}
{% block content %}
{{ callback_method() }}
{% endblock %}
Inheriting the Tree/Heirarchy;
By default, each Controller overrides its template heirarchy depending on the specificity of the Controller (the same way WordPress templates work).
You can inherit the data from less specific Controllers in the heirarchy by implementing the Tree.
For example, the following app/controllers/Single.php example will inherit methods from app/controllers/Singular.php;
app/controllers/Single.php
<?php namespace App; use Cig\Sage\Controller\Controller; use Cig\Sage\Controller\Module\Tree; class Single extends Controller implements Tree { }
If you prefer you can also do this;
<?php namespace App; use Cig\Sage\Controller\Controller; class Single extends Controller { protected $tree = true; }
You can override a app/Controllers/Singular.php method by declaring the same method name in app/Controllers/Single.php;
Creating Global Properties;
Methods created in app/Controllers/App.php will be inherited by all views and can not be disabled as resources/views/layouts/app.php extends all views.
app/Controllers/App.php
<?php namespace App; use Cig\Sage\Controller\Controller; class App extends Controller { public function siteName() { return get_bloginfo('name'); } }
Disable Option;
protected $active = false;
Twig Debugging;
Coming soon
Updates
Composer:
- Change the composer.json version to ^2.0.2
- Check CHANGELOG.md for any breaking changes before updating.
$ composer update
WordPress:
Includes support for github-updater to keep track on updates through the WordPress backend.
- Download github-updater
- Clone github-updater to your sites plugins/ folder
- Activate via WordPress
Special Thanks
- Most of the leg work here was done by Daren Jacoby so show him some love
- For Controller updates and other WordPress dev, follow @withjacoby
itcig/sagecontroller 适用场景与选型建议
itcig/sagecontroller 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.54k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「wordpress」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 itcig/sagecontroller 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 itcig/sagecontroller 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 itcig/sagecontroller 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Must-use plugin integrating WordPress with the Upsun platform: environment awareness, router-cache friendliness, safe preview clones, deploy migrations, Site Health checks, and a wp upsun CLI command.
Inertia.js server-side adapter for PHP. Handles full visits, Inertia XHR visits, asset-version 409 handshakes and partial reloads. Framework-agnostic, with transparent WordPress integration.
Adds Cache-Tag HTTP response headers for singular WordPress content and purges Cloudflare by tag when content changes.
AI Chatbot & Helpdesk Agent for WooCommerce. Connects your store to Egentify for AI-powered chat, order tracking, email, and ticketing.
MIDDAG's reference architecture for a WordPress plugin — composes the OSS framework/wordpress/ui libraries end to end.
Assets helper package for the Hybrid Core framework.
统计信息
- 总下载量: 1.54k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 13
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-02-02