承接 fullsix/wordpress-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

fullsix/wordpress-bundle

Composer 安装命令:

composer require fullsix/wordpress-bundle

包简介

Let WordPress handle Symfony2's views

README 文档

README

This is a work in progress

This bundle tries to integrate WordPress with Symfony2, in the way that WordPress is used to handle the views, and Symfony2 is used to handle the controller.

This bundle should be used when you want to add Symfony2 controllers to an existing WordPress website, for example. This is really basic but in fact it works quite well, and we have implemented this mechanism on various wordpress instances, one of them containing two blogs, using various languages (using the excellent WPML plugins).

It needs Symfony >= 2.2 in order to interpret Twig tags inside WordPress content.

Installation

Download and install the bundle

Add the following dependencies to your projects composer.json file:

"require": {
    # ..
    "fullsix/wordpress-bundle": "dev-master"
    # ..
}

Register the bundle in the kernel

<?php
// app/AppKernel.php

public function registerBundles() {
    $bundles = array(
        // ...
        new FullSIX\Bundle\WordPressBundle(),
        // ...
    );
}

Install WordPress

Install WordPress in your web directory and launch its configuration.

Install the WordPress plugin

A simple WordPress plugin is needed to interpret the Twig content inside WordPress. Install it from here.

Modify your app_dev.php

Edit your app_dev.php file and add this line at the beginning:

use FullSIX\Bundle\WordPressBundle\WordPressResponse;

And at the end, replace:

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

With:

$kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
global $container, $response;
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$container = $kernel->getContainer();
if ($response instanceof WordPressResponse) {
    $container->enterScope('request');
    $container->set('request', $request, 'request');
    $targetUrl = $response->getTargetUrl();
    if (!empty($targetUrl)) {
        $_SERVER['REQUEST_URI'] = $targetUrl;
    }
    define('WP_USE_THEMES', true);
    require('./wp-blog-header.php');
} else {
    $response->send();
    $kernel->terminate($request, $response);
}

Once modified, replicate the changes to your app.php file and delete WordPress's index.php file. If you activate WordPress's url rewriting capabilities, you may need to comment modifications that WordPress automatically made to your .htaccess file.

Example of Symfony2 controller

/**
 * @Route("/test-page/")
 */
public function pageAction()
{
    return WordPressResponse::currentPage(array("var1" => "value1", "var2" => 2));
}

/**
 * @Route("/test-form/")
 */
public function formAction(Request $request)
{
    // Create form
    $builder = $this->createFormBuilder();
    $form = $builder
        ->add('var1', 'text', array("label" => "Variable 1", "required" => false, "constraints" => new NotBlank()))
        ->add('var2', 'text', array("label" => "Variable 2", "required" => false, "constraints" => new NotBlank()))
        ->getForm();
    $result = null;
    if ($request->getMethod() == 'POST') {
        $form->bind($request);
        $data = $form->getData();
        $result = $data["var1"]." ".$data["var2"];
    }
    return WordPressResponse::currentPage(array('form' => $form->createView(), "result" => $result));
}

This will create two routes, which will delegate their view to the respective WordPress page. For example, in the /test-page/ WordPress content, you can have:

This is my test page, with some really interesting content.
<ul>
<li>var1 = {{ var1 }}</li>
<li>var2 = {{ var2 }}</li>
</ul>

Which will display as:

This is my test page, with some really interesting content.
    * var1 = value1
    * var2 = 2

The /test-form/ page can have the following content:

Hi,

This is my test form.

<form method="post">
    {{ form_widget(form) }}
    <input type="submit" value="Ok" />
</form>
{% if result is not null %}
Result: {{ result }}
{% endif %}

This will display a basic form which will concatenate the two variables when submitted.

统计信息

  • 总下载量: 156
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-12-17