slotmachine/slotmachine 问题修复 & 功能扩展

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

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

slotmachine/slotmachine

Composer 安装命令:

composer require slotmachine/slotmachine

包简介

A dynamic page content container for PHP 5.3. Designed to power landing pages and make webpage prototypes

README 文档

README

Each 'slot' on a page can have its content changed by query parameters, allowing for any possible number of permutations of content on the same page.

This is especially useful for marketing and creating landing pages for A-B testing or for prototyping a design of a webpage without cluttering your templates with a bunch of if-statements.

For example, visiting exmaple.com/page?h=1&b=1 and exmaple.com/page?h=2&b=2 will give you a page with a different headline and body if we were to assign the following:

headline:
    1: Book your test drive
    2: Test drive our new vehicle
    
body:
    1: Fill in this quick form and one of our representatives will call you back
    2: Request a callback for a 48 hour test drive today 

Works with PHP 5.3 through PHP 8 and HHVM.

Version 1.0 is fully released, build on top of Pimple 1.0 and Symfony HttpFoundation 2.8.

Version 2.0 provides compatibility with PHP 7+, Symfony HttpFoundation 3+ and Pimple 2+, but still works with older versions of PHP and these packages.

Build Status Scrutinizer Quality Score

Latest Stable Version Total Downloads Latest Unstable Version License

Since November 2012, SlotMachine has been used on several live web pages with tens of thousands of hits a day, mostly landing pages from Facebook ads.

Concept

For those working in digital marketing, a effective landing page is always required for an advertisement to lead to. This is more effective if the page matches the content and design of the ad. However if one has many ads, many static pages would therefore be required. Sometimes only one feature on the page such as the headline or the border color of a container would want to be different to another page.

SlotMachine is a PHP library that allows for one HTML page to display different variants of content on the fly. These can be defined beforehand or with a query string. (ie. example.com/landingpage?h=2&c=3)

Think of a bus that changes its blinds (assuming they still use blinds instead of a matrix) to display different combinations of what route number its running, what places it passes through and what its destination is. In SlotMachine, that would be three slots.

Basic Usage

  1. Install with Composer
  2. Set up the configuration and data
  3. Create your page
<?php

// your-landing-page.php

require __DIR__.'/vendor/autoload.php';

$data = include __DIR__.'/slotmachine.config.php';

$slots = new SlotMachine\SlotMachine($data);

$headline    = $slots->get('headline');
$body        = $slots->get('body');
$description = $slots->get('description');
$image       = $slots->get('image');

?>

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title><?=$headline?> :: My Site</title>
    <meta name="description" content="<?=$description?>">
</head>
<body>
    <h1><?=$headline?></h1>
    <p><?=$body?></p>
    <img src="<?=$image?>" alt="Featured Image" />
    <small>© <?php echo date('Y'); ?> My Site</small>
</body>
</html>

Installation

First, Download Composer from the command line

$ cd path/to/your/project
$ curl -s https://getcomposer.org/installer | php

Then create a composer.json file in your project with the most recent and widely compatible version 2

{
    "require": {
        "slotmachine/slotmachine": "~2.0"
    }
}

Or to use the older, more stable version (currently v1.0)

{
    "require": {
        "slotmachine/slotmachine": "~1.0"
    }
}

Then install with the following command

$ php composer.phar install

Configuring and Setting Data

All new instances of SlotMachine\SlotMachine will take an array for its configuration. Below is an example that would be used with the page example above.

<?php
// slotmachine.config.php

return array(
    'reels' => array(
        'headline' => array(
            'cards' => array(
                'Join our free service today.',
                'Welcome, valued customer.',
                'Welcome back, {user}!',
                'Check out our special offers'
            ),
        ),
        'body' => array(
            'cards' => array(
                0 => 'Time is of the essence, apply now!',
                1 => 'Get a discount today',
                2 => 'Merry Christmas'
            ),
            'aliases' => array(
                'xmas' => 2
            )
        ),
        'description' => array(
            'cards' => array(
                0 => 'Acme Corp. Specialists for anvils.',
                1 => 'Special offer only online at Acme.',
                9001 => 'Acme has anvils for all occasions.',
            ),
            'aliases' => array(
                '_default' => 9001
            ),
            'undefined_card' => 'DEFAULT_CARD'
        ),
    ),
    'slots' => array(
        'headline' => array(
            'reel' => 'headline',
            'keys'  => array('h'),
            'nested' => array(
                'user'
            )
        ),
        'body' => array(
            'reel' => 'body',
            'keys'  => array('c'),
        ),
        'description' => array(
            'reel' => 'description',
            'keys'  => array('c'),
        ),
        'user' => array(
            'reel' => array(
                'cards' => array(
                    0 => 'valued customer',
                    1 => 'Peter',
                    2 => 'Lois',
                    3 => 'Brian',
                    4 => 'Chris',
                    5 => 'Meg',
                    6 => 'Stewie'
                ),
            ),
            'keys'  => array('uid'),
        ),
    ),
);

Usage

Basic

The values returned from a slot are called cards.

To get a card from a single slot, pass the name of the slot to get() method

$slots->get('headline');

To get a card from every slot in the SlotMachine, use the all() method.

$slot = $slots->all();

echo $slot['headline'];

To override the default card returned from a slot, pass the card's array key to the second argument of the get() method.

$slots->get('headline', 4);

Assigning multiple HTTP query parameters to a slot

If you would like example.com/?app_data[i]=1 and example.com/?i=1 to render the same result, just assign an array of HTTP query parameters to the keys attribute. This is useful for passing parameters to the Facebook Page Tab, but not having to use app_data each time.

