来自控制器的 Laravel 调用路由

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

Laravel call route from controller

phplaravellaravel-5routeslaravel-5.2

提问by Umair Zahid

I am calling getting_started route after successfully login :

成功登录后我正在调用getting_started路由:

protected $redirectTo = '/getting_started';

Here is my getting_started route code :

这是我的 Getting_started 路线代码:

Route::get('/getting_started','UserController@getting_started');

And controller code :

和控制器代码:

public function getting_started()
{
    $id= Auth::id();
    $user = DB::table('user_profiles')->where('user_id', '=', $id)->first();

    if($user->dashboard_access == 0)
    {
        DB::table('user_profiles')
            ->where('user_id', $id)
            ->update(['dashboard_access' => 1]);
        return view('user.getting_started');
    }

    return view('user.dashboard');
}

It works perfectly and show in url :

它完美运行并显示在 url 中:

http://localhost:8080/getting_started

http://localhost:8080/getting_started

Now I actually want that if user.dashboardview is call it show in url like :

现在我实际上希望如果user.dashboard视图调用它显示在 url 中,例如:

http://localhost:8080/dashboard`

http://localhost:8080/dashboard`

And on getting_startedview show :

并在getting_started视图显示:

http://localhost:8080/getting_started

http://localhost:8080/getting_started

It is possible to call dashboard route instead of :

可以调用仪表板路由而不是:

  return view('user.dashboard');

My dashobard route is :

我的dashobard路线是:

Route::get('/dashboard',['middleware' => 'auth', function () {
    return view('user.dashboard');
}]);

回答by Yasir Ijaz

What I understand it is that you are looking for is this function

我的理解是你正在寻找的是这个功能

return redirect()->route('dashboard');

return redirect()->route('dashboard');

It's my understanding of your question which can be wrong. Maybe you are asking something else.

我对你的问题的理解可能是错误的。也许你在问别的。

回答by Zakaria Acharki

That called Redirectionand especially you want to Returning A Redirect To A Named Route, you route called user.dashboardso you could redirect to it using redirect()->route(route_name):

这称为重定向,尤其是您想要将重定向返回到命名路由,您路由被调用,user.dashboard以便您可以使用redirect()->route(route_name)以下命令重定向到它:

return redirect()->route('user.dashboard');

Hope this helps.

希望这可以帮助。