简体   繁体   English

如何检查当前请求或路由是否是 Laravel 中 Auth::routes() 的一部分

[英]How to check if current request or route is part of Auth::routes() in Laravel

I'm trying to hide the sidebar in the default app.blade.php file without making a new layout file.我试图隐藏默认app.blade.php文件中的侧边栏而不制作新的布局文件。

I am trying to add an if statement to hide the sidebar if the request or route is part of the ones created by Auth::routes()如果请求或路由是Auth::routes()创建的请求或路由的一部分,我正在尝试添加一个 if 语句来隐藏侧边栏

I know I can list a bunch of Request::is('login') and Request::is('register') etc...我知道我可以列出一堆Request::is('login')Request::is('register')等...

I was just wondering if there's a simple catch all to check if the request is any of the auth-related pages.我只是想知道是否有一个简单的方法来检查请求是否是任何与身份验证相关的页面。

Thanks!谢谢!

One way is to get sneaky and use the actions array for a Route.一种方法是偷偷摸摸地使用 Route 的actions数组。 When defining a group these attributes get merged in the with Route's attributes.定义组时,这些属性会合并到与 Route 的属性中。

Setup a group with a authroutes (or any other name you would like) attribute:使用authroutes (或您想要的任何其他名称)属性设置一个组:

Route::group(['authroutes' => true, ....], function () {
    Auth::routes(...);
});

In a view you can check the current route to see if it has this authroutes action attribute:在视图中,您可以检查当前路由以查看它是否具有此authroutes操作属性:

@if (Request::route()->getAction('authroutes', false))
    // this is one of the routes from `Route::auth`
@endif

getAction takes the property we want to find and a default value. getAction获取我们想要查找的属性和默认值。

You can create a helper function that will check auth routes.您可以创建一个帮助程序 function 来检查身份验证路由。

function checkAuthRoute() {
 $routes = \Route::getRoutes();
 // iterate routes
 foreach($routes as $route){
   // your logic here
 }

}

Check whether the Current route name falls within the desired list.检查当前路由名称是否在所需列表中。

@if (in_array(request()->route()->getName(), ['login', 'register']))

// Request is in Auth Routes

@else

// Request not in Auth Routes

@endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM