Laravel Course

- 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 tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Routing

Last accessed pages

  1. Common PHP Errors and Solutions (9771)
  2. PHP getElementById and getElementsByTagName (49471)
  3. Mysql SELECT JOIN tables on two different Databases (4498)
  4. jQuery UI draggable - Drag elements (11448)
  5. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)