- Route Parameters
- Naming Routes

The essential function of any web application framework is taking requests from user and delivering responses, usually via HTTP(S).
Basic routing is meant to route your request to an appropriate controller.
The routes of the web application in Laravel 5.5 can be defined in "routes/web.php" file. If you open this file with a text editor, you'll see a code like this:
Route::get('/', function(){
  return view('welcome');
});
Here 'Route' is a class that has a static method get() that returns a view() method which presents a web page.
When a user visits the home page, it is displayed the content of the 'welcome.blade.php' (stored in the views folder).
You can send another response instead of the default route. For example:
Route::get('/', function(){
  return 'welcome';
});
It will simply return the word "welcome" on your home page.
If you want to modify the home-page, just edit the 'welcome.blade.php' file in the "resources/views/" folder.

Display content according to the URL address

Using Route::get() method, you can display page content according to the URL address.
Lets create another route for a "test" page.
- Add this code in "routes/web.php":
//home-page
Route::get('/', function(){
  return view('welcome');
});

//test page
Route::get('/test', function(){
  return view('test');
});
- In the "resources/views/" folder create a "test.blade.php" file, with this content:
<!doctype html>
<html lang="{{app()->getLocale()}}">
<head>
<meta charset="utf-8">
<title>Test page</title>
</head>
<body>
<h1>Something about nothing</h1>
</body>
</html>
Open the Command Line Interface (CMD on Windows), navigate to the folder where you have Laravel project, and run this command to start the php built-in server:
php artisan serve
Now, if you acess the "test" page in your browser, for example:
//localhost:8000/test/
It will display a page like in this image:
Laravel route test page
Using a framework, you not have to create a view file for each page; this is just an example to see how routes work.

Route Parameters

If the route you’re defining has parameters, it’s simple to add them into both the URI definition and the callback.
- Example: Capture and display the ID of "page" added in URL.
Route::get('page-{id}', function($id){
  return 'Page id: '. $id;
});
If you add the above code in the "routes/web.php", whatever argument that we pass after the "public/page-" in URL, it will be stored in $id and we can use that parameter for further processing. Here we are simply displaying it.

You can also make your route parameters optional, by adding the "?" sign after the name of the parameters:
Route::get('page{id?}', function($id ='noid'){
  return 'Page id: '. $id;
});
And you can use regular expressions to define that a route should only match if a parameter meets particular requirements; by using where() method on route instance.
Route::get('page-{name}', function($name){
  return 'Page name: '. $name;
})->where('name', '[A-z-]+');
Also, you can define multiple parameters for route:
Route::get('/{name}/{id?}', function($name, $id=1){
  return 'Page name: '. $name .', id: '. $id;
})->where(['name'=>'[A-z-]+', 'id'=>'[0-9]+']);

Naming Routes

Laravel allows you to name each route, which enables you to refer to it without explicitly referencing the URL. This is helpful because you can give simple nicknames to complex routes, create links by using simple the route-name, and also linking them by name means you don’t have to re-write page links if the URIs change.
- You can define the name of the route by using the name() method in the route instance.
Route::name(name_id)->get('/{name}/{id?}', function($name, $id=1){
  return 'Page name: '. $name .', id: '. $id;
})->where(['name'=>'[A-z-]+', 'id'=>'[0-9]+']);
To create URL for this route (named 'name_id'), use the route() function, passing an array with the required parameters.
For example, in a view file you can add this code to create two links for the "name_id" route:
<?php
$url1 = route('name_id', ['name'=>'test']); //here id is not specified because it is optional
$url2 = route('name_id', ['name'=>'tutorial', 'id'=>2]);
?>
<a href='{{$url1}}' title='test'>Test</a>
<a href='{{$url2}}' title='tutorial'>Tutorial</a>
- Will create two links like this:
<a href='http://localhost:8000/test/' title='test'>Test</a>
<a href='http://localhost:8000/tutorial/2' title='tutorial'>Tutorial</a>
• To make relative URL, add "false" as a third argument in route() function:
route('route_name', array(), false)
- Will create a link like this:
<a href='/tutorial/2' title='tutorial'>Tutorial</a>
Similarly, for redirecting to a named route, the proper syntax would be:
return redirect()->route('name_id', ['name'=>'test']);
• The get() method is used for HTTP(S) GET requests, but you can define routes for each of the possible request, like a form POST, or maybe some JavaScript sending PUT or DELETE requests.

Laravel' Route Verbs

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

//for multiple HTTP verbs
Route::match(['get', 'post'], '/', function(){
  //
});

//route that responds to all HTTP verbs
Route::any('foo', function(){
  //
});
- Documentation: Laravel - Routing

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
}
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
alert(url);
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("http://coursesweb.net/");
echo $homepage;
Routing

Last accessed pages

  1. Puzzle Games (12236)
  2. Qwop (59127)
  3. Farms of Dreams (1815)
  4. Chicken Run (211)
  5. Drag Racer V3 (26088)

Popular pages this month

  1. Qwop (4607)
  2. Drag Racer V3 (2233)
  3. Flash Games - Free online Games (1106)
  4. Bubbles3 (992)
  5. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (981)