如何重定向到被调用函数中的路由 laravel


How to redirect to a route in a called function laravel

我正在整理我的代码,删除表单处理程序函数之外的登录函数对我来说是有意义的。 但是,当我在调用的函数中调用返回路由行时,它只是将此路由返回给父控制器,父控制器对此没有任何作用。 我希望我的重定向在被调用的函数中执行。

public static function loginFormHandler(){
 //some stuff is done
$this->doLogin();
}
public static function doLogin(){
if ('Auth::attempt($this->credentials, $this->remember)) {
         return 'Redirect::route("dashboard");
    }
}

它不是重定向而是返回到loginFormHandler,我知道这是因为它不会进入仪表板页面。

是不可能的。 当你说return Redirect::route('dashboard')时,它会将该函数调用返回给调用函数,而不是执行该函数。 通过离开返回,它仍然会回到调用函数。

从那以后,我重新组织了我的逻辑。

您必须返回从方法调用返回的内容。 第一个函数应如下所示:

public static function loginFormHandler(){
    //some stuff is done
    return $this->doLogin();
}
但是

,您可以重定向到URL:

Redirect::to('/dashboard');

这是一条路线:

Route::get('/dashboard', 'DashboardController@index');

古德勒克

我喜欢

将重定向锚定到控制器上,以便在需要时可以更改SEO策略。要实现这一点非常简单:

return Redirect::action('SomeController@someFunction');

希望这有帮助

您好,我现在正在构建一个 Lumen API,我需要相同的功能来返回来自其他函数的响应。

我希望这能有所帮助。

//helpers.php
function responseWithJsonErrorsArray($text, $code)
    return response('error' => $text, $code);
//FooTrait.php
protected function fooBar(){
   responseWithJsonError('Foo Error', 404)->send();
   exit;
}

你可以简单地返回函数中返回的内容,比如:

public static function loginFormHandler(){
    //some stuff is done
    return $this->doLogin();
}
 public static function doLogin(){
 if ('Auth::attempt($this->credentials, $this->remember)) {
     return 'Redirect::route("dashboard");
 }

}

查看文档

您可以使用:

return redirect('home/dashboard');
return redirect()->route('profile', ['id' => 1]);
return redirect()->action('HomeController@index');
return redirect('dashboard')->with('status', 'Profile updated!');
...