我可以将Zend-Framework支持的Web应用程序集成到wordpress站点吗?

时间:2022-01-01 19:34:56

I have a project in which I want to be able to call wp_list_pages() on a page that also uses the Zend Framework to power some complex interfaces manages custom data outside of wordpress.

我有一个项目,我希望能够在一个页面上调用wp_list_pages(),该页面也使用Zend Framework为一些复杂的接口提供支持,以管理wordpress之外的自定义数据。

This page should also redirect the user to the wordpress login screen if they're not already logged in with the appropriate level of authorization.

如果用户尚未使用适当的授权级别登录,则此页面还应将用户重定向到wordpress登录屏幕。

How would this work at a high level, i.e. do I need to edit the wordpress bootstrap file to conditionally implement the custom interface based on a specific URL or something, but still include certain files to be able to call wp_list_pages() on that custom interface?

这将如何在高级别工作,即我是否需要编辑wordpress引导程序文件以基于特定的URL或其他条件有条件地实现自定义接口,但仍然包括某些文件以便能够在该自定义接口上调用wp_list_pages() ?

6 个解决方案

#1


I've developed a couple of WordPress plugins, and I've found it's really easy to extend. Haven't worked with Zend though.

我开发了几个WordPress插件,我发现它很容易扩展。但是没有和Zend合作过。

You should check the WordPress plugin api. Mostly the part about actions, filters and hooks: http://codex.wordpress.org/Plugin_API

你应该检查WordPress插件api。主要是关于动作,过滤器和钩子的部分:http://codex.wordpress.org/Plugin_API

You can even override some functions (not sure if wp_list_pages() is overridable).

您甚至可以覆盖某些函数(不确定wp_list_pages()是否可以覆盖)。

It's pretty well documented, and there's a large developer community behind it on IRC, forums, etc.

它有很好的文档记录,在IRC,论坛等方面有一个庞大的开发者社区。

#2


Thanks Fernando.

I just read this thread which suggests that you can use Zend in any script by just including:

我刚刚阅读了这个帖子,它建议您可以在任何脚本中使用Zend,只需包括:

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

So given that all I need to use Zend for is on one page, can I just include that code in a custom template file that I assign to the appropriate page in the navigation? If I used javascript to submit the form via XHR, the requested URL would take the form '/controller/action' - but Zend wouldn't know the controller directory.

因此,鉴于我需要使用Zend for在一个页面上,我可以将该代码包含在我分配给导航中相应页面的自定义模板文件中吗?如果我使用javascript通过XHR提交表单,请求的URL将采用'/ controller / action'形式 - 但Zend不会知道控制器目录。

Could I put Zend code into the wordpress bootstrap, i.e. the above code plus the frontController configuration, and then use Zend wherever however?

我可以将Zend代码放入wordpress引导程序,即上面的代码加上frontController配置,然后在任何地方使用Zend吗?

#3


So I've created a page in Wordpress and a custom template for that page, in which I've placed the following Zend Framework code:

所以我在Wordpress中创建了一个页面,并为该页面创建了一个自定义模板,我在其中放置了以下Zend Framework代码:

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'      => 'localhost',
    'username'  => 'username',
    'password'  => 'password',
    'dbname'    => 'dbname'
));
Zend_Db_Table::setDefaultAdapter($db);

class Users extends Zend_Db_Table_Abstract {
    protected $_name = 'wp_users';
}

$users = new Users();
$users = $users->fetchAll()->toArray();

print_r($users[0]['user_login']);

This all works fine, so it's clearly possible to use Zend in conjuction with Wordpress at least to some extent.

这一切都很好,所以显然可以将Zend与Wordpress结合使用至少在某种程度上。

It's becoming apparant that the problem is about who controls the URL rewriting, or the routing, or the bootstrapping (not sure of the correct terminology). If I were to put the end of the above code, starting $users = new Users();, into a controller as follows:

问题在于谁控制URL重写,路由或引导(不确定正确的术语),这一点变得很明显。如果我要把上面代码的结尾,启动$ users = new Users();,进入控制器,如下所示:

