Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
Laravel400
  • Overview
  • Transcript

6.2 Using a Presenter

There are always the times when we need to perform just a little bit of logic on some data before we display that data in the view. And that always raises the question as to where do we do that logic? Do we do it in the view? Well, no, we don't. Do we do it in the model? And once again, no, we don't. This is something that is specifically for the view, it's for displaying the data and nothing else. So it doesn't make sense to put it in the model as well. Like for example, here we have our list of pages. And while it's okay that we just have a list of pages, it would be nice to have some visual cue as to see what page is a child of what other page. So, the FAQ sheet is a child of About, so it would be nice if there was some indentation here. And that's logic that doesn't belong in the view, and it's also logic that doesn't belong in the model. So this is where something called a view presenter would be very useful. And there are many view presenter packages that we could use. Unfortunately it's not built in to Laravel, but there are many packages. And the one that we are going to use has been written by Jeffrey Way. It is simply called Laracasts Presenter, so we're going to say composer require Laracasts/presenter. And while that is installing, we are going to go to our project and instead of app, I'm going to create a new folder called Presenters. Now, it doesn't matter where you put your Presenters. I'm organizing this so that all of my presenters are going to be inside of this folder called Presenters. And we are going to create a new file called PagepPresenter. So we typically have a class for an individual model. So PagePresenter let's have our piphp tag. Let's also have the namespace of App\Presenters and we also need to use Laracasts\Presenter\Presenter. Because all of our Presenter classes are going to inherit this Presenter class, so class PagePresenter extends Presenter. And then the methods inside of this class are going to be used inside of our view for performing whatever logic that it needs. So we need a function or a method rather for padding the title. So let's just call it paddedTitle and the first thing we need to do is get the padding which we will just set as a empty string right now. And then we will return padding, we will concatenate this, and title. But before we can even use this, we do need to go to our model, and we do need to add a trait here. So we first want to use Laracasts\Presenter\ Presentabletrait. That's what we want. And then we will simply use that trait inside of our class. But then we also have to specify what the Presenter this model is going to use. So we do that with a protected field called presenter, and that's it's simply the path. So App\Presenters\PagePresenter. So there we have set that up, so we can close our model. And let's go to our views, so that is resources > views > admin > pages > index. And instead of using our title property here, we are going to say, present, we're going to call that method. And then we're going to use our paddedTitle but we aren't going to call it as a method, we're going to use it as a property. So that's going to give us whatever that is going to give us. Right now it's just going to give us the exact same thing because an empty string is well, it's empty. So let's do this. Let's just add some dashes, so that we can see what that looks like. Now right now it's going to add dashes to everything because we haven't taken the depth of the page into account. By depth I mean how deep in the tree it is. So you can think about the root of something, about is one of those root pages. So it's depth will be zero. The FAQ sheet is a child, a direct child of About. So its depth would be one. If there was a child of the FAQ sheet, then that child's depth would be two. So basically what we want to do is take the depth of the page and use that to calculate how many spaces that we're going to add to the title. So we can do something like this, we can say str_repeat and we're going to use the nonbreaking space, so we need an  , and then we have a property called depth. Now, right now, we aren't going to get anything for this value. We're going to have to make some modifications to our controller. But this is going to be fine for now, we're going to take our depth and then we're going to multiply it by four. So there's going to be four spaces for each level of child. So let's refresh the page, we just don't see anything except the dashes go away. So here's the thing, the depth isn't given to us by default, we have to explicitly say that we want the depth as well. So we wanted to go to our pages controller. And this is for the index. So we're going to say defaultOrder, I mean nothing's going to change there, but then we want to say widthDepth. That is going to include the depth so that we can then use the depth property. So now, whenever we view this, we're going to see, well, Call to undefined method, Query\Builder::widthDepth(). It helps if you type it correctly. So we will withDepth, that's kind of hard to say. So whenever we refresh, now we see that there is a visual difference. So let's play around with this, let's make contact a child of the FAQ sheet. Now we will come back and we will add the same thing there. But let's submit, and then we can see how our tree is being built. But I don't want contact there, I want contact to be after About, so we will make that back and there we go. So now we need to do something for our drop down box which is essentially going to be the same thing. It's just that we need to include the widthDepth before we pass this to the view. So withDepth is going to be used there and then we need to go down to the edit method and add that as well. So now let's just refresh the page and whenever we, well, that will. We also have to change the view [LAUGH] as well. So we need to go to our views > partials > fields. And we are going to use not the title but instead we are going to say, present() and then paddedTi. So now whenever we refresh we are going to see the padding there, and thanks to a View Presenter, we have some clean code for that. So whenever you need to perform even just a little bit of logic for the data that's going into your view, a View Presenter is a very good option. Use whatever package that you want, set it up correctly, and use it.

Back to the top