如何将 Symfony2 操作嵌入到 WordPress 中? [英] How to embed Symfony2 actions into WordPress?

查看:23
本文介绍了如何将 Symfony2 操作嵌入到 WordPress 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究它并尝试它,但我有点难住了.

I've been researching it a lot and trying it out but I'm kind of stumped.

我想在 WordPress 中设置一个站点,这对与我一起工作的另一个人很有帮助.该网站将宣传我们的产品并提供信息.然后用户可以通过一系列表格进行注册.我想在 Symfony2 中编写这个自定义部分(表单等),因为它不需要绑定到 WordPress,并且在用户注册后它会有可重用的 Doctrine2 实体来显示数据(无论如何显示都发生在 WordPress 之外).

I want to setup a site in WordPress, which is helpful for another guy working with me. The site will advertise our product and provide information. Then users can sign up through a series of forms. I want to write this custom part (forms, etc.) in Symfony2 because it has no need to be tied to WordPress, and it would have reusable Doctrine2 entities to display the data after a user has signed up (displaying happens outside of WordPress anyway).

但是,在设计方面,我们希望整个过程不间断并具有相同的外观和感觉.所以表单实际上应该在 WordPress 页面中呈现.我们使用的是自定义的、非免费的主题,我不想只是将一堆 WordPress CSS 和标题复制粘贴到 Symfony 视图中.

But, design-wise, we would like the whole process to be uninterrupted and have the same look-and-feel. So the forms should actually be rendered in WordPress pages. We are using a custom, non-free theme and I'd hate to just copy-paste a bunch of WordPress CSS and headers into Symfony views.