class UsersController extends Zend_Controller_Action {
    function getUserAction() {
        $this->_helper->viewRenderer->setNoRender();

        $users = new Users();
        $users = $users->fetchAll()->toArray();

        echo $users[0]['user_login'];
    }
}

How would I then call that function? My intention would be to call it from javascript via an XHR request in response to an event on the page, but requesting the URL 'index.php/Users/getUser/' returns 'No input file selected'. Trying to access the URL http://www.domain.com/Users/getUser/ produces a Wordpress 404 page.

那我怎么称呼那个功能呢?我的目的是通过XHR请求从javascript调用它来响应页面上的事件,但是请求URL“index.php / Users / getUser /”返回“未选择输入文件”。尝试访问URL http://www.domain.com/Users/getUser/会生成Wordpress 404页面。

Is there a way around this? It doesn't just apply to wordpress, of course - I expect it applies to any existing application that rewrites/routes requests via a bootstrap.

有没有解决的办法?当然,它不仅适用于wordpress - 我希望它适用于通过引导程序重写/路由请求的任何现有应用程序。

#4


I guess you could do that, just import the framework into the one page you need it for. I don't know how Zend works, but check the paths as to where to put your directories so that Zend finds them.As I said I guess you could do that, just experiment and tell us how it went!

我想你可以这样做,只需将框架导入到你需要的一个页面中。我不知道Zend是如何工作的,但检查路径到哪里放置你的目录,以便Zend找到它们。正如我所说,我想你可以做到这一点,只是试验并告诉我们它是如何进行的!

Beware of name conflicts for functions and/or variables, this shouldn't be much of a problem coming from such popular products as WordPress and Zend though... (which should be theoretically well coded)

谨防功能和/或变量的名称冲突,这不应该是来自像WordPress和Zend这样的流行产品的问题......(理论上它应该是编码良好的)

#5


I guess you could do that, just import the framework into the one page you need it for. I don't know how Zend works, but check the paths as to where to put your directories so that Zend finds them.As I said I guess you could do that, just experiment and tell us how it went!

我想你可以这样做,只需将框架导入到你需要的一个页面中。我不知道Zend是如何工作的,但检查路径到哪里放置你的目录,以便Zend找到它们。正如我所说,我想你可以做到这一点,只是试验并告诉我们它是如何进行的!

Beware of name conflicts for functions and/or variables, this shouldn't be much of a problem coming from such popular products as WordPress and Zend though... (which should be theoretically well coded)

谨防功能和/或变量的名称冲突,这不应该是来自像WordPress和Zend这样的流行产品的问题......(理论上它应该是编码良好的)

#6


I've built a plugin for wordpress that has a similar goal to yours, more modeled on CodeIgniter though. Not knowing Zend terribly well, I think this should help:

我已经为wordpress构建了一个插件,其目标与你的类似,但更多的是以CodeIgniter为蓝本。不知道Zend非常好,我认为这应该有所帮助:

Make a file named routes.php in your plugins directory with the following code:

使用以下代码在plugins目录中创建一个名为routes.php的文件:

add_action( 'init', 'add_custom_urls' );

function add_custom_urls(){
    global $wp, $wp_rewrite;

    $wp_rewrite->add_rule( '(.*)$', 'index.php?&cPath=$matches[1]', 'top' );
    $wp->add_query_var( 'cPath' );
}

Be sure to activate both plugins in your admin. These two files will allow you to catch the url before Wordpress tries to figure out what to do with it. You can use regular expressions to have finer control over which pages to catch. You may have to delete the record in your _options db table where option_name = 'rewrite_rules' before this works.

务必激活管理员中的两个插件。这两个文件将允许您在Wordpress尝试弄清楚如何处理它之前捕获URL。您可以使用正则表达式来更好地控制要捕获的页面。在此工作之前,您可能必须删除_options db表中的option_name ='rewrite_rules'的记录。

Next, make another plugin with the following code:

接下来,使用以下代码创建另一个插件:

add_action( 'template_redirect', 'bootstrap' );

function bootstrap(){
    global $cPath;
    echo( "cPath : $cPath" );
    if( $cPath ){
        dosomethingwith( $cPath ); 
    }
}

