http://localhost/laravel4.2/foldername/public/------------------------
Sample 1) simple route
Route::get('/', function(){If http://localhost/laravel4.2/foldername/public/ (root page), the root page display text “All dogs only”
return "All dogs";
});
------------------------
Sample 2) with route parameters
Route::get('cats/{id}', function($id){If visiting "http://localhost/laravel4.2/foldername/public/cats/123", text “Cat#123” shown
return "Cat #$id";
});
------------------------
Sample 3) route with route parameters and condition
Route::get('cats/{id}', function($id){If visiting http://localhost/laravel4.2/foldername/public/cats/123, shown text “Cat#123” :
return "Cat #$id";
})->where('id','[0-9]+');
However, if you visit the url where parameters aren’t in number: (for e.g: http://localhost/laravel4.2/foldername/public/cats/abc) , would display 404 errors page:
------------------------
Sample 4) redirect with route
Route::get('/', function(){If visiting the page http://localhost/laravel4.2/foldername/public/dogs, it would redirect you to http://localhost/laravel4.2/foldername/public/
return "All dogs";
});
Route::get('/dogs', function(){
return Redirect::to('/');
});
------------------------
Sample 5) Returning views
Route::get('welcome', function(){This sample is for returning view but not the text only, in this sample used the default hello view (at app\views\hello.php) provided by Laravel framework.
return View::make('hello');
});
if you entering the "http://localhost/laravel4.2/foldername/public/welcome", the screen shows this page:
------------------------
Sample 6) Returning views with data
Create a php file named “about.php” at “/app/views”
directory and add these code:
<h2>About this site</h2>There are <?php echo $number_of_dogs; ?> dogs on this site.
And then add code at route :
Route::get('about', function(){return View::make('about')->with('number_of_dogs', 500);});
When visit the uri : http://localhost/laravel4.2/foldername/public/about,
you would see content of the view “about.php” and also the number_of_dogs
valuable defined at route.php
------------------------
Sample 7) Returning views with data 2 (data from url)
This is an example returning a view and shows numbers from url params:
Route::get('about_params/{num_of_dogs}', function($num_of_dogs){
return View::make('about')->with('number_of_dogs', $num_of_dogs);
})->where('id','[0-9]+');
================== The routing code ====================
Reference:
Getting start with Laravel4 p.33-p36
No comments :
Post a Comment