理想情况下,我只想在 WordPress 中定义可以呈现 Symfony2 操作的页面.因此,操作本身可能会显示和处理表单(应该独立于 WordPress 工作,如 http://example.com/app.php/注册),但它们通常应该显示在 WordPress 网站中(例如在像 http://example.com/index.php?page_id=2 或固定链接).

Ideally, I want to just define pages in WordPress which can render Symfony2 actions. So the actions themselves might display and process forms (which should work independently of WordPress say at http://example.com/app.php/signup), but they should normally be displayed in the WordPress site (for example within a page like http://example.com/index.php?page_id=2 or a permalink).

我一直在研究 LowPress 作为一种集成方式 (http://www.lowpress.com/),但通过完全删除 WordPress 主题并用 Twig 主题替换它们,它的效果超出了我的预期.我试图从中借鉴一些想法,所以现在我在 Symfony 项目的 web 文件夹中有 WordPress,而在我的 wp-config.php 中有这个:

I have been researching LowPress as a way to integrate (http://www.lowpress.com/), but it does way more than I want by removing the WordPress themes altogether and replacing them with Twig themes. I tried to borrow a few ideas from it, so now I have WordPress in the web folder of the Symfony project and this in my wp-config.php:

// ...code omitted
define('WP_DEBUG', true);

define('SYMFONY_DIR', __DIR__.'/../app/');

require_once SYMFONY_DIR.'/bootstrap.php.cache';
require_once SYMFONY_DIR.'/AppKernel.php';
//require_once SYMFONY_DIR.'/AppCache.php';

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Request;

$kernel = new AppKernel((WP_DEBUG) ? 'dev' : 'prod', WP_DEBUG);
$kernel->loadClassCache();
$kernel->boot();
$GLOBALS['sf2_kernel'] = $kernel;
// save request before WordPress messes with it
$GLOBALS['sf2_request'] = Request::createFromGlobals();

$doctrine = $kernel->getContainer()->get('doctrine');
$conn = $doctrine->getConnection($doctrine->getDefaultConnectionName());

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME',       $conn->getDatabase());

/** MySQL database username */
define('DB_USER',       $conn->getUsername());

/** MySQL database password */
define('DB_PASSWORD',   $conn->getPassword());

/** MySQL hostname */
define('DB_HOST',       $conn->getHost());

/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');

/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

// ...code omitted

所以我现在所拥有的只是通过 Symfony 中的 parameters.ini 共享的数据库配置.接下来,我一直在尝试制作一个使用短代码的简单插件,以便我可以在 WordPress 页面中呈现 Symfony2 操作.这是我目前的想法(它依赖于上述引导并且不完整):

So all I've got now is a shared DB config via parameters.ini in Symfony. Next I've been trying to make a simple plugin which uses a shortcode so I can render a Symfony2 action in a WordPress page. Here is what I have so far as an idea (it is dependent upon the above bootstrapping and it's incomplete):

use Symfony\Component\HttpFoundation\Request;

class Symfony2Page
{
    public function render($atts)
    {
        extract(shortcode_atts(array(
            'controller' => 'CroltsMainBundle:Default:index',
        ), $atts));
        $kernel = $GLOBALS['sf2_kernel'];
        $request = $GLOBALS['sf2_request'];
        $request->headers->set('X-Php-Ob-Level', ob_get_level());
        $attributes = array();
        // @TODO fix this

        $kernel->getContainer()->enterScope('request');
        $kernel->getContainer()->set('request', $request, 'request');

        try {
            $response = $kernel->getContainer()->get('http_kernel')->render($controller, $attributes)->getContent();
        } catch (\Exception $e) {
            $kernel->getContainer()->leaveScope('request');

            throw $e;
        }

        $kernel->getContainer()->leaveScope('request');

        return $response;
    }
}

add_shortcode('sf_action', array('Symfony2Page', 'render'));

我的第一个问题是当给定的 $request 没有我需要的信息时,我不知道如何实际渲染一些可能有参数的 Symfony2 路由.另一个问题是,如果我想要提交表单,它可能无法工作,因为它会将用户重定向到 WordPress 之外,而实际上我可能想要一系列都存在于 WordPress 页面中的表单.同时,我希望表单独立于 WordPress,以便它们独立工作.

My first problem is I don't know how to actually render some Symfony2 route, which may have parameters, when the given $request won't have the information I need. The other problem is, if I want forms to submit, it probably wouldn't work because it would redirect the user outside of WordPress, when really I may want a series of forms which all exist in the WordPress page. At the same time, I want the forms to be independent of WordPress so they work on their own.

所以我想知道这是否只是一个行不通的坏主意/hacky 想法,或者是否有某种方法可以让它发挥作用.

So I want to know if this is just a bad/hacky idea which won't work, or if there is some way to get it working.

我还考虑过使用 AJAX 在 WordPress 中加载 Symfony2 代码(假设我的所有用户都启用了 Javascript).作为后备,该页面可以只进入 Symfony2 的应用程序,而不是在 WordPress 页面中.那会更好/更容易吗?我能看到的唯一缺点是我必须保持 ajax 代码与我的 Symfony2 代码同步.

I was also thinking of just using AJAX to load the Symfony2 code in WordPress (assuming that all of my users have Javascript enabled). As a fallback, the page could just go to a Symfony2-only app rather than being within a WordPress page. Would that be better/easier? The only downside I can see is that I have to keep the ajax code in sync with my Symfony2 code.

推荐答案

Symfony 2 允许使用框架的一部分而不使用整个框架.

Symfony 2 permits the usage of parts of the framework without using the whole.

表单是一个可拆卸的组件,您可以在这里找到:

The form is a component that IS detachable, as you can find here:

http://symfony.com/components

遗憾的是,目前还没有太多文档,因此您必须自己去发现它...

sadly ,there is not much documentation yet, therefore you'd have to discover it by yourself...

路由等也是如此.如果您希望能够使用 symfony 路由来识别路径/要做的事情,则必须包含 HttpFoundation 组件,当然还有路由.

edit: the same goes along with the routing, etc. if you want to be able to use the symfony routing to recognize paths/things to do , you'd have to include the HttpFoundation component, and obviously the routing.

这篇关于如何将 Symfony2 操作嵌入到 WordPress 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