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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19524999/
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 08:35:34  来源:igfitidea点击:

How to redirect to a route in a called function laravel

phplaravellaravel-4

提问by Claire

I am tidying up my code and it makes sense for me to remove the login function outside of my formhandler function. However, when I call the return route line in my called function, it simply returns this route back to the parent controller, which does nothing with it. I want my redirect to be carried out within the called function.

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

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");
    }

}

It is not redirecting but coming back to loginFormHandler, which I know because it's not going to the dashboard page.

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

回答by Claire

It's not possible. When you say return Redirect::route('dashboard'), its returning that function call to the calling function, rather than carrying out the function. By leaving off return, it still comes back to the calling function anyway.

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

I have since reorganised my logic.

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

回答by searsaw

You have to return what is returned from the method call. The first function should look something like this:

您必须返回从方法调用返回的内容。第一个函数应该是这样的:

public static function loginFormHandler(){

    //some stuff is done

    return $this->doLogin();
}

回答by richardhell

But, you can redirect to URL:

但是,您可以重定向到 URL:

Redirect::to('/dashboard');

and that is a route:

这是一条路线:

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

Goodluck

祝你好运

回答by clod986

I like to keep my redirects anchored to the controllers, in order to have the possibility to change SEO strategy if needed. To achieve this is really simple:

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

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

Hope this helps

希望这可以帮助

回答by dimitar032

Hello i am building an Lumen API right now and i needed the same functionality to return response from other function.

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

I hope this could help.

我希望这会有所帮助。

//helpers.php
function responseWithJsonErrorsArray($text, $code)
    return response('error' => $text, $code);

//FooTrait.php
protected function fooBar(){
   responseWithJsonError('Foo Error', 404)->send();
   exit;
}

回答by Ilya Degtyarenko

Look in docs

查看文档

You can use:

您可以使用:

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