Laravel routes( routes.php)

IMG_4620

In previous post we understand the directory structure of laravel application, so we have some basic idea about where different application components reside. Before we go deeper and start architectural components like model, view and controller, lets understand routes.php. As routes.php file gives you a hint for each request which controller and which method inside that controller will execute.

Laravel routes

Below are few routes of one app,
To handle above request we need to add below lines in routes.php
Route::get(‘signin’,array(‘as’=>’signin’,’uses’=>’AuthCtrl@getSignin’));
 
Route::get(‘about’,array(‘as’=>’about’,’uses’=>’AuthCtrl@getAbout));
 
NOTE: it is assumed that AuthCtrl.php exist inside app/controllers directory and it have AuthCtrl class which extends Controller class. Also this AuthCtrl class have two methods
getSignin()
getAbout()
available to server above requests mentioned in routes.php
Now lets understand routes.php entry briefly.
Request Method:
As you can see after Route:: there is a get word, which states request type for this route which can be either get or post.
 
Route name:
After that you can see ‘signin‘ word which state route name which can be use in anchor tag as below.
<a href=’signin’> sign in</a>
Alias: after that array is passed which have two attributes as and uses where as mentioned alias. It’s usage is same as route name but you can add diferent name here. It can be called with URL facade available in laravel framework as below.
<a href='<?pho URL::route(‘sign’);?’> signin </a>
Usibg above code you are always getting below url in above anchor tag,
Irraepective of you are where in whole web application
Let say if you are http://www.mysite.com/blog/mypost and simply write
<a href=’signin’> sign in</a>
 Than it will show 404 as it will call make below request ,
Which but if you use URL face as mentioned above you will always get absolute url. So alias will be helpful here.
Uses:
 This is important parameter which indicates controller name along with it’s method for given route name. So you have complete idea from the route that for given request which controller and insise that controller which method will be called.
For example route which we created above have AuthCtrl as controller and getSignin as it’s method. How to give method name is a question for you intially. But it is really a simple and not a big deal as you think. For that you have to simply follow two rules.
1. If route is handlibg HTTP GET request than start your method name with get.
Exa. getSignin
2. If your route is handling post request than start your method name with post.
Exa. postSignin
So that us about it for laravel route.php . If you have any doubt, query and concern, please post a comment or email me.
Thanks,
-Ahesanali
Advertisements

One thought on “Laravel routes( routes.php)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s