Quantcast
Viewing all articles
Browse latest Browse all 104

Laravel 4.2 Basic Routing

When I first started working with Laravel 4.2, routing baffled me a little. Being used to CodeIgniter routing where the controller/method concept is easy to follow, Laravel does things very differently. Here’s a quick overview.

Let’s start by saying forget controller-based routing for now. You can do it but it’s not really where the routing action is with Laravel. I’ll include a controller route at the end of this post.

The app/routes.php file is a major player in the structure of your application. And if you are a CodeIgniter like me, that’s going to mess you up for a while. It seems that Laravel is much more like Sinatra in the way that routing is handled.

We’ll start with a new Laravel app that will have some basic static pages. My forest instinct would be to create a pages controller with methods like: about, contact ands on. That would give routes like pages/about and pages/contact.

That’s not how it works in Laravel. Instead, you add routes in your app/routes.php like this:

Route::get('/', function()
{
    return View::make('home');
});

Laravel uses closures for routing which keeps things nice and tidy. The above route sends you through to the home page. A route like this:

Route::get('about', function()
{
	return View::make('about');
});

Will provide a url like: www.mydomain.com/about. The main learning point here is that both these routes are examples of how to route to static pages. You’ll need routes like these whenever you are linking to static pages. CodeIgniter users: notice that there are no controllers involved here.

Notice that the routes set up so far respond to the HTTP verb get. We can have routes for the other verbs too (post, put, delete etc.).

Next, we’ll deal with the concept of a named route. These can be useful because it helps to identify specific parts of your app. Named routes are also handy because even if you change the location of the route, it will still work. Or it will, if you don’t change the name.

A named route looks like this:

Route::get('writing', array('as' => 'writing', function()
{
    return View::make('writing');
}));

Now we can look at a controller route. These do work a little more like a CodeIgniter route. That’s because you add the route like this:

Route::get('info', 'HomeController@info');

The way this works is: pointing your browser to: http://www.mysite.com/info will call an action named info in the HomeController.

It is also possible to create RESTful controllers and associated routes. I’ve not tried these out yet, but you can read about them in the official docs.

Finally…

It’s worth saying again that your routes.php file is the place where you get a real snapshot of your whole app. I’m still not sure that I like the idea that even models can be bound to a route. However, there are certainly a lot of powerful options in Laravel routing.


Viewing all articles
Browse latest Browse all 104

Trending Articles