Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Laravel Route

Introduction to Laravel Route

The Laravel Route group allows the developer to route all the groups. This helps in creating clean codes and whoever takes over the development of the website will be able to follow the exact routine. The other benefits one gets with grouping the routes is that all the attributes can be shared with all the groups. This saves a lot of time. In any other framework, this might have to be done individually. However, with Laravel Framework, this feature appears as a force multiplier. Route grouping also saves duplication which otherwise would have been the case if done individually. Attributes like middleware and namespaces are allowed to be shared in a group without doing it individually.

Syntax

The query looks like this:

Route::group( [ ] , callback);

Explanation: The Laravel framework is one of the most sought after frameworks for this very reason. It is expressive and its library allows the developer to choose from a large number of queries. This assists in creating robust functionalities. The laravel framework is also an immensely scalable framework that is capable of handling most challenges. One of the other features of the laravel framework is its flexible query command line system. This allows it to integrate a third-party command line. For eCommerce owners, it is a reason for employing laravel frameworks for their online business.

Use of Parameters

below are some parameters:

Code:

Route::get('/page/{number}', function ($number) {
echo "Your are on page ". $number;
});

Use of parameters which are optional and with values which are default:

Code:

Route::get('/page/{number?}', function ($number = 1) {
echo "Your are on page ". $number;
});

Using expressions in place for parameters meant for routing:

Code:

Route::get('/page/{number?}', function ($number = 1) {
echo "Your are on page ". $number;
})->where('number', '[0-9]+');

The naming of the routes:

Code:

Route::post('/submit', 'ContactFormController@submitForm')->name('contact.submit');

Grouping the routes:

Code:

Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});

Another form of the grouping of the routes can look like:

Code:

Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::group(['middleware' => ['auth']], function () {
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});
});

The usage of namespaces:

Code:

Route::group(['namespace' => 'Post'])
// this route group will load all controllers
//from within the "App\Http\Controllers\Post"
Route::group(['namespace' => 'Post','prefix' => 'posts', 'as' => 'posts.'], function () {
Route::get('/', 'PostController@index')->name('index');
Route::group(['middleware' => ['auth']], function () {
Route::get('/create', 'PostController@create')->name('create');
Route::post('/store' 'PostController@store')->name('store');
});
});

If one has to route the cache:

PHP artisan route: clear
PHP artisan route: cache

And finally, if the route has to be debugged:

PHP artisan route: list

Another quick example for route grouping:

Code:

// In Laravel 4.0 your routes would look something like this
Route::get('/', array('as' => 'home', 'uses' => 'Controllers\HomeController@index'));
Route::get('admin/dashboard', array('as' => 'admin.dashboard.index', 'uses' => 'Controllers\Admin\DashboardController@index'));
// etc. This gets messy very quickly.
// Laravel 4.1 allows us to write this a lot cleaner:
Route::group(array('namespace' => 'Controllers'), function()
{
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@index'))
Route::group(array('namespace' => 'Admin'), function()
{
// Notice how, by nesting route groups, the namespace will automatically
// be nested as well!
Route::get('admin/dashboard', array('as' => 'admin.dashboard.index', 'uses' => 'DashboardController@index'));
});
});

Examples to Implement Laravel Route

Let us see some examples mentioned:

Example #1

Code:

Route::group([], function()
{
Route::get('/first',function()
{
echo "first route";
});
Route::get('/second',function()
{
echo "second route";
});
Route::get('/third',function()
{
echo "third route";
});
});

Output:

Example #2

Code:

Route::group(['prefix' => 'tutorial'], function()
{
Route::get('/aws',function()
{
echo "aws tutorial";
});
Route::get('/jira',function()
{
echo "jira tutorial";
});
Route::get('/testng',function()
{
echo "testng tutorial";
});
});

Output:

Example #3

Code:

