Laravel 5.6 - 在控制器中按名称获取路由

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

Laravel 5.6 - Get Route By Name In Controller

laravelauthenticationroutes

提问by heady12

Within my login controller there is a hardcoded URL string which sets where to redirect to once the user has logged in. I am trying to make this dynamic by getting the route by name:

在我的登录控制器中,有一个硬编码的 URL 字符串,用于设置用户登录后重定向到的位置。我试图通过按名称获取路由来使其动态化:

protected $redirectTo = '/home';

Updated To:

更新为:

protected $redirectTo = route('home');

However the above give the following error:

但是上面给出了以下错误:

FatalErrorException (E_UNKNOWN) Constant expression contains invalid operations

FatalErrorException (E_UNKNOWN) 常量表达式包含无效操作

Is it possible to get the URL to the route by its name?

是否可以通过名称获取路由的 URL?

回答by DigitalDrifter

You can use

您可以使用

request()->route()->getName()

To get the url you would use

要获取您将使用的网址

request()->url()

And the path

和路径

request()->path()

Current route method

当前路线方法

request->route()->getActionMethod()

In the case of redirectToyou can override the function:

redirectTo你可以覆盖函数的情况下:

protected function redirectTo() {
    return route('foo.bar');
}

回答by eamirgh

You have to define a route named homelike this: Route::get('/home', 'HomeController@index')->name('home'); or Route::get('/home', ['as' => 'home', 'uses' => 'HomeController@index']);

你必须定义一个home像这样命名的路由: Route::get('/home', 'HomeController@index')->name('home'); Route::get('/home', ['as' => 'home', 'uses' => 'HomeController@index']);

回答by Sourav

Another way to get route url

另一种获取路由url的方法

$route_url = \Request::route()->getURLByName("name of the route");

回答by Jonathon

Your problem is that you're not allowed to use a function call when declaring a property in your class. You should use your controller's constructor to set it:

您的问题是在类中声明属性时不允许使用函数调用。您应该使用控制器的构造函数来设置它:

class LoginController extends Controller
{
    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
        $this->redirectTo = route('home');
    }
}

Alternatively, you can define a redirectTomethod which returns the location that you want the user to be redirected to after a successful login. You can then remove the $redirectToproperty altogether:

或者,您可以定义一个redirectTo方法,该方法返回您希望用户在成功登录后重定向到的位置。然后,您可以$redirectTo完全删除该属性:

class LoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function redirectTo()
    {
        return route('home');
    }
}

回答by Adnan Mumtaz

protected $redirectTo = route('home');

You cannot assign a function to property. That's why you are getting this error.

您不能将功能分配给属性。这就是您收到此错误的原因。

you can do this in your constructor like

你可以在你的构造函数中这样做

public function __construct(){

  $this->redirectTo = route('home')

}

回答by Mohammed Aktaa

This is the way which i use and it will work with you.just put this function in your LoginController to override authenticated function.

这是我使用的方式,它将与您一起使用。只需将此功能放在您的 LoginController 中即可覆盖经过身份验证的功能。

 protected function authenticated(Request $request, $user)
    {
            return redirect()->route('your_route_name');
    }