Scroll to top

UNDERSTANDING LARAVEL ROUTES

Today we will see how we can use routes file(app/routes.php) in Laravel to define our application routes. If you are a beginner I strongly recommend you to go through Getting Started with Laravel tutorials. For Instance, if your application needs to respond to GET URI and send back JSON response in laravel below the 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. Let’s 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. That’s so easy. Next, let’s see how to post data from HTML Form. First, create an 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 the username, password fields and press Login you will see var_dump of Input data It will contain the username, password and an extra field known as _token which will safeguard our request from CSRF

We can also match multiple verbs for a 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 an 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 is much complex stuff like Named Routes, Route Filters, Route Groups etc which will be covered in the separate tutorial.

Thanks
KodeInfo

Author avatar

admin

http://www.kodeinfo.com
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.

Post a Comment

Your email address will not be published. Required fields are marked *