Learn about the Controllers in Laravel

0
1570
Laravel-Controller

Laravel-Controller

Following our previous article of setting up Laravel and working with the view, today we will be looking at the C in MVC: Controllers. Let’s take a look first at exactly what the controller’s job is in the hierarchy of an application. Then, we will get some practice in setting up a controller, and how we can pass information from a controller to our view.

The Controller
We already saw the view, and we can see that it’s responsible for what the user actually sees and interacts with. The Controller’s job is to pass information between the view and the model. The model itself is responsible for the business logic, things like accessing and returning results from a database. This is the basic idea of how an MVC application or framework would work.  Below, see a quick look at what this interaction might look like.

  1. User requests a category page – /stuff
  2. Route picks up /stuff and passes it to the Stuff Controller
  3. Controller initiates a stuff object which queries the database for the items in the category
  4. Stuff model object passes the info found back to controller to process
  5. Controller passes needed data to view
  6. View displays data to user and waits for new input

This is a pretty basic example of how a MVC application might work and what the controller’s purpose is in it. Let’s go ahead and set up a new controller now. Laravel Course provides some handy command line tools for doing this. You could always copy and paste the existing controller and rename it, but this way is much easier. Navigate to your Laravel project folder in your command line, then run the following command.

Php artisan make:controller TestController

You should now see a TestController.php file in your app/Http/controllers folder. It should look similar to this:

Don’t worry about the namespace and “use” commands just yet, we’ll go over these when we look at models and when we integrate other parts of Laravel. The important thing to see is that our controller extends Laravel’s built in controller class. This allows us to use the basic functionality it provides; you’ll see this type of inheritance used throughout the entirety of the framework.

Right now the controller doesn’t do much, there’s no actual code to fire in it. Let’s fix that. Going back to our example above, why don’t we create a simple function to display a stuff page? To start we’ll create a public function that simply returns a view, much like the function passed in routes.php file in the earlier tutorial.

Now we have that go ahead and make a stuff.php file and stick that into your views folder. If you need a refresher on how to do that, check out the getting started with Laravel tutorial, where we go over making views.

At this point, we now need a way to tell our route to fire the showStuff() function. If you navigate to the /show route now, you’ll get a route not found error, which makes sense since we never created one to handle it.

In our last example we passed a function to the route and were able to return the view through that. This time however, we’ll be passing the controller to the route to let it know what we want to use. The syntax for that is

So ours would be:

We pass this just like we did the function. Open up your routes.php file and add the following:

It really is that simple. This simply tells the route to fire the specific function in the controller. Easy!

This is great and all, but wouldn’t it be nice if we could pass some data back to the view, it is an important part of an application after all. Add to your controller:

Learn PHP Fundamentals From Scratch

We’ve now got a new value in our controller, but no way to access it. If we try to access the $value variable in the stuff.php file, you’ll get an undefined variable error. This makes sense since the value will fall out of scope once we access the function. To fix this we only need to add one little extra bit of code:

Now, we are telling the view to be returned but also to pass with it a variable named value with $value assigned to it. We could make a change like:

Go back to your view and now try to access a variable $stuffVariable. You can name it whatever you like and it’s passed to the view. You can also do multiple withs if needed. That would look something like this:

Now on the page you’ll have two different variables, $stuffVariable1 and $stuffVariable2, both with the same value. Not very interesting in this situation, but you can see how easy it would be to return the values of several database queries for interest.

There’s a lot more you can do with controllers, but this is more than enough to get you started. Practice setting up some new routes with controllers and experimenting with how they work. This is the basic building blocks of an MVC application and we’re well on our way to getting started building our first one. Next we’ll finish up with Models, and then finally begin working on our simple to do list application using Laravel. Stay tuned!

LEAVE A REPLY

Please enter your comment!
Please enter your name here