简体   繁体   English

如何通过Laravel Blade获取页面名称

[英]How can I get the name of a page via Laravel Blade

Here is the code of my controller: 这是我的控制器的代码:

    public function getIndex()
{
    // Get all the apps
    $apps = $this->app->orderBy('created_at', 'DESC')->paginate(50);

    // Show the page
    return View::make('site/apps/index', compact('apps'));
}

From this how can I get in my template "site/apps/index" the name of the page? 由此我如何在模板“ site / apps / index”中获取页面名称?

When I do a var_dump() I get a huge array and object that is just impossible to sift through. 当我执行var_dump()时,我得到了一个巨大的数组和对象,这是无法筛选的。

I am looking for a best practice. 我正在寻找最佳实践。

The reason I am trying to get the name of the page is to control when a CSS value is added to the HTML. 我尝试获取页面名称的原因是为了控制何时将CSS值添加到HTML。

Here is what I am doing right now: 这是我现在正在做的事情:

@if (Auth::check())
    <style>
        body {
            background-color: #fbfbfb !important;
        }
    </style>
    @endif

But not all the pages when I login should have that background color, just one... The page name is apps. 但是,并非所有登录时的页面都应具有该背景色,而只有一种...页面名称是apps。

If you have a better way of doing this you are welcome to share it. 如果您有更好的方法,欢迎您分享。

One way you can accomplish this is to get the current URL and validate it against the URL that you want to make the CSS change on. 实现此目的的一种方法是获取当前URL并针对您要在其上进行CSS更改的URL进行验证。 Take a look at this post to see how this is done. 看看这篇文章 ,看看如何完成。

When I look into the code it looks like you manipulate the style of the body element depending whether a user is logged in or not. 当我查看代码时,好像您根据用户是否登录来操纵body元素的样式。

I would solve this task as follows: 我将按以下方式解决此任务:

Create two classes and add those into your external CSS file. 创建两个类,然后将它们添加到您的外部CSS文件中。

.body-logged-in{
  background-color: #somecolor;
}

.body-not-logged-in{
  background-color: #someothercolor;
}

In your template file echo the relevant class name depending on the information you get from the Router or Controller. 在模板文件中,根据从路由器或控制器获得的信息,回显相关的类名。

eg Router: 例如路由器:

Route::get('yourpage', function()
{   
    $users = User::all();
    return View::make('yourtemplate')->with('users', $users)->with('loggedInStatus', 'body-logged-in'); 
});

As your template get passed authentication details of the user, you know that a current user is logged in. Alternatively pass the loggedInStatus with your controller to the template. 当模板通过用户的身份验证详细信息时,您就知道当前用户已登录。或者,将LoggedInStatus与控制器一起传递给模板。

It could look like this 它可能看起来像这样

<body class="{{ $loggedInStatus }}" >

With this approach you keep MVC principle and don't mess up your code. 使用这种方法,您可以保持MVC原理,并且不会弄乱您的代码。 If you want to make changes to the CSS style, just edit the .CSS file. 如果要更改CSS样式,只需编辑.CSS文件。 This way you do not need to touch the template/view file. 这样,您无需触摸模板/视图文件。 In addition, if you want to add this to other pages, you do not need to check for the page name and work with views and CSS only. 此外,如果要将其添加到其他页面,则无需检查页面名称,仅使用视图和CSS。

At the end it depends on what your business logic requires. 最后,这取决于您的业务逻辑要求。 Hope I could give you some ideas . 希望我能给你一些想法。

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

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