Refactoring Routes to Controllers0:00
Okay, let's refactor our routes file and make use of controllers. So throughout this series, I pretty much just dumped all of the logic into the routes file, which is fine, since the main purpose of this series is to show you how to use React Native. But it does seem a bit messy to me, so I'll spend this video refactoring our routes file. So I don't have tests here, which would make it easier, and I would definitely write tests if this were a real app. But since we don't have tests, we have to manually test everything. So let's start with the auth stuff. So login and logout. So let's make a new route here.
Extracting Login/Logout Controller0:31
So login and logout. So let's make a new route here. I'll use my snippet, route.get. So it's going to be a POST request. It's going to be the login route. We'll make a new one called AuthController. And you can call a method called login, but I like to try to use the resource methods that are available. So we'll make use of the store method, since we're creating a token. And we'll name this login.
So we'll make use of the store method, since we're creating a token. And we'll name this login. I'm not making use of route names on the front end. But again, if this were a real app, I might consider that. So let's go ahead and make a new controller here. So let's say php artisan make:controller AuthController. And let's make it resourceful. Okay, let's go to that. Actually let me copy this. So all of the logic for logging in, let's comment it out.
Actually let me copy this. So all of the logic for logging in, let's comment it out. And let's paste it in our AuthController. And I said it was in the store method. Okay, let's paste that in. Let's re-indent here. And let's make sure to import User. Okay. And Hash, okay. And this ValidationException.
And hash, okay. And this validation exception. Okay, so that looks good. Save that. Let's move on to log out. And I'll just leave this until we verify that it's working. So for log out, let's do that as well. It's a POST request as well. And it's probably better to make it a DELETE request, since we're deleting a token. But I don't want to change the front end here.
And it's probably better to make it a DELETE request, since we're deleting a token. But I don't want to change the front end here. So it's /logout. We're going to make use of the AuthController. And we'll make use of the destroy method. And we'll name it logout. Okay. Did I import AuthController? I'm not sure if I did. Okay, it's imported now.
I'm not sure if I did. Okay, it's imported now. And let's grab the logic. And let's comment this out. Let's go to the AuthController. Let's go to the destroy method. We don't need this param here. So we can get rid of the comment as well. And let's paste this in here. Okay.
And let's paste this in here. Okay. So I am grabbing the logged in User from the request here. But I'm going to change that and just make use of the authUser method. So authUser, okay. So let's try this out. Hopefully, I did everything correctly. So let's make sure log in and log out still works. So let's try logging in. Okay, let's log in here.
So let's try logging in. Okay, let's log in here. Okay, it seems to work. Let's log out. And we're getting this error because there needs to be middleware on the log out route. So let's add that here, middleware auth:sanctum, okay. So let me just log in and log out again. Okay, and let's try logging out again. And now we don't get that error. Let's just make sure that the server errors don't work as well.
Creating Register Controller3:46
And now we don't get that error. Let's just make sure that the server errors don't work as well. And they do. Cool. So we can get rid of these comments. Okay, so for register, let's go ahead and make a route for that or a controller for that. So let's say Route::get, say POST request, register. And we can use the AuthController, but I'm going to make a new one. So we can make use of the resource methods.
And we can use the AuthController, but I'm going to make a new one. So we can make use of the resource methods. So let's say RegisterController, and we'll make use of the store method. And we'll name it register. So let's go ahead and make that should be RegisterController. Okay. Let's make sure to import RegisterController. Okay. And let's go ahead and grab all of this logic and paste it into there. You can also make it an invocable method on the controller, since we're only making
And let's go ahead and grab all of this logic and paste it into there. You can also make it an invocable method on the controller, since we're only making use of one method, but I'm okay with just using the store method. So let's comment this out. Let's go to our RegisterController, the store method. And let's paste that in. Okay. We have to import User and Hash. So let's do that. Hash.
So let's do that. Hash. Okay. And hopefully that works. So let's go ahead and give that a try. Did I comment out my original route here? I did. Let's register. Let me just fill this out. Okay.
Refactoring Tweet CRUD Routes5:24
Let me just fill this out. Okay. So I'm going to register here. And that looks like it worked. Cool. So we can get rid of all of this here. Okay. So next, this stuff has to do with following, and we'll do that later on. But next I want to take care of everything that has to do with CRUD on tweets. So this is deleting tweets.
But next I want to take care of everything that has to do with CRUD on tweets. So this is deleting tweets. This is creating tweets, and this is showing a single tweet. This is showing all tweets that the user is following, and this one shows all tweets. So let's start with all tweets here. So this one. So all tweets that the user follows. So I believe I have a TweetController that was created when I created the Tweet model. So if I open my sidebar, there should be a TweetController here. There it is.
So if I open my sidebar, there should be a TweetController here. There it is. So we'll make use of that. And it is resourceful. So let's just hide this. Let's go to our routes/api.php. Let's start with this one, like I said. So it is a GET request, this /tweets. And we're making use of the TweetController. And we are making use of the index method.
And we're making use of the TweetController. And we are making use of the index method. And let's call it tweet.index. So let's make sure to import TweetController. And we also need to make use of the middleware here. And all of these routes need to make use of that middleware. So I'm going to make a route group here. Actually, let me just copy this. And then we'll make it a group. So right here, route middleware auth:sanctum.
And then we'll make it a group. So right here, route middleware auth sanctum. And then we can say group. And then we can have a function. And this callback will be all of our routes. So let's put this in there. And the middleware should be applied to everything within this closure. So let's grab this. Let's comment it out. And let's put a semicolon here.
Let's comment it out. And let's put a semicolon here. Let's go to TweetController index. And let's paste that in. Let's re-invent. And that should be fine. Let's just re-format this here. OK, next. So we have this tweetAll endpoint, which shows all tweets. So we can pretty much just duplicate this one.
So we have this tweetAll endpoint, which shows all tweets. So we can pretty much just duplicate this one. Say tweetsAll. And we can either combine it with this controller, or we can make a new controller. So we can make use of the index method again. So it's up to you what you want to do. I'll make a new controller here so we can make use of the resourceful method. And let's rename this to tweetAllIndex, or tweet.index.all. OK, let's go ahead and make that controller. So it's TweetAllController, or AllTweetsController.
OK, let's go ahead and make that controller. So it's TweetAllController, or AllTweetsController. TweetAllController. Let's go ahead and import this. OK, let's grab this. Let's comment this out. And we actually don't need this route as well. Let's go to TweetAllController index. And let's paste that in. We have to import tweet here.
And let's paste that in. We have to import Tweet here. OK, and that should be good. And you can clean up all the other methods that you're not using as well. But I'll just leave it. OK, next is this one, which shows a single tweet. So we'll make use of the show method. So let's duplicate this. The end point is tweets/{tweet}. And we're just going to make use of the TweetController, the show method.
The end point is tweets/{tweet}. And we're just going to make use of the TweetController, the show method. And we'll call it tweet.show. OK, let's grab this. Let's go to our TweetController show. And the tweet is ready in the $param. Let's paste that in. And that should be good to go. OK, let's comment this out. Trying to do this all in one shot, at least for the tweet section.
OK, let's comment this out. Trying to do this all in one shot, at least for the tweet section. Next, we have creating a tweet, I believe. Yep, so let's grab all of this. It's /tweets, and it's a POST request. OK, so let's comment this out. Let's make that tweet, or method, or endpoint, I mean, /tweets, TweetController. And we'll make use of the store method. And we'll name it tweet.create, or store.
And we'll make use of the store method. And we'll name it tweet.create, or store. Let's go to that TweetController store. Let's paste that in. Let's re-indent. And hopefully, this works. And the last one for our tweets should be deleting tweets. Right here, so it's a DELETE request to this endpoint. And we'll grab the logic here. OK, let's duplicate this.
And we'll grab the logic here. OK, let's duplicate this. It's a DELETE request. tweets/tweet, TweetController destroy. And let's name it tweet.delete. OK, let's go to that, the destroy method. Let's paste that in. Let's re-indent. Let's save. And hopefully, I did everything correctly.
Let's save. And hopefully, I did everything correctly. Now we can test it out, or at least the tweet section. So let's go ahead and log in. OK, so tweets works. Let's try tweets all. That works as well. Let's try a single tweet. That works as well. Let's try adding a new tweet.
That works as well. Let's try adding a new Tweet. Adding new Tweet. That works too. And let's try deleting a Tweet. So I'm going to delete this one. And that works as well. Cool. So let's just clean up all the commented out code here. Now let's take care of these two routes.
Public Profile and User Tweets11:42
So let's just clean up all the commented out code here. Now let's take care of these two routes. So this one gets the user's profile, and this one gets the tweets of that specific user. So I didn't put these behind middleware, but I think I'm going to do that now. So I'll grab this. And let's do the same process here. Actually, I'm not going to put it behind middleware, since users who are not logged in.
Actually, I'm not going to put it behind middleware, since users who are not logged in should be able to view this information as well. That all depends on what you want to do, but let's make it public. So let's start with this one here. So say Route::get, say GET request to the same route. And let's make a new controller called UserProfileController. And we'll make use of the show method,
called UserProfileController. And we'll make use of the show method, and we'll name it user.profile.show. Okay, so let's make that controller. UserProfileController. Okay. Let's make sure to import that. Okay, let's grab the logic from here. And let's comment this out. Let's paste that in UserProfileController show.
And let's comment this out. Let's paste that in UserProfileController show. Paste that in. Let's re-indent here. And let's grab the $user here. $user, $user. Let's import that. And that should be good. Next is the user's tweets. Again, if you want,
Next is the user's tweets. Again, if you want, you can make use of this controller that we made, or you can make a new one. So I'll just make a new one here, so we can make use of the resourceful methods. So I named it UserTweetsController. Okay. And I just made use of the index method. So let me just delete this now,
And I just made use of the index method. So let me just delete this now, since I know it should work. And we'll add this route as well. /user, /user. /tweets. UserTweetsController index. And I named it user.tweets.index. Okay. Let's grab this.
Okay. Let's grab this. And we also need to take in a User for the param. Let's comment this out. UserTweetsController index. Again, we need the User here. Okay. And let's paste that in. Make sure to import User. And hopefully that works.
Make sure to import User. And hopefully that works. So let me just delete this. And if we go to any profile page, let's just save this. If that works, then I did everything correctly. So let's go to this person's profile. And the tweets don't seem to work. Profile does work. And I forgot to import the class.
Profile does work. And I forgot to import the class. So I imported that class now. Let's save. Let me just reload the app to make sure. Let's go to this person's profile. And it does work. Cool. And our last set of routes are for following and unfollowing users.
Following/Unfollowing Route Refactor14:52
And our last set of routes are for following and unfollowing Users. So we have followUser, unfollowUser, and one for checking. So let's go ahead and make a route group again, because we do need the sanctum middleware. So let's put it here. Okay. Let's get rid of all of these. And let's start replacing these routes.
Let's get rid of all of these. And let's start replacing these routes. Okay, let's make the first one, which is for following users. It's a POST request. And we have follow/user. I named the controller UserFollowController. And for following, I'm using the store method. And it's a user.follow.
And it's a user.follow. Okay. Let's go ahead and make that controller. UserFollowController. Okay. Let's make sure to import that here. Okay. Let's duplicate this for unfollowing. And again, it should be a DELETE request, but POST is fine as well.
And again, it should be a DELETE request, but POST is fine as well. Unfollow. And we'll make use of the destroy method for this. And it's called userUnfollow. Okay. So for following, we need to take the $user in as a param. Let's grab this. Let's comment it out. Let's go to UserFollowController store.
Let's comment it out. Let's go to UserFollowController store. Let's paste that in. Again, we don't need the request, but we do need the user in our params. So let's grab a User here. User. Import User. Okay.
Import User. Okay. And for destroy, let me just get rid of this. Or for unfollow, let's copy this. Let's comment this out. And again, we need a User in the params. UserFollowController destroy method. Again, we need the User here. Let's paste this in.
Again, we need the User here. Let's paste this in. Let's re-indent. And hopefully I did that correctly. Okay. Let's get rid of this. Now we also have this one here, which checks if the User is following another User. And this is a POST request as well. And this functionality doesn't really follow CRUD actions.
And this is a POST request as well. And this functionality doesn't really follow CRUD actions. So I'm okay with just adding a new method here. So let's duplicate this one. Let's grab the URL here. Okay. We'll use the same controller, but we'll just make a custom method here. And we'll name it isFollowing. And the name will be user.isFollowing.
And we'll name it isFollowing. And the name will be user.isFollowing. Okay. Let's grab this. Again, it takes in a user as a param. Let's comment that out. Let's just delete it. Save that. Go into our UserFollowController. Let's add that method.
Go into our UserFollowController. Let's add that method, isFollowing. Takes in a User. And we can paste in that code. Okay. Hopefully I did everything correctly. Let me just move up this group. I want the auth stuff to be last. So I'm gonna move this.
This is just a simple explanation with no code.
So let's go to their profile. And this should be unfollow. So there's something wrong. And you can see this error here. GET method is not supported. And that's because this should be a GET request. Okay. So one more time, let me just reload this. Again, everyone in this list, I should be following. So if I go to their profile,
Okay. So that seems like it worked. So if we go back and go to our list of tweets, we should see a tweet here by person 80. And there it is. And now let's try unfollowing. Let's unfollow this person. Okay. That did not work. And I spelled destroy wrong.
And if I go back to this list here, if I refresh, person 80's tweet is no longer there. So yeah, that was a successful refactor. Again, it would have been faster if I had tests. But now if you take a look at our routes file, we've extracted everything to resourceful controllers. So here's everything that has to do with tweets. Here's everything that has to do with following users. Here's the user profile stuff, which is public. And this has to do with auth.
Here's the user profile stuff, which is public. And this has to do with auth. So let's go ahead and make a commit. There's only changes to the backend. So we can say git add, git commit, refactor routes, use controllers.