Put all your code in the dosomethingwith() function. You'll need to figure out if the url requested can me mapped to a zend controller, etc. http://www.domain.com/Users/getUser/ would give you $cPath = Users/getUser/ If successful, you'll also probably want to die(), so once it is completed Wordpress won't try and take over again.

将所有代码放在dosomethingwith()函数中。您需要弄清楚所请求的网址是否可以映射到zend控制器等.http://www.domain.com/Users/getUser/会给你$ cPath = Users / getUser /如果成功,你' ll也可能想死(),所以一旦完成,Wordpress就不会再尝试接管了。

#1


I've developed a couple of WordPress plugins, and I've found it's really easy to extend. Haven't worked with Zend though.

我开发了几个WordPress插件,我发现它很容易扩展。但是没有和Zend合作过。

You should check the WordPress plugin api. Mostly the part about actions, filters and hooks: http://codex.wordpress.org/Plugin_API

你应该检查WordPress插件api。主要是关于动作,过滤器和钩子的部分:http://codex.wordpress.org/Plugin_API

You can even override some functions (not sure if wp_list_pages() is overridable).

您甚至可以覆盖某些函数(不确定wp_list_pages()是否可以覆盖)。

It's pretty well documented, and there's a large developer community behind it on IRC, forums, etc.

它有很好的文档记录,在IRC,论坛等方面有一个庞大的开发者社区。

#2


Thanks Fernando.

I just read this thread which suggests that you can use Zend in any script by just including:

我刚刚阅读了这个帖子,它建议您可以在任何脚本中使用Zend,只需包括:

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

So given that all I need to use Zend for is on one page, can I just include that code in a custom template file that I assign to the appropriate page in the navigation? If I used javascript to submit the form via XHR, the requested URL would take the form '/controller/action' - but Zend wouldn't know the controller directory.

因此,鉴于我需要使用Zend for在一个页面上,我可以将该代码包含在我分配给导航中相应页面的自定义模板文件中吗?如果我使用javascript通过XHR提交表单,请求的URL将采用'/ controller / action'形式 - 但Zend不会知道控制器目录。

Could I put Zend code into the wordpress bootstrap, i.e. the above code plus the frontController configuration, and then use Zend wherever however?

我可以将Zend代码放入wordpress引导程序,即上面的代码加上frontController配置,然后在任何地方使用Zend吗?

#3


So I've created a page in Wordpress and a custom template for that page, in which I've placed the following Zend Framework code:

所以我在Wordpress中创建了一个页面,并为该页面创建了一个自定义模板,我在其中放置了以下Zend Framework代码:

require_once('Zend/Loader.php');
Zend_Loader::registerAutoload();

$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'      => 'localhost',
    'username'  => 'username',
    'password'  => 'password',
    'dbname'    => 'dbname'
));
Zend_Db_Table::setDefaultAdapter($db);

class Users extends Zend_Db_Table_Abstract {
    protected $_name = 'wp_users';
}

$users = new Users();
$users = $users->fetchAll()->toArray();

print_r($users[0]['user_login']);

This all works fine, so it's clearly possible to use Zend in conjuction with Wordpress at least to some extent.

这一切都很好,所以显然可以将Zend与Wordpress结合使用至少在某种程度上。

It's becoming apparant that the problem is about who controls the URL rewriting, or the routing, or the bootstrapping (not sure of the correct terminology). If I were to put the end of the above code, starting $users = new Users();, into a controller as follows:

问题在于谁控制URL重写,路由或引导(不确定正确的术语),这一点变得很明显。如果我要把上面代码的结尾,启动$ users = new Users();,进入控制器,如下所示:

class UsersController extends Zend_Controller_Action {
    function getUserAction() {
        $this->_helper->viewRenderer->setNoRender();

        $users = new Users();
        $users = $users->fetchAll()->toArray();

        echo $users[0]['user_login'];
    }
}

How would I then call that function? My intention would be to call it from javascript via an XHR request in response to an event on the page, but requesting the URL 'index.php/Users/getUser/' returns 'No input file selected'. Trying to access the URL http://www.domain.com/Users/getUser/ produces a Wordpress 404 page.

