تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Planning Reply Functionality0:00

We can create posts now we need to be able to reply to them. Now, when we look at the ui, I don't think it makes a lot of sense to display a reply form for every single post. That's just going to clutter things. So I think what we will do is just set up everything so that we can get the functionality working and then we will hide it that way. Jeffrey can do whatever he needs or wants to do when it comes to displaying the reply form.

Adding Reply Route0:29

Jeffrey can do whatever he needs or wants to do when it comes to displaying the reply form. So once again, let's start with our routes because I think that makes the most logical place. And I want a URL similar to what we have for displaying an individual Post, except that we just need another segment. We need to be able to reply to it. So inside of our group for the auth middleware, I'm going to bind the profile and the post together.

So inside of our group for the auth middleware, I'm going to bind the profile and the post together because we want to reply to a post by the given profile, but we will add the reply segment to the URL. The method name will be reply, and then the name of the route will be reply. Now, I know that this gets away from the typical crud controller, but you know, you can make the argument that this isn't typical crud.

but you know, you can make the argument that this isn't typical crud. I mean, yes, we are still creating reading, updating and deleting, but we have a PostController for displaying posts for creating posts. It kind of makes sense to go ahead and use that PostController to create replies and that's what we are going to do. Now this is a POST request, however, so we need to make sure

Implementing Reply Controller1:41

Now this is a POST request, however, so we need to make sure that we do indeed make this a POST request. So with that out of the way, let's open up the PostController because this is of course where we will need to, you know, do our thing. However, I want to take the show method and use that as the basis because this is going to give us the signature that we need,

and use that as the basis because this is going to give us the signature that we need, except we also need the request. But you know, as far as creating a reply, all we are doing is creating a Post for another Post. So we already have all of the information. The only thing that we need to supply is the content. So we have that to create PostRequest class. So let's use that and that way we can validate the content and we'll be good to go.

So let's use that and that way we can validate the content and we'll be good to go. So that then it's essentially going to start off the same way as the store method, except that we need to call this currentProfile, this is the logged in profile. And then we will create our post using the reply method that we created. But the currentProfile is what is going to reply to the post with the content from the request.

Extracting Reply Form Component2:45

But the current profile is what is going to reply to the post with the content from the $request. And then from there, you know, what do we do? Uh, I'm just gonna redirect back to the post index, which is our home. And if Jeffrey wants or needs to do something else, then he can do that. So with that out of the way, we just need to implement our reply form. And let's look at the

to implement our reply form. And let's look at the feedItem.blade.php partial view, because that is where we have our reply form. It's at the very bottom of a Post and it is before the threaded replies. Now looking at this, you know, in the previous episode we created a PostForm component. And you know, my mindset was to, you know, reuse that for both the post creation and the reply.

And you know, my mindset was to, you know, reuse that for both the Post creation and the reply. But I'm beginning to think maybe we should just separate those two things together, separate them together, separate them from each other so that we have an individual component for each form. It kind of just makes sense to do that and ultimately it makes things simpler to use. So that's what I'm going to run with.

and ultimately it makes things simpler to use. So that's what I'm going to run with. So that means I need to extract, starting from this div element all the way down to this div element. And we are going to, uh, use artisan to make a component called ReplyForm. Now, you know, my first thought is this could just be a view component. However, you know,

a view component. However, you know, looking at this, you know, we still need the profile. We need to be able to create links to the profile of the currently signed in profile and display the avatar and things like that. So if we have a component that has a class, we could automatically supply the profile that way. That way we don't have to pass the profile down from one component to the next component to the next component.

That way we don't have to pass the profile down from one component to the next component to the next component. It's just automatically supplied. So let's do that. So inside of the ReplyForm class, we will first of all have a public profile, which we'll just call profile, so that inside of the constructor we will say that the profile is equal to auth::user(). And then we will get to the profile that way. Of course, we need a use statement for profile and for auth. And I'm going to go ahead

Of course, we need a use statement for Profile and for Auth. And I'm going to go ahead and also pull in a use statement for the Post model because if we are replying to a Post, we need to know what that Post is. So we're going to have a public Post called post, and we need to be sure that this post here is our Post model, not the post component, because that will of course cause some problems.

not the Post component, because that will of course cause some problems. But with that done, you know, we're, we're pretty much done with this class so that we can go to the reply form view and we just need to, well, I closed the file, didn't I? Um, we need to paste in the markup. So let's open up the feed item. Again, we want to copy all of this markup so that we can paste it in here.

Again, we want to copy all of this markup so that we can paste it in here. And since we have the profile already, we can go ahead and output some of these things. So the route here will be for profiles.show, and then we will provide the profile there. Then we need to display the avatar, URL for the profile. We also want to use the displayName for the avatar. So we'll have displayName and you know, I think that's it. We are going to use the markup

