Laravel 5.7 — Controllers

Mohammad Javed
Dec 23, 2018 · 4 min read
Image for post
Image for post

Here is a perfect breakdown on what a Laravel Controller is taken from the official Laravel documentation;

Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored in the app/Http/Controllers directory.

In our routes file, we may want to add more logic to a view, this could get tiresome with loads of code showing. We want to simplify how we work, Laravel allows us to do that, we can use a dedicated controller to add more logic to our routes/views.

If you take a look at the routes file in your editor, each page is static, so what we can do is create a Pages Controller — we can simplify the code more by using the controller we are about to create.

Image for post
Image for post

So let’s start with the homepage route, we need to create a new line in our routes file and define the Pages Controller followed by a method/action. If your getting confused now, hang in there, this will become clearer to you shortly.

Route::get(‘/’, ‘PagesController@home’);

Comment out the code for the homepage route that has the array of tasks, reload the homepage and you should see the following error;

Image for post
Image for post

Oops, we’ve not created the “PagesController” — Laravel is looking for this and can’t find it, this is easy to fix. Head over to your terminal and run the following command to create the controller.

$ php artisan make:controller PagesController

What we must not do is create the controller manually, Laravel offers the boilerplate to you, it whips up the necessary code when you create the controller through the command line. Let’s get into the habit of using the terminal and running the necessary commands, the more you use the commands, the more it’ll sit in your brain for future reference.

You will find the controller inside app/http/controllers/ — this is where the controllers live in Laravel. This is what the Pages Controller looks like once it has been created;

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class PagesController extends Controller {

You can see why it was much simpler to run the command to create the controller. We wouldn't want to be importing the namespace etc manually in case we need to.

Let’s create a method for home, otherwise, the PagesController will not work.

public function home() {}

We now need to migrate the data from the old route we initially had into the method we have just created. Delete the old route for the homepage as we won’t be needing that moving forward. This is what the end result should look once you’ve migrated over the data to the method.

Image for post
Image for post

We can now migrate the rest of the routes over to the Pages Controller. We need to copy the same line where we called the home method and name the methods as the page name. This is to keep some consistency and quickly identify each method. Our routes file should now look something like this;

Route::get(‘/’, ‘PagesController@home’);;Route::get(‘/about’, ‘PagesController@about’);;Route::get(‘/contact’, ‘PagesController@contact’);

And here we have the Pages Controller with two additional methods added that return the view. If you’ve followed the steps correctly, then the homepage and the other pages should be working just like they did without the Pages Controller created.

Image for post
Image for post

So for any large projects that you may be working on, this is the ideal way to go about things, you may come across existing projects that follow this structure. This keeps things nicely separated.

That’s it, you now should understand how to use dedicated controllers in Laravel. In the next article, we will be looking at “Databases and Migrations”.

I hope you’ve enjoyed this article, give it a clap and share. If you have any comments, feel free to use the comments section or get in touch with me on my Twitter. Don’t forget to have a look at my previous articles;

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store