WEBVTT

00:00.800 --> 00:04.670
So last time we were able to sign in the user.

00:04.700 --> 00:09.980
Let's see if we can do something quick but interesting right now.

00:10.010 --> 00:15.380
So if we are signed in we should be able to sign out.

00:15.710 --> 00:21.830
And if we are not signed in, well we should be able to go to the login form.

00:21.830 --> 00:24.620
And that would be the topic of this video.

00:24.650 --> 00:31.070
What we would achieve is we will display a conditional link in the main layout.

00:31.100 --> 00:35.720
If the user is authenticated, he will see an option to log out.

00:35.750 --> 00:38.030
Otherwise to log in.

00:38.810 --> 00:43.460
Let me jump to this main layout and let me show you the way.

00:43.460 --> 00:51.530
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

00:51.530 --> 00:52.730
kind of variable.

00:52.730 --> 00:59.390
Let's call it user and this variable should contain the user model.

00:59.390 --> 01:03.990
If the user is authenticated or otherwise should be null.

01:04.620 --> 01:07.710
That would really make our life simpler.

01:08.130 --> 01:15.990
Now, I shouldn't be able to fetch the current user and check if he or she is authenticated in every

01:15.990 --> 01:17.520
controller action.

01:17.550 --> 01:19.860
This would be an overkill.

01:20.370 --> 01:28.680
So what I'd like to do if if the user is authenticated, I'd like to display him a logout link.

01:29.910 --> 01:34.470
But we need to think about the way of how can we make this possible?

01:34.500 --> 01:43.680
How can we have some kind of, let's call it global view variables, that we can automatically assume

01:43.680 --> 01:54.630
that those variables exist in every single template, whether that's layout or a normal template, and

01:54.630 --> 01:59.490
that we don't really have to explicitly make sure it exists.

02:01.830 --> 02:12.290
Why don't we jump to the view class from the chord namespace, and we always render everything.

02:12.320 --> 02:14.780
Using this render method.

02:15.680 --> 02:20.510
So we don't use render template nor render layout.

02:20.540 --> 02:23.330
That's always the main entry point.

02:23.480 --> 02:27.980
And we can see that we are passing the data.

02:28.010 --> 02:33.620
That is being used both to render the template and then to render the layout.

02:33.770 --> 02:40.130
What if we have some global way to supplement this data?

02:40.160 --> 02:52.610
What if we also have a field here that is static and is called globals, something we already know from

02:52.790 --> 02:53.180
PHP?

02:53.210 --> 02:56.060
We've got some super globals.

02:57.260 --> 02:59.000
This is a little different.

02:59.000 --> 03:00.680
We just call it globals.

03:00.710 --> 03:08.880
It's not a global variable and it is protected, which means we don't want this Accessible outside this

03:08.880 --> 03:12.510
class and it would be an associative array.

03:12.540 --> 03:13.740
Let's initialize it.

03:13.740 --> 03:20.610
Then we need a way to add something to this global array.

03:21.480 --> 03:24.690
Okay, not very complicated.

03:25.110 --> 03:27.870
Let me add a static method.

03:27.960 --> 03:33.060
I'm going to call it share as we are sharing some information.

03:33.930 --> 03:35.610
And we need a key.

03:35.940 --> 03:42.660
And let's make it mixed some kind of value.

03:44.580 --> 03:46.350
And it is void.

03:47.940 --> 03:49.860
This would be super simple.

03:49.860 --> 03:57.930
We just work with the global array and under a given key we are adding the value.

03:58.770 --> 04:01.440
Not complex at all.

04:01.470 --> 04:09.720
And once we render something now it's the time that we add something to the data.

04:09.740 --> 04:12.350
So data is supposed to be an array.

04:12.980 --> 04:18.470
So what we can do is we can add static globals.

04:21.320 --> 04:23.390
And the data itself.

04:25.460 --> 04:33.020
This way we're always supplement the data with the global values that we have inside the view.

04:35.210 --> 04:37.730
So let's see if this works.

04:37.760 --> 04:48.230
The problem I see currently, if we don't really have a good place where we can share the user with

04:48.230 --> 04:50.600
the views, let's call it this way.

04:52.430 --> 04:58.730
That's why temporarily let's add that inside our front controller.

04:58.730 --> 05:02.660
That's the index.php file inside public folder.

05:02.960 --> 05:08.420
Later on we're going to do it differently through a mechanism called middleware.

05:08.450 --> 05:16.670
That's super useful and interesting mechanism that you're gonna use in any HTTP framework, whether

05:16.670 --> 05:19.850
that's PHP or Node.js or Python.

05:20.030 --> 05:24.290
So that would be something interesting to learn for sure.

05:24.320 --> 05:33.830
For now, maybe right after the Root's definition, we can just get the view class, mind the imports

05:33.830 --> 05:35.540
that need to be added.

05:36.350 --> 05:38.360
It has a share method.

05:38.360 --> 05:41.420
And let's share a user.

05:41.420 --> 05:47.480
And let me just put some text inside just for now.

05:47.510 --> 06:00.230
And if we take a look at our layout, if the user is well non-empty not null, we can display the logout

06:00.260 --> 06:01.040
link.

06:01.040 --> 06:10.460
And we can safely use this variable because we are assuming that it just has to be defined.

06:10.490 --> 06:17.140
That's one of the magic variables that we assume we have available inside our templates.

06:17.890 --> 06:19.420
Let's try this.

06:20.200 --> 06:24.010
Let me refresh this page and see what happened.

06:24.610 --> 06:27.100
I now have this logout button.

06:28.840 --> 06:31.960
So do we have the user.

06:31.990 --> 06:33.100
Let's see.

06:33.190 --> 06:37.480
Let's see what does the user contain by just echoing it.

06:37.750 --> 06:44.440
So the user contains my name which confirms that this mechanism works perfectly.

06:46.240 --> 06:51.670
So now that we know, how can we share some information with every single view?

06:52.630 --> 07:01.360
For example, to allow users to log out if this variable is set with something, then.

07:01.360 --> 07:10.060
Next up, we need to figure out a way to get the currently authenticated user model to actually be able

07:10.060 --> 07:14.290
to process logging out or displaying the sign in link.

07:14.320 --> 07:16.510
And we're going to do that next.
