从Wordpress内部检查Laravel上经过身份验证的用户?

问题描述:

这很奇怪.

我有两个网站,一个由Laravel(5.3)驱动,一个由Wordpress驱动.

I have two websites, one powered by Laravel (5.3) and one powered by Wordpress.

Laravel存在于portal.example.com

Laravel exists at portal.example.com

Wordpress存在于example.com

Wordpress exists at example.com

我正在尝试从Wordpress网站检查当前在Laravel网站上登录的用户.

I'm trying to check which user is currently logged in on the Laravel site from the Wordpress site.

我已将Laravel设置为使用cookie驱动程序进行会话,并将cookie域设置为.example.com,以便现在可以从Wordpress网站上的任何位置查看此cookie.

I have Laravel set up to use the cookie driver for sessions, and have the cookie domain set to .example.com so I can now see this cookie from anywhere on the Wordpress site.

在wordpress头文件(或wp-load.php文件或functions.php文件)的顶部,我尝试了很多地方,但遇到相同的问题)我包括以下内容...

At the top of the wordpress header file (or the wp-load.php file, or functions.php file, I've tried a bunch of places but get the same problem) I have included the following...

require $_SERVER['DOCUMENT_ROOT'].'/../laravel/bootstrap/autoload.php';
$laravel = require $_SERVER['DOCUMENT_ROOT'].'/../laravel/bootstrap/app.php';

$laravel->make('Illuminate\Contracts\Http\Kernel')
        ->handle(Illuminate\Http\Request::capture());

if (Auth::check()) {
    var_dump(Auth::user()->id);
} else {
    var_dump(false);
}

现在,每当我登录Laravel网站时,我都可以访问example.com并在页面顶部找到我的Laravel用户ID当前已成功var_dump'd.

Now, whenever I login on the Laravel site, I can go to example.com and up at the top of the page my Laravel user's ID is currently being var_dump'd successfully.

但是,一旦我访问example.com/other-pages/,它就是var_dumping false.

However, once I visit example.com/other-pages/ it is var_dumping false.

我知道随着站点继续加载,它仍然可以找到我需要的两个文件,如果我将其更改为无意义的文件名,该站点将消失.我什至可以使用var_dump($ laravel)并获得大量的Laravel外观列表.

I know it's still finding the two files I'm requiring as the site continues to load, and if I change them to non-sense file names the site dies. I can even var_dump($laravel) and get a huge list of Laravel looking stuff.

有没有机会有人知道这里会发生什么?

Is there any chance anyone has some idea what could be going on here?

我注意到的第一件事是,在不起作用的页面上有所不同,如果我是var_dump($ laravel)...我得到...

The first thing I've noticed that is different on the pages where this doesn't work, is if I var_dump($laravel)... I get...

protected 'routeResolver' => null

接近尾声而不是...

Towards the end instead of...

protected 'routeResolver' => 
        object(Closure)[805]
          ...

这里有些进步.

如果我这样做...

$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
$response = $kernel->handle( $request = Illuminate\Http\Request::capture());

var_dump($response);

我可以看到其他页面上的响应是Laravel 404页面,因此它遇到了错误,并且从未返回过我的用户.

I could see that the response on these other pages was the Laravel 404 page, so it hit an error and never returned my user.

我可以通过将每个wordpress域添加到我的Laravel路由文件中来修复"它,但这只是,大​​声笑.

I can "fix" it by adding every wordpress domain to my Laravel routes file but that's just dirty, lol.

有什么办法解决这个问题吗?

Any idea for a way around this?

制作一个称为WordpressMiddlware的中间件,并在每个请求中添加它:

make a middleware called WordpressMiddlware and in make it in every request:

PHP

public function handle($request, Closure $next)
{
    return $request->has("from_wordpress") ? redirect("PATH_TO_AUTH") : $next($request);
}

在wordpress中:

In wordpress:

PHP

$kernel = $app->make('Illuminate\Contracts\Http\Kernel');
$response = $kernel->handle( $request = Illuminate\Http\Request::capture());
$response->merge(["from_wordpress" => true]);

现在,来自wordpress的每个请求都将被重定向到所需的路径,并且您不会得到404.

now every request from wordpress will be redirected to the desired path and you don't get 404.