KodeInfo | Learning resources for web and mobile development

UNDERSTANDING LARAVEL ROUTES

September 3rd, 2014 12:11:49 by Imran Iqbal Comments(0) - Views(4804)

Today we will see how we can use routes file(app/routes.php) in Laravel to define our application routes . If you are beginner i strongly recommend you to go through  Getting Stated with Laravel tutorials . For Instance if your application need to respond to GET URI and send back JSON response in laravel below line will do this .

Route::get('/update',function(){
 
return Response::json(array('result'=>'0','message'=>'Students updated successfully'));
 
});

In the above code we see get method is taking two parameters first is the URI to which application should respond and second is closure callback which will be executed if the URI matches . Some of the other methods available in route class are

Route::get();
Route::post();
Route::put();
Route::delete();
Route::any();

Laravel searches routes from start to end If any route’s URI is matched then that will be executed all other routes are skipped . Lets see one more example

Route::get('/cakephp',function(){
 
return "CakePHP";
 
});
 
Route::get('/codeigniter',function(){
 
return "CodeIgniter";
 
});
 
Route::get('/laravel',function(){
 
return "Laravel is Beautiful";
 
});

Now If you navigate to /cakephp then CakePhp will be printed for /codeigniter CodeIgniter will be printed and for /laravel it will print Laravel is Beautiful . Thats so easy . Next lets see how to post data from HTML Form . First create a HTML Form using open method of Form class and pass our url to which data will be posted and set method as POST . We have also put two input fields so we can access it in our route

index.blade.php

<html>
<head></head>
<body>
{{Form::open(array('url' => '/testing', 'method' => 'post'))}}
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="submit" name="submit" value="Login"/>
{{Form::close()}}
</body>
</html>

routes.php

Route::get('/testing', function () {
return View::make('index');
});
 
Route::post('/testing', function () {
dd(Input::all());
});

Now go to /testing and type something in username , password fields and press Login you will see var_dump of Input data It will contain username,password and an extra field known as _token which will safeguard our request from CSRF

We can also match multiple verbs for single route using match method of Route class

Route::match(array('GET', 'POST'), '/', function()
{
    return 'Hello World';
});

ROUTE PARAMETERS

we can also send parameters to routes like /foo/1 here “1″ is the extra parameters so our route will be /foo/{id} in routes file like below

Route::get('/foo/{id}', function($id)
{
    return 'Foo ID is '.$id;
});

ID value will be passed to closure as a parameter so you can access it . you can also pass value to controller and controller method should have parameters to get the values like below

Route::get('/foo/{id}', 'FooController@index');
 
public function index($id){
  return 'Foo ID is '.$id;
}

If you want to make id as optional parameter then make like this {id?} question mark appended to id . We can also put constraints on id to be numeric like below code

Route::get('/foo/{id}', 'FooController@index')->where('id','[0-9]+');
 
public function index($id){
  return 'Foo ID is '.$id;
}

We can also define a global pattern for parameters

Route::pattern('id', '[0-9]+');

For now this is enough there are many complex stuff like Named Routes , Route Filters , Route Groups etc which will be covered in separate tutorial .

Thanks

KodeInfo

Author

  • Imran Iqbal
    Imran Iqbal

    Imran is a web developer and consultant from India. He is the founder of KodeInfo, the PHP and Laravel Community . In the meantime he follows other projects, works as a freelance backend consultant for PHP applications and studies IT Engineering . He loves to learn new things, not only about PHP or development but everything.

Related

WHY USE A FRAMEWORK OVER PLAIN PHP

WHY USE A FRAMEWORK OVER PLAIN PHP
read more

GETTING STARTED WITH LARAVEL

GETTING STARTED WITH LARAVEL
read more

UNDERSTANDING LARAVEL STRUCTURE

UNDERSTANDING LARAVEL STRUCTURE
read more

LARAVEL AUTHENTICATION PACKAGES

LARAVEL AUTHENTICATION PACKAGES
read more

comments powered by Disqus