Laravel Classroom image

Yasar  Khan / Professional / Web Technology
 
To post your Question Join Classroom
 
Lesson Topics's No:-  |1 | 2 | 3 | 4|Last
Lessons:-Routes With Closures

Routes With Closures
In this chapter we will be using Routes with Closures instead of Controllers with Actions. If
you have not yet read the previous chapter on the topic of using controllers then I would suggest
starting there since we will be building on what we have already learned in this chapter.
Routes allow us to map our framework URLs to closures, which is a very clean way of containing
our logic without all of the ‘class fluff’. Closures are anonymous functions (function() {}), they
can be assigned to and treated like any other variable. For more information on Closures, check
out the PHP API article¹.
4.1 Closures
Lets have a look at a route that routes to a closure.
1 <?php
2
3 // application/routes.php
4 Route::get('/', function()
5 {
6 return View::make('home.index');
7 });
In this example we are responding to requests to the root of the web application that use the
HTTP verb GET with a closure that simply returns a view object. The output is the default
welcome page.
Please note that you only need the root slash for the root page, all other routes omit it, for
example..
1 <?php
2
3 // application/routes.php
4 Route::get('account/profile', function()
5 {
6 return View::make('account.profile');
7 });
Routes are RESTful by nature, but you can use Route::any() to respond to any HTTP verb. Here
are your options:1 <?php
2
3 // application/routes.php
4 Route::get();
5 Route::post();
6 Route::put();
7 Route::delete();
8 Route::any();
To pass parameters to your closures simply add the usual view place-holders to the URI, and
define parameters in your closure. They will be matched in the order from left to right, for
example..
1 <?php
2
3 // application/routes.php
4 Route::get('user/(:any)/task/(:num)', function($username, $task_number)
5 {
6 // $username will be replaced by the value of (:any)
7 // $task_number will be replaced by the integer in place of (:num)
8
9 $data = array(
10 'username' => $username,
11 'task' => $task_number
12 );
13
14 return View::make('tasks.for_user', $data);
15 });
 

 
 
 

Yasar  Khan

Skills    Laravel

Qualifications :-
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:- Hi i am yasar working as web developer since last 3 years.
Explore
 

  Students (0)