So last time we were able to sign in the user.
Let's see if we can do something quick but interesting right now.
So if we are signed in we should be able to sign out.
And if we are not signed in, well we should be able to go to the login form.
And that would be the topic of this video.
What we would achieve is we will display a conditional link in the main layout.
If the user is authenticated, he will see an option to log out.
Otherwise to log in.
Let me jump to this main layout and let me show you the way.
I'd like it to be done, so it would be super nice if I can just add an if statement and check for some
kind of variable.
Let's call it user and this variable should contain the user model.
If the user is authenticated or otherwise should be null.
That would really make our life simpler.
Now, I shouldn't be able to fetch the current user and check if he or she is authenticated in every
controller action.
This would be an overkill.
So what I'd like to do if if the user is authenticated, I'd like to display him a logout link.
But we need to think about the way of how can we make this possible?
How can we have some kind of, let's call it global view variables, that we can automatically assume
that those variables exist in every single template, whether that's layout or a normal template, and
that we don't really have to explicitly make sure it exists.
Why don't we jump to the view class from the chord namespace, and we always render everything.
Using this render method.
So we don't use render template nor render layout.
That's always the main entry point.
And we can see that we are passing the data.
That is being used both to render the template and then to render the layout.
What if we have some global way to supplement this data?
What if we also have a field here that is static and is called globals, something we already know from
PHP?
We've got some super globals.
This is a little different.
We just call it globals.
It's not a global variable and it is protected, which means we don't want this Accessible outside this
class and it would be an associative array.
Let's initialize it.
Then we need a way to add something to this global array.
Okay, not very complicated.
Let me add a static method.
I'm going to call it share as we are sharing some information.
And we need a key.
And let's make it mixed some kind of value.
And it is void.
This would be super simple.
We just work with the global array and under a given key we are adding the value.
Not complex at all.
And once we render something now it's the time that we add something to the data.
So data is supposed to be an array.
So what we can do is we can add static globals.
And the data itself.
This way we're always supplement the data with the global values that we have inside the view.
So let's see if this works.
The problem I see currently, if we don't really have a good place where we can share the user with
the views, let's call it this way.
That's why temporarily let's add that inside our front controller.
That's the index.php file inside public folder.
Later on we're going to do it differently through a mechanism called middleware.
That's super useful and interesting mechanism that you're gonna use in any HTTP framework, whether
that's PHP or Node.js or Python.
So that would be something interesting to learn for sure.
For now, maybe right after the Root's definition, we can just get the view class, mind the imports
that need to be added.
It has a share method.
And let's share a user.
And let me just put some text inside just for now.
And if we take a look at our layout, if the user is well non-empty not null, we can display the logout
link.
And we can safely use this variable because we are assuming that it just has to be defined.
That's one of the magic variables that we assume we have available inside our templates.
Let's try this.
Let me refresh this page and see what happened.
I now have this logout button.
So do we have the user.
Let's see.
Let's see what does the user contain by just echoing it.
So the user contains my name which confirms that this mechanism works perfectly.
So now that we know, how can we share some information with every single view?
For example, to allow users to log out if this variable is set with something, then.
Next up, we need to figure out a way to get the currently authenticated user model to actually be able
to process logging out or displaying the sign in link.
And we're going to do that next.