itcig/sagecontroller 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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:

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 function to expose the returned values to the Twig views/s.
    • Use public static function to use the function within your Twig view/s.
    • Use protected function for internal controller methods as only public methods are exposed to the view. You can run them within __construct.
  • 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.

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 itcig/sagecontroller 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 13
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 2
  • Forks: 43
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-02