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:
Route::get('path', function()
{
// closure logic here
});
We’ll do something like this instead:
Route::get('agents', 'AgentsController@index');
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
Ok, let’s navigate to our Controllers directory and see what has been created for us:
<?php
class AgentsController extends BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
//
}
}
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:
class AgentsController extends BaseController {
// Controller logic
}
Just for fun, let’s add some agents to our index method like so:
public function index()
{
return 'Agents Brown, Smith, and Jones';
}
Now upon visiting http://you.rock/agents
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:
Route::get('agents/{name}', 'AgentsController@show');
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:
public function show($name)
{
return 'Agent '.$name.': Mister Anderson, you look surprised to see me, again.';
}
We can now visit http://you.rock/agents/smith
and be greeted with the ever friendly Agent Smith’s message.