Route::middleware(['age'])->group( function()
{
Route::get('/aws',function()
{
echo "aws tutorial";
});
Route::get('/jira',function()
{
echo "jira tutorial";
});
Route::get('/testng',function()
{
echo "testng tutorial";
});
});
namespace App\Http\Middleware;
use Closure;
class CheckAge
{
/**
* Handle an incoming request.
*
* @param
\Illuminate\Http\Request  $request
* @param  \Closure  $next
* @return mixed
*/
public function handle($request, Closure $next)
{
//return "middleware";
echo "Hello javaTpoint
";
return $next($request);
}
}

Output:

Example #4

Code:

127.0.0.1       localhost
127.0.0.1       fakebook.dev
127.0.0.1       masud.fakebook.dev
127.0.0.1       sohel.fakebook.dev
::1             localhost

DocumentRoot "C:\xampp\htdocs\blog\public"
ServerName fakebook.dev

Options Indexes FollowSymLinks
AllowOverride all
Order Deny, Allow
Deny from all
Allow from all


Route::group(['domain' => 'fakebook.dev'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
Route::group(['domain' => '{username}.fakebook.dev'], function()
{
Route::any('/', function($username)
{
return 'You visit your account: '. $username;
});
});

Output:

Code:

Route::group(['domain' => 'fakebook.dev'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
Route::group(['domain' => '{username}.fakebook.dev'], function()
{
Route::any('/', function($username)
{
return 'You visit your account: '. $username;
});
$data_user = [
'masud' => [
'profile' => ' a cute programmer. ',
'status' => [ 'I\'m cool!', 'I\'m cool very Cool!', 'Fantastic!'] ],
'sohel' => [
'profile' => 'a boss programmer.' ,
'status' => [ 'Sweet!', 'Today is incredible!', 'Nice ..'] ] ];
Route :: get ( 'profile', function ($username) use ($data_user)
{
return $username." is a ".$data_user[$username] [ 'profile'];
});
});

Output:

Code:

Route::group(['domain' => 'fakebook.dev'], function()
{
Route::any('/', function()
{
return 'My own domain';
});
});
Route::group(['domain' => '{username}.fakebook.dev'], function()
{
Route::any('/', function($username)
{
return 'You visit your account: '. $username;
});
$data_user = [
'masud' => [
'profile' => ' a cute programmer. ',
'status' => [ 'I\'m cool!', 'I\'m cool very Cool!', 'Fantastic!'] ],
'sohel' => [
'profile' => 'a boss programmer.' ,
'status' => [ 'Sweet!', 'Today is incredible!', 'Nice ..'] ] ];
Route :: get ('status/{id}', function ($username, $id) use ($data_user)
{
return $username. ' writes: '. $data_user [$username] ['status'] [$id];
});
});

Output:

Example #5

Code:

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class NewsController extends Controller
{
//
}

Output:

Example #6

Code:

namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class NewsController extends Controller
{
//
}
Route::get('admin/news', [
'uses' => 'Admin\NewsController@index'
]);

Output:

Code:

Route::group(['namespace' => 'Admin'], function()
2
{
3
Route::get('admin/news', [
'uses' => 'NewsController@index'
]);
Route::get('admin/users', [
'uses' => 'UserController@index'
]);
});

Example #7

Code:

Route::group(['namespace' => 'Admin', 'prefix' => 'admin'], function()
{
Route::get('news', [
'uses' => 'NewsController@index'
]);
Route::get('users', [
'uses' => 'UserController@index'
]);
...
});

Output:

Conclusion

The Laravel framework with evocative and expressive from querying helps the developer in creating scalable functions that can withstand the rigors of modern-day programming. The route grouping is a great way to keep the code neat as well as reduce programming time.

Recommended Articles

This is a guide to Laravel Route. Here we discuss an introduction to Laravel Route, syntax, use of parameters, examples respectively. You can also go through our other related articles to learn more –

  1. Laravel Route Controller
  2. Laravel Middleware
  3. Laravel Debug
  4. Laravel Orderby

The post Laravel Route appeared first on EDUCBA.



This post first appeared on Free Online CFA Calculator Training Course | EduCB, please read the originial post: here

Share the post

Laravel Route

×

Subscribe to Free Online Cfa Calculator Training Course | Educb

Get updates delivered right to your inbox!

Thank you for your subscription

×