Move Route to Controller0:00
In this video, I'd like to show you a few tips on how you can simplify your controllers. To demonstrate this, we're going to use a vanilla Laravel application. Let's head over to phpStorm and take a look at the routes file. Now in a vanilla Laravel application, we simply have this welcome view, and I'm going to move that to a controller. So let's create a controller now, php artisan make:controller WelcomeController, and I'm going to move this logic to that WelcomeController. So I'm going to replace the callable by WelcomeController, I'm going to go to WelcomeController, I'm going to add a function here, I'm not going to give it a proper name, but instead use the __invoke function, and I'm going to return the rendered view here.
Remove BaseController Extension0:43
going to add a function here, I'm not going to give it a proper name, but instead use the __invoke function, and I'm going to return the rendered view here. Let's save and check in the browser if it still works, and sure enough, it does. Now there's already something bothering me in this controller, and that is the fact that this controller extends a BaseController. For the longest time, I have thought that extending from the BaseController was required. But if I remove this, and head to the browser and refresh, we can see that it still works. And to me, using a controller that doesn't extend another one feels much lighter. It signals to me that there is nothing magical going on, it is just a regular class. Now let's take a look at what actually is in the BaseController.
It signals to me that there is nothing magical going on, it is just a regular clause. Now let's take a look at what actually is in the BaseController. We see that there are three traits being used here, authorizesRequests, dispatchesJobs, and validatesRequests. Of course you can still use these traits. If you want to authorize a certain request, you can still apply the authorizesRequests trait to your controller, and just authorize like you're used to. Let's remove this for now. Now let's rename the controller and see what happens. I'm going to rename it to WelcomeLaracastsController, and sure enough, the class has been renamed.
Renaming Breaks Route String2:00
Now let's rename the controller and see what happens. I'm going to rename it to WelcomeLaracastsController, and sure enough, the class has been renamed and the file has been renamed. But if I go to the browser and try to refresh, the application blows up. Even though I renamed the controller, phpStorm didn't modify this string in the routes file. Because to phpStorm, WelcomeController here is just a string with no connection to the name of the WelcomeController at all. If WelcomeController here is just a string, how does Laravel know which controller to call? The answer for that lies in the routesServiceProvider.
Remove Default Namespace2:38
call? The answer for that lies in the RouteServiceProvider. In the RouteServiceProvider, there is a default namespace being set up, App\Http\Controllers. And Laravel uses that as a prefix to WelcomeController, and it has a fully qualified class name then. So it will look in App\Http\Controllers and get the controller. Now my opinion is that controllers are better off without a default namespace. So if we remove this, and we also remove it in mapWebRoutes and mapApiRoutes. And with that out of the way, we can use the fully qualified class name of the controller here. phpStorm will even offer auto-completion.
here. phpStorm will even offer auto-completion. I'm going to use that. Let's also import this. And with that out of the way, our application should work again. A nice benefit that you get from using the fully qualified class name here is that you can now click through to the actual controller. And of course, I can rename it to whatever I want now. I'm going to rename it back to WelcomeController. phpStorm will have updated the name here as well.
Redirect Using Controller Action3:42
I'm going to rename it back to WelcomeController. phpStorm will have updated the name here as well. And the application still works. Another place where you can use fully qualified controller class names is when redirecting. Let me demonstrate this by creating a new controller. I'm going to call it AnotherController. And I'm going to add a save method on here. Let's pretend that we just saved something. And we want to redirect back to the WelcomeController. We're not going to use a route name here.
And we want to redirect back to the WelcomeController. We're not going to use a route name here. Instead, we are going to use an action. And in this action, I can use the fully qualified namespace. So I can just use WelcomeController and be done with it. And if I go to WelcomeController and change the name, let's rename it back to WelcomeLaraCastsController. Like this. Then phpStorm will have changed it in the action as well. I think not using a default namespace for controllers brings a lot of benefits to the table.
