Introducing Laravel, part 2

In the first part of this introductory mini series we looked at simple routes and views and now we’ll look at how to work with controllers and models , how these two fit in the framework and how to use them.

Controllers

In the first part of this article I showed you how to link routes to views, but as you might have noticed we still haven’t added any logic and that is where controllers come into play.

Controllers contain public methods that are known as actions and a simple controller that calls a view looks something like this:

class UserController extends BaseController{
public function showProfile() {
return View::make('profile');
}
}

This is as simple as we can get with controllers; we create a UserController class that will contain all the functions that are needed in our application when it comes to users. Inside it we create a function called showProfile that returns the profile view.

This is complete, but in order for it to redirect the user to the right place, we need to link it to the routes. So we’ll use controller routing and to link that controller we just made to a route we need :

Route::get('profile', 'UserController@showProfile');

To link a URI to a controller we still used the Route::get() method and the first parameter still works the same way , it’s with the second that things vary a little.

In the previous article we had a function there, but since our logic is in the controller, that function is no longer needed; we just need to point to the action we want. So, for the second parameter we add the class name and an @ sign and lastly the name of the action we wish to route.

Eloquent ORM

The Eloquent ORM is a simple and beautiful ActiveRecord implementation that helps you work with your database. For each database we use we should have a corresponding Model to interact with that table.

class Post extends Eloquent {}

One thing we usually do not need to do is tell Eloquent which table to use, Eloquent will take the name of the class we use, make it lowercase and plural and that will be used as the table name (unless another name is specified). In this class Eloquent will assume I am using the posts table because my class name is Post.

If I want to use a different table I can specify it as follows:

class Post extends Eloquent {
protected $table = 'articles';
}

Eloquent will also assume that each table you create has a primary column named id. Once this is created you are ready to start retrieving and creating records in your table. You will also need updated_at and created_at columns because this the default approach taken by Laravel. (To avoid this you’ll need to set the $timestamps property on your model to false.)

Final Thoughts

This completes our quick introduction to Laravel, this introduction was just a little taste to get you warmed up, and exited about learning this amazing framework.

I hope these two articles showed you the benefits in using Laravel and how great your code ends up looking when you use it.