$config = array(
    //...
    'slots' => array(
        //...
        'hero_image' => array(
            'keys' => array('i', 'app_data[i]'),
            'reel' => 'hero_image'
        )
    )
)

Dependencies

SlotMachine uses the Symfony2 HttpFoundation component to resolve the page based on query string parameters.

The SlotMachine class also extends Pimple, a lightweight dependency injection container, which the Slots are injected into.

Using YAML files for configuration

Although not required, it is recommended that you install the Symfony Yaml component. Just add "symfony/yaml": "~2.0" to your composer.json file. This makes reading the configuration easier and give access to more features that YAML files provide.

The following YAML would be used instead of the PHP array above

#slots.config.yml

slots:
    headline:
        keys: [ h ]
        reel: headline
        nested: [ 'user' ]

    body:
        keys: [ c ]
        reel: body

    description:
        keys: [ c ]
        reel: description

    user:
        keys: [ 'uid' ]
        reel:
            cards:
                0: 'valued customer'
                1: 'Peter'
                2: 'Lois'
                3: 'Brian'
                4: 'Chris'
                5: 'Meg'
                6: 'Stewie'

reels:
    headline:
        cards:
            - 'Join our free service today.'
            - 'Welcome, valued customer.'
            - 'Welcome back, {user}!'
            - 'Check out our special offers'
    body:
        aliases: { xmas: 2 }
        cards:
            0: 'Time is of the essence, apply now!'
            1: 'Get a discount today'
            2: 'Merry Christmas'


    description:
        undefined_card: DEFAULT_CARD
        aliases: { _default: 9001 }
        cards:
            0:    'Acme Corp. Specialists for anvils.'
            1:    'Special offer only online at Acme.'
            9001: 'Acme has anvils for all occasions.'

Integration with Silex

Silex is a micro-framework that uses Symfony2 components. Like SlotMachine, it also extends Pimple, and uses Symfony HttpFoundation. Since SlotMachine was initally designed to work with Silex, a service provider is included as part of the the library.

Note that the the SlotMachineServiceProvider is removed from the 2.0 version of SlotMachine since SlotMachine uses Pimple 2.0 and Silex is still using Pimple 1.0.

Below is an example of using SlotMachine in Silex, including Twig for templating.

<?php

require __DIR__.'/vendor/autoload.php';

use SlotMachine\SlotMachineServiceProvider;
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\HttpFoundation\Request;

$app = new Application();
$app['debug'] = true;

$app->register(new SlotMachineServiceProvider(), array(
    'slotmachine.config'  => Yaml::parse(file_get_contents(__DIR__.'/config/slots.config.yml')),
    'slotmachine.request' => Request::createFromGlobals()
));

$app->register(new TwigServiceProvider(), array(
    'twig.path' => __DIR__.'/templates';
));

$pageData['slot'] = $app['slotmachine']->all();
$pageData['site_name'] = 'My Site';

$app['page_data'] = $pageData;

// If a Silex app is iframed in Facebook, the request type has to match POST first before matching GET
$app->match('/', function() use ($app) {
    return $app['twig']->render('landing_page.html.twig', $app['page_data']);
})->method('POST|GET');

$app->run();

The above would render the following Twig template.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="description" content="{{ slot.description }}">
    <title>{{ slot.headline }} :: {{ site_name }}</title>
</head>
<body>
    <h1>{{ slot.headline|upper }}</h1>
    <p>{{ slot.body }}</p>
    <small>© {{ "now"|date('Y') }} {{ site_name }}</small>
</body>
</html>

Assign all slots to one HTTP query parameter - Working with apps for the Facebook Page Tab

Incompability with Symfony 3

Unfortunately, this feature will not work for installations of Symfony 3 or installations of HttpFoundation 3.0 and above! See symfony/symfony#14039

Usage

Whilst a normal query string combination is used to resolve cards and render the page uniquely...

http://example.com/campaign/landingpage?a=1&b=2&c=3

... and given the following configuration that will resolve this query...

slots:
    foo:
        keys: [ a ]
        reel: foo
    bar:
        keys: [ b ]
        reel: bar
    baz:
        keys: [ c ]
        reel: baz

However, you can assign all of them to one parameter, so the query string would use array keys. This is required for Facebook apps and landing pages being accessed through the Facebook Page Tab as Facebook will only whitelist the app_data query parameter and ignore others.

http://mycampaign.facebook.com/landingpage?app_data[a]=1&app_data[b]=2&app_data[c]=3

Here is the following configuration which will resolve this query:

slots:
    foo:
        keys: [ "q[a]" ]
        reel: foo # "[]" must be in quotes so not to be confused for a YAML array
    bar:
        keys: [ "q[b]" ]
        reel: bar
    baz:
        keys: [ "q[c]" ]
        reel: baz

Run Tests with PHPUnit

These commands assumes that PHPUnit and Composer are installed globally on your system.

$ cd path/to/SlotMachine/
$ composer install
$ phpunit

Found a bug? Missing feature?

Please feel free to create a new issue on the GitHub repository https://github.com/archfizz/slotmachine

slotmachine/slotmachine 适用场景与选型建议

slotmachine/slotmachine 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 378 次下载、GitHub Stars 达 8, 最近一次更新时间为 2013 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dependency injection」 「container」 「dynamic」 「Advertising」 「prototyping」 「marketing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 slotmachine/slotmachine 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 slotmachine/slotmachine 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 378
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 8
  • 点击次数: 30
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-01-12