从外部类调用时,Laravel 5重定向不起作用

时间:2017-05-17 00:58:11

标签: php laravel redirect laravel-5

我有一个控制器,用于使用以下方法将用户添加到数据库

public function store()
{

    $request = request()->all();


    $data = [
        'fname' => $request['first_name'],
        'lname' => $request['last_name'],
        'email' => $request['email'],
        'password' => bcrypt($request['password'])
    ];

    (new AddUserService($this))->execute($data);


}

public function successResponse($message)
{

    return redirect()->back()->with('success', 'Great! ' . $message);
}

public function failResponse($message)
{
    return redirect()->back()->with('error', 'Sorry! ' . $message)->withInput();
}

在AddUserService上,execute方法和构造函数如下

private $caller;

function __construct($caller)
{
    $this->caller = $caller;
}

public function execute(array $data)
{

    try{

        $user = User::create($data);

        $this->caller->successResponse('success');

    }catch(\Exception $e){
        $this->caller->failResponse('Failed');

    }

}

当我在控制器上的successResponse()或failResponse()方法上执行var_dump(' test')时,它成功指示流程到达那里但重定向不起作用,它仍然在同一页面上。

1 个答案:

答案 0 :(得分:2)

你错过了回报

return (new AddUserService($this))->execute($data);
return $this->caller->successResponse('success');
return $this->caller->failResponse('Failed');