那我怎么称呼那个功能呢?我的目的是通过XHR请求从javascript调用它来响应页面上的事件,但是请求URL“index.php / Users / getUser /”返回“未选择输入文件”。尝试访问URL http://www.domain.com/Users/getUser/会生成Wordpress 404页面。

Is there a way around this? It doesn't just apply to wordpress, of course - I expect it applies to any existing application that rewrites/routes requests via a bootstrap.

有没有解决的办法?当然,它不仅适用于wordpress - 我希望它适用于通过引导程序重写/路由请求的任何现有应用程序。

#4


I guess you could do that, just import the framework into the one page you need it for. I don't know how Zend works, but check the paths as to where to put your directories so that Zend finds them.As I said I guess you could do that, just experiment and tell us how it went!

我想你可以这样做,只需将框架导入到你需要的一个页面中。我不知道Zend是如何工作的,但检查路径到哪里放置你的目录,以便Zend找到它们。正如我所说,我想你可以做到这一点,只是试验并告诉我们它是如何进行的!

Beware of name conflicts for functions and/or variables, this shouldn't be much of a problem coming from such popular products as WordPress and Zend though... (which should be theoretically well coded)

谨防功能和/或变量的名称冲突,这不应该是来自像WordPress和Zend这样的流行产品的问题......(理论上它应该是编码良好的)

#5


I guess you could do that, just import the framework into the one page you need it for. I don't know how Zend works, but check the paths as to where to put your directories so that Zend finds them.As I said I guess you could do that, just experiment and tell us how it went!

我想你可以这样做,只需将框架导入到你需要的一个页面中。我不知道Zend是如何工作的,但检查路径到哪里放置你的目录,以便Zend找到它们。正如我所说,我想你可以做到这一点,只是试验并告诉我们它是如何进行的!

Beware of name conflicts for functions and/or variables, this shouldn't be much of a problem coming from such popular products as WordPress and Zend though... (which should be theoretically well coded)

谨防功能和/或变量的名称冲突,这不应该是来自像WordPress和Zend这样的流行产品的问题......(理论上它应该是编码良好的)

#6


I've built a plugin for wordpress that has a similar goal to yours, more modeled on CodeIgniter though. Not knowing Zend terribly well, I think this should help:

我已经为wordpress构建了一个插件,其目标与你的类似,但更多的是以CodeIgniter为蓝本。不知道Zend非常好,我认为这应该有所帮助:

Make a file named routes.php in your plugins directory with the following code:

使用以下代码在plugins目录中创建一个名为routes.php的文件:

add_action( 'init', 'add_custom_urls' );

function add_custom_urls(){
    global $wp, $wp_rewrite;

    $wp_rewrite->add_rule( '(.*)$', 'index.php?&cPath=$matches[1]', 'top' );
    $wp->add_query_var( 'cPath' );
}

Be sure to activate both plugins in your admin. These two files will allow you to catch the url before Wordpress tries to figure out what to do with it. You can use regular expressions to have finer control over which pages to catch. You may have to delete the record in your _options db table where option_name = 'rewrite_rules' before this works.

务必激活管理员中的两个插件。这两个文件将允许您在Wordpress尝试弄清楚如何处理它之前捕获URL。您可以使用正则表达式来更好地控制要捕获的页面。在此工作之前,您可能必须删除_options db表中的option_name ='rewrite_rules'的记录。

Next, make another plugin with the following code:

接下来,使用以下代码创建另一个插件:

add_action( 'template_redirect', 'bootstrap' );

function bootstrap(){
    global $cPath;
    echo( "cPath : $cPath" );
    if( $cPath ){
        dosomethingwith( $cPath ); 
    }
}

Put all your code in the dosomethingwith() function. You'll need to figure out if the url requested can me mapped to a zend controller, etc. http://www.domain.com/Users/getUser/ would give you $cPath = Users/getUser/ If successful, you'll also probably want to die(), so once it is completed Wordpress won't try and take over again.

将所有代码放在dosomethingwith()函数中。您需要弄清楚所请求的网址是否可以映射到zend控制器等.http://www.domain.com/Users/getUser/会给你$ cPath = Users / getUser /如果成功,你' ll也可能想死(),所以一旦完成,Wordpress就不会再尝试接管了。