在Laravel外部使用RedirecResponse


Using RedirecResponse outside Laravel

我正在连接我的外部应用程序做一些动作到Laravel 5,得到响应并继续。其中一个操作是使用Laravel代码登录:

<标题>外Laravel h1> aravel自定义连接类(Laravel外部)
<?php
namespace Laravel;
use App;
use Illuminate;
class Laravel
{
    private static $app;
    private static function load()
    {
        require_once self::getPath('bootstrap/autoload.php');
        self::$app = require_once self::getPath('bootstrap/app.php');
        self::$app->make('Illuminate''Contracts''Http''Kernel')->handle(Illuminate'Http'Request::capture());
    }
    public static function login()
    {
        self::load();
        $response = App::make('App''Http''Controllers''User')->login($_POST);
        if ($response instanceof RedirectResponse) {
            # Here I want to stop my app, save cookies/headers from Laravel Login
            # and redirect to $response target url location
            # If I do it with a simple Location redirect, I lost cookies and headers
            # and User will be not authenticated on next request
            die($response);
        }
    }
    public static function loginView()
    {
        self::load();
        return view('pages.users.login');
    }
}
<标题> Laravel App ' Http '控制器' User@login h1> 有所有的代码工作(视图模板,用户验证,登录),但我需要在结束重定向与头和cookie的登录响应可用于下一个请求。

有简单干净的方法吗?(有更多的代码,但这里简化了)

有一个简单而干净的方法来做到这一点,我来到这里希望做同样的事情,这个页面向我展示了简单地在响应上添加对->send()的调用将发送响应。在您的示例中,login方法可能如下所示:

public function login($data)
{
    $success = Auth::attempt([
        'user' => $data['user'],
        'password' => $data['password'],
    ], $data['remember']);
    if ($success) {
        $response = redirect()->to(url('/'));
        $response->send();
        return $response;
    }
}

如果有一个单一的点在你的集成,你调用你的控制器方法,它会更有意义的地方,这样你就不会从你的控制器内部调用->send()(如Laravel没有)。