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 meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
}
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
alert(nr_divs);
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Routing

Last accessed pages

  1. Get Lower, Higher, and Closest Number (5342)
  2. Simple Admin Login PHP Script (10894)
  3. jQuery parent, children and nth-child() (13826)
  4. Display UL bullets and OL numbers on the right side (8088)
  5. Convert BBCode to HTML and HTML to BBCode with JavaScript (9298)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (201)
  2. Read Excel file data in PHP - PhpExcelReader (66)
  3. The Mastery of Love (56)
  4. PHP Unzipper - Extract Zip, Rar Archives (51)
  5. Working with MySQL Database (34)
Chat
Chat or leave a message for the other users
Full screenInchide