So we'll have display name and you know, I think that's it. We are going to use the markup for this partials post form view. So let's just grab this and we're gonna paste it in here. Now of course we do need to make some changes so that now we don't have to pass in the label text or anything like that. It's just going to be provided here. So the label text is going to be replyBody. So let's use that where we have our label text.

So the label text is going to be replyBody. So let's use that where we have our label text. And as far as the field name is concerned, since we are using that createPost request, the form field needs to be content. So we will use that there. The placeholder is, I don't remember, we'll grab it, but I know that the rows are 5 and now we don't need to check if we have a value for rows because it's just going to be automatically put in.

and now we don't need to check if we have a value for $rows because it's just going to be automatically put in. So now we need the placeholder, which is reply to the post profile display name. So with that in place, we can get rid of that. Include. And we do need to paste in the placeholder here, but we don't have $item. Now we have a Post so that we will get the profile, the display name, and that should be that. So that then we just need to display this Post.

the display name, and that should be that. So that then we just need to display this Post. And I think that is done. Where is that done? I think it is in the post.blade.php component. And it will be right here after this end. If so, we will have the reply form so that the post is the provided post that this Post component is displaying and that should be most everything. Let's do a sanity check in the browser.

Wiring Form Action and Testing7:57

and that should be most everything. Let's do a sanity check in the browser and we see our reply form. Let's do this for a single Post so that we're not bombarded with all of these posts. Okay, so we are going to reply to the Post that we created in the previous episode, but we're not completely done because we do need to set the URL for the form here. The method is going to be POST the action is going to be

because we do need to set the URL for the form here. The method is going to be POST the action is going to be for the route of posts.reply, and then we need the profile, which was what the post.profile. And then we also need the post itself, which is the post. So that should give us our URL. We do need the CSRF token, so that's, that will work. But I think everything else is gonna be okay.

We do need the CSRF token, so that's, that will work. But I think everything else is gonna be okay. So if we go back to the browser and we refresh, let's do a hard refresh. Let's reply here. So we will submit this. Hopefully we get redirected and we're not going to see the reply here, but if we go back to the page for that Post, yes indeed there is that reply. So we've just created our reply,

Refactoring PostForm Consistency9:06

yes indeed there is that reply. So we've just created our reply, we have everything ready to go. However, since we have this reply form as the form itself, I want to make the same change to the PostForm component because I want these things to be consistent. I don't want one form to be one way and the other form to be the other way. So it's just a, a few changes.

and the other form to be the other way. So it's just a, a few changes. It's not gonna be very much at all. We'll start with the ReplyForm class. We're gonna copy that and we're just gonna rename it to PostForm. We need the Profile here as well, but we don't need the parent Post. So we can get rid of that, which means we can get rid of the use statement for the Post model.

So we can get rid of that, which means we can get rid of the use statement for the Post model. And that's gonna be it as far as the class is concerned. So we can close those files, but then we need the markup here. So if we go to the index.blade.php view, that's where we can pull in that markup. So we have the div all the way down to that a element. We're gonna cut that out so that we can put it inside of our post-form.blade.php view.

We're gonna cut that out so that we can put it inside of our post form Blade view. And the great thing is we already have the profile being pulled in for this component, so we don't have to worry about that. The only other thing that we do need to worry about are the values that we supply to the post form. But again, it's gonna be easy enough to do, let's add the closing div tag there.

But again, it's gonna be easy enough to do, let's add the closing div tag there. So that then is just a matter of changing these values. The field name we know is content, so we will use that for our field name. The label text I believe was post body. The action here is now going to be the route for, well, we can just copy it, can't we? We can take the route for posts.store and use that for our action.

We can take the route for posts.store and use that for our action. We still have CSRF. The only other thing is the placeholder. We don't have any rows here. So we can take what we have for the placeholder. Let's cut that out. We will paste it in here, which will make this easier all the way around anyway. So we will display the profile handle and that's gonna be that.

So we will display the profile handle and that's gonna be that. So we can get rid of all of these things for the HostForm. And now everything should work inside of the browser. We still see the reply form here. That's great. But let's go to our home. So we will go home and let's just be sure we didn't break anything, which obviously we did. Cannot read. Declare class, ReplyForm. Yeah, we can't.

which obviously we did. Cannot read. Declare class, ReplyForm. Yeah, we can't. Can we? Because if we take a look at PostForm, we need to change the name of that class to PostForm. Alright, so now we have undefined variable profile and I bet that's because we didn't change the render method. This is now going to be components PostForm. Okay, so let's do a hard refresh so that everything is loaded. Nope, but maybe we need to clear the view.

so that everything is loaded. Nope, but maybe we need to clear the view. So php artisan view:clear and back in the browser. Sure enough, that was a problem. So that's now you know, we have that Post form that we've just modified. Let's be sure that we can post another Post and hopefully we should see that we can post another Post. So great, we can create Posts, we can reply to Posts. We have those two separate components for the Post

So great, we can create Posts, we can reply to Posts. We have those two separate components for the Post and Reply forms. So now we need to focus on Reposts.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