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 HTML5 tag is indicated to be used as container for menu with navigation links in Web site?
<section> <nav> <article>
<nav><ul>
 <li><a href="http://coursesweb.net/css/" title="CSS Course">CSS Course</a></li>
 <li><a href="http://www.marplo.net/jocuri/" title="Flash Games">Flash Games</a></li>
</ul></nav>
Which CSS property shifts an item horizontally to the left or right of where it was?
text-align clear float
.some_class {
  width: 30%;
  float: left;
}
Click on the Math object method which returns x, rounded downwards to the nearest integer.
Math.ceil(x) Math.abs(x) Math.floor(x)
var num = 12.34567;
num = Math.floor(num);
alert(num);       // 12
Indicate the PHP function which returns the number of characters in string.
mb_strlen() count() stristr()
$str = "string with utf-8 chars åèö";
$nrchr = mb_strlen($str);
echo $nrchr;        // 30
Routing

Last accessed pages

  1. $_GET, $_POST and $_REQUEST Variables (33703)
  2. Other Free Courses (2612)
  3. SHA256 Encrypt hash in JavaScript (31190)
  4. Prevent Hotlinking / Block External Access to Video and Audio files (4314)
  5. jqPlot Charts (6204)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (194)
  2. Read Excel file data in PHP - PhpExcelReader (72)
  3. The Four Agreements (64)
  4. PHP Unzipper - Extract Zip, Rar Archives (63)
  5. Get and Modify content of an Iframe (49)