laravel4 Routing to Controller Classes

So far we have completed three blog posts to the development of thisdayinbangladesh.com laravel4 live project. First one was about creating virtual host for local development environment, second one was about installing basic laravel4 & the third one was about migrations & seeding database.
In this lesson we will cover the routing mechanisms of laravel4 that will be/was necessary for our mentioned project.
Most of the routes for our application will be defined in the “app/routes.php” file. The simplest Laravel routes consist of a URI and a Closure callback. Just like,

means if you hit the root URL of your application then it will return the ‘Hello World’ & no task any more.
Laravel allows you to not only route to Closures, but also to controller classes, and even allows the creation of resource controllers. For our application we will also register several routes to/for our various controller classes like a basic controller, a RESTful controller & also to a Resource controller.
The following code block is all the routes we will/have used for our app,


Lets try to understand step by step:
In the above code block, there are mainly 5 routes that have been registered to several types of controllers.
1. First one is a basic GET Route. When users request the root URL of our application then this Route acts and as you can see we are making a redirection to another type of URL inside it’s Route logic.

2. The redirection tries to find such an URL structure which has been already registered by our second Route. We attached the second Route to our RESTful controller called “FactController” that resides in “app/controllers” directory.

Laravel allows you to easily define a single route to handle every action in a controller using simple, REST naming conventions.

The Route::controller method accepts two arguments. The first is the base URI the controller handles, while the second is the class name of the controller. Next, just add methods to your controller, prefixed with the HTTP verb they respond to. We will also add methods in such a convention in our “FactController” later (If you want to check now, then head toward the github repository that has the full code already) like “getIndex()”, “postFact()” etc.

3. The third registered Route is another kind of Basic Route which is simply attached to a controller named “StaticContentController”. This route informs the framework that requests to the “/about” route should call the “about” method on the StaticContentController class.

4. In fourth Routing we have used Route Group. Sometimes you may need to apply filters to a group of routes. Instead of specifying the filter on each route, you may use a route group.
In our case we have added/defined a prefix named “admin” to the “/users” route and registered a Route inside this group. So, if any further request come to this application like “admin/users …” then the RESTful controller named “UserController” inside “app/controllers/admin” directory will act and it has also some RESTful methods on it to do corresponding tasks.
We have registered this Route in a sense that we may further have an admin panel from where we can manage our application frontend users.

5. Fifth Route is also within a Route group. You already know why we used Route Group. But in this group we attached a Resource Controller to the “api/v1/fact” route (as it is also prefixed such a way). We used two filters in this Route Group. One for prefixing the text “api/v1” before our “/fact” route & other for checking an authentication (which might be done later).

Resource controllers make it easier to build RESTful controllers around resources.

This single route declaration creates multiple routes to handle a variety of RESTful actions on the “fact” resource that resides in “app/controllers/api” directory. When you will generate/make a Resource Controller (will learn later), then the controller will already have stubbed methods for each of these actions with notes informing you which URIs and verbs they handle. Actions Handled By Resource Controller are: four types of GET, one type of POST, PUT/PATCH & DELETE.
We will mainly use these kinds of controllers to provide JSON data to any other API consumer application from our application in future.

So, this is all about Routings needed for our application. Next lesson will be about making our Models, Controllers and Views! Stay tuned by subscribing this blog via email.

4 thoughts on “laravel4 Routing to Controller Classes”

Comments are closed.