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. Take a look at this post to see how this is done.
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?
When I do a var_dump() I get a huge array and object that is just impossible to sift through.
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.
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.
If you have a better way of doing this you are welcome to share it.
Решение
Другие советы
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.
I would solve this task as follows:
Create two classes and add those into your external CSS file.
.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.
e.g. 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.
It could look like this
<body class="{{ $loggedInStatus }}" >
With this approach you keep MVC principle and don't mess up your code. If you want to make changes to the CSS style, just edit the .CSS file. 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.
At the end it depends on what your business logic requires. Hope I could give you some ideas .