Introduction To Laravel Controllers

Laravel Controllers

So far we have covered a fair number of concepts in the great Laravel Framework. One thing that has been missing so far is the use of controllers. Laravel is an MVC framework, isn’t it? Well, yes it is. But in fact, you could say the creators threw a bit of a curve ball. It seems Laravel can be used in the MVC style, but it is not mandatory. If you’re used to Controllers in Codeigniter, or something else like Zend, or CakePHP you’ll find the Laravel implementation to be a bit different. It is powerful and quite sleek, so let’s check it out now!

Why Use Controllers?

The reason we haven’t really covered controllers yet, is because you could actually create a whole application using nothing but the routes.php file. It would definitely start to get messy, but it could be done. In addition, the examples so far have been simple snippets that we could easily just show in a closure. Growth is good though, and in order for us to grow, we need to go beyond routing to closures and start jumping into the use of controllers.

Explicit Routing to Controllers

In beginning to use controllers, we’ll start with explicit routing to controllers. What this means is that in the routes.php file, instead of following this format:

We’ll do something like this instead:

So what we are saying here is that when we route to agents we’ll use the AgentsController. This follows the format of a Collection name followed by the word Controller. But wait, we don’t have an actual controller yet. Well that is easy to fix, let’s use Artisan to generate one for us:

php artisan controller:make AgentsController

Bingo: Controller created successfully!

Ok, let’s navigate to our Controllers directory and see what has been created for us:

When we create a controller, Laravel sets up a bunch of methods for us automatically. We have index, create, store, show, edit, update, and destroy. So we can visit http://you.rock/agents and even though we don’t see anything in the browser yet, we know the controller is working since no errors are thrown. Now Artisan did all this work for us, but if you are doing this manually, make sure that you follow the format of your controller extending the BaseController like so:

Just for fun, let’s add some agents to our index method like so:

Now upon visiting http://you.rock/agents

We get: Agents Brown, Smith, and Jones

Passing Parameters to Controllers

If you know how to pass parameters to closures, and you do since you have checked out the basic Laravel Routing Tutorial 🙂 then you also know how to pass parameters to controllers. The reason is because it works in the same way. Check it out:

Here, we add another route to our routes.php file. It says, when you hit the agents path, and pass in the agent name wildcard, run the AgentsController, and call the show method. By adding a quick snippet to our show method:

We can now visit http://you.rock/agents/smith and be greeted with the ever friendly Agent Smith’s message.

Agent smith: Mister Anderson, you look surprised to see me, again.