将 Laravel 集成到 Wordpress

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52366578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:03:33  来源:igfitidea点击:

Integrate Laravel into Wordpress

wordpresslaravel

提问by Maxime Barber

The idea is to use WordPress as the front-end using Yootheme and Laravel as the back-end. I am able to do this to a certain extent using wl-bootstrap.

这个想法是使用 WordPress 作为前端,使用 Yootheme 和 Laravel 作为后端。我可以使用wl-bootstrap在一定程度上做到这一点。

However I am unable to get access to my laravel controllers in my wordpress views.

但是,我无法在我的 wordpress 视图中访问我的 Laravel 控制器。

Any feedback on how to do this would be much appreciated.

任何关于如何做到这一点的反馈将不胜感激。

I would also like to know if you believe this setup is a good idea or not?

我还想知道您是否认为这种设置是个好主意?

Edit: The client wants to be able to do updates via WordPress and use Yoothemefor the front end

编辑:客户希望能够通过 WordPress 进行更新并使用Yootheme作为前端

Edit: Thank you for all your answers. I ended up creating a Laravel API to query data in WordPress.

编辑:感谢您的所有回答。我最终创建了一个 Laravel API 来查询 WordPress 中的数据。

回答by ggdx

TBH I can't really see why you would need Laravel to do a Wordpress site from the question, so this answer is a punt at best.

TBH 我真的不明白为什么你需要 Laravel 从这个问题中做一个 Wordpress 网站,所以这个答案充其量只是一个平底船。

Laravel has many components from Symfony, there is actually a Symfony/Wordpress hybrid at https://github.com/ekino/EkinoWordpressBundlehowever my experience tells me that if you need something more than Wordpress can provide, Wordpress isn't the right choice at all (right tool for the job and all that).

Laravel 有很多来自 Symfony 的组件,实际上在https://github.com/ekino/EkinoWordpressBundle 上有一个 Symfony/Wordpress 混合体,但是我的经验告诉我,如果你需要的东西比 Wordpress 能提供的更多,Wordpress 不是正确的选择完全(适合工作和所有这些的正确工具)。



Integrate Wordpress into Laravel

将 Wordpress 集成到 Laravel 中

If you add require( 'path/to/wp-blog-header.php' );to the top of the Laravel public/index.php, you should have access to the Wordpress functionality in Laravel, then you could do something like...

如果您添加require( 'path/to/wp-blog-header.php' );到 Laravel 的顶部public/index.php,您应该可以访问 Laravel 中的 Wordpress 功能,然后您可以执行类似...

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{

    protected $posts;

    public function __construct()
    {
        $this->posts = get_posts($args);
    }



    public function index($cat_slug)
    {
        return view('dashboard', ['posts' => $this->posts]);
    }
}


Add a wordpress blog without integrating

添加wordpress博客,无需集成

If you want to do something like have a blog on a Laravel site, you could just add a directory public/blog/and put the Wordpress installation in there.

如果你想在 Laravel 站点上创建一个博客,你可以添加一个目录public/blog/并将 Wordpress 安装放在那里。



Sharing database

共享数据库

If you want them to share a database, make sure you create a model class containing the tables and columns.

如果您希望他们共享一个数据库,请确保您创建了一个包含表和列的模型类。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    protected $table = 'wp_posts';

    protected $fillable = ['post_title', 'post_content', 'etc'];

}

Then you can reference them in your controllers or other libraries.

然后您可以在您的控制器或其他库中引用它们。

回答by user644729

The smartest way to create Wordpress Laravel integration is with WordPressPete. Imagine powering the front end of your website in WordPress while managing content, payments, subscriptions, advanced tools, and custom dashboard in the powerful MVC framework Laravel 5. Picture being able to deploy this robust development environment in just a few clicks:

创建 Wordpress Laravel 集成的最聪明方法是使用 WordPressPete。想象一下,在强大的 MVC 框架 Laravel 5 中管理内容、支付、订阅、高级工具和自定义仪表板的同时,在 WordPress 中为您网站的前端提供支持。 图片只需点击几下即可部署这个强大的开发环境:

Check this tutorial:

检查本教程:

https://wordpresspete.com/2018/11/03/create-a-wordpress-laravel-integration-with-wordpresspete-part-one/

https://wordpresspete.com/2018/11/03/create-a-wordpress-laravel-integration-with-wordpresspete-part-one/

WordPressPete is a local and production server environment that can be installed in macOS and Linux with just a few clicks.

WordPressPete 是一个本地和生产服务器环境,只需点击几下即可安装在 macOS 和 Linux 中。

回答by Brian

Somewhat old post but came across this as I am also interested in using Laravel functionality in a Wordpress site. It absolutely makes sense and there is no reason why the other way around is only sensible approach.

有点旧的帖子,但遇到了这个,因为我也对在 Wordpress 站点中使用 Laravel 功能感兴趣。这绝对是有道理的,并且没有理由认为相反的方法只是明智的方法。

So yeah, Laravel components available in Wordpress backend is pretty awesome.

是的,Wordpress 后端中可用的 Laravel 组件非常棒。

The way I am currently doing it is installing Laravel components that I want via composer in top level directory. So I now have a vendors folder, where each component has a composer.json with autolad info. So in my functions.php, I just call

我目前的做法是在顶级目录中通过 composer 安装我想要的 Laravel 组件。所以我现在有一个 vendor 文件夹,其中每个组件都有一个带有 autolad 信息的 composer.json。所以在我的functions.php中,我只是调用

require_once 'vendor/autoload.php';

I just did this for the Pusher php library that is awesome for real time notification functionality and often used with Laravel. So I am using Pusher on my WordPress site using same techniques as used on my Laravel site. Just that I have to write the callbacks in line with WordPress, like authorizing a private Pusher channel.

我只是为 Pusher php 库做了这个,它非常适合实时通知功能,并且经常与 Laravel 一起使用。所以我在我的 WordPress 网站上使用 Pusher,使用的技术与我在 Laravel 网站上使用的技术相同。只是我必须根据 WordPress 编写回调,例如授权私人 Pusher 频道。

So in your case, not very different conceptually. Just require autoload, then figure out what classes you need.

所以在你的情况下,概念上没有太大不同。只需要自动加载,然后找出您需要的类。

You could create a folder like App/Http/Controllers/Apiand set up a restAPI approach to pull data as needed from WordPress, where in WordPress you define a Custom RestAPI Endpoint to your Laravel endpoint.

您可以创建一个像这样的文件夹App/Http/Controllers/Api并设置一个 restAPI 方法来根据需要从 WordPress 中提取数据,在 WordPress 中,您可以为 Laravel 端点定义一个自定义 RestAPI 端点。

Jut read that you ended up writing a Laravel API, so maybe this is what you ended up doing? Curious, are you using Laravel Routes? That is something I would really be interested in figuring out - that is, how to use Laravel routes in way that don't interfere with Wordpress, so that I could have site.net/laravelroute and it would bypass all wordpress checks, routing.

只是读到你最终编写了一个 Laravel API,所以也许这就是你最终要做的?好奇,你在使用 Laravel 路由吗?这是我真正想弄清楚的事情——也就是说,如何以不干扰 Wordpress 的方式使用 Laravel 路由,这样我就可以拥有 site.net/laravelroute 并且它会绕过所有 wordpress 检查、路由。