#3 Basic about Laravel Routing

#3 Basic about Laravel Routing

 

Routes are use to define any pages url and logics. Laravel routes are in the routes folders, web.php file and api.php file. web.php file is the main route file where all the routes of a web page is defined.

Laravel has several types of Routes - getpostputdeletepatch

 

Get Route

 Basic Get Route of laravel, here is an example of route inside function.

Route::get('foo', function () {
    return 'Hello World';
});

 But preactically, we should make a route like this - upto Laravel 7.x

Route::get('about-us', 'PagesController@aboutPage')->name('pages.about');

Here,

  1. about-us is the URL of web page
  2. PagesController is the Controller class and aboutPage() is the function, where all logics are written for this route.
  3. name('pages.about') is the route name, where pages.about is the route name.

 

In Laravel 8, route has been changed little bit - First We've to import the Controller class also at top.

use App\Http\Controllers\PagesController;
Route::get('about-us', [PagesController::class, 'aboutPage'])->name('pages.about');

 

 

Pass Parameters in Route

Here is a basic way how we can pass data in route and catch that in controller.

 

web.php file

use App\Http\Controllers\PostsController;
Route::get('posts/{id}', [PostsController::class, 'show'])->name('posts.show');

 

Then, PostsController file inside show() function -

public function show($id){
   $post = Post::find($id);
   return view('posts.show', compact('post'));
}

 

Laravel Dynamic Route with Multiple Parameter

Here is a basic example to make a dynamic route with multiple dynamic variable. 

Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});

 This is a basic example where, we could get comments of specific post and also a specific comment. The real example would be like this - https://test.com/posts/sample-post/comments/first-comment

And in Controller we've to catch this like -

public function postCommetDetail(Post $post, Comment $comment){
 
}

Where

  1.  sample-post  is for dynamic post's slug by which we can identify the post.
  2.  first-comment is for comments slug by which we can identify the specific comment.

 

 

Route Grouping

Sometimes, we need to group a set of routes. Like - When entering into admin panel, we need /admin url for all of the routes inside this. So, we need to grouping the route url path as prefix. We could create that -

Route::prefix('admin')->group(function () {
    Route::get('posts', [PostsController::class, 'index'])->name('posts.index');
    Route::get('posts/{id}', [PostsController::class, 'show'])->name('posts.show');
    Route::post('posts', [PostsController::class, 'store'])->name('posts.store');
});

We could access this grouped routes like this -

  1. http://test.com/admin/posts
  2. http://test.com/admin/posts/1

So, we don't need to write admin in every route, we can use that in prefix.

 

Route Model Binding

In Laravel 8, there is a faster model binding added in Laravel. See the Example - We want to get the specific post we could write it like this -

Route::get('posts/{post}', function (App\Models\Post $post) {
    return view('posts.show', compact('post'));
});

So, look at this inside function, we don't need to find the post by it's ID. Laravel by default do that. It checks that there is a Post Model and we fetch in url like post, that's why it looks for id in posts table, so that it matches. If matches any post id, it will return that post and that is the Laravel route model binding.

By default - it fetches the ID

We could change this also, by passing extra using colon. 

Route::get('posts/{post:slug}', function (App\Models\Post $post) {
    return view('posts.show', compact('post'));
});

So we just changed the default ID as post's slug. 

 

Route Rate Limiting

In Laravel 8, there is a brand new concept of route rate limiting. Like, it's actually useful in a scenerio where my subscriber will get an api, which

  1. can consume 50 times hit for Beginner Level User
  2. can consume 1000 times hit for Intermediate Level User
  3. More thn 1000 to 100000 for Pro Level User

 

So, now it's become easy for Laravel Rate limiting - 

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;

RateLimiter::for('global', function (Request $request) {
    return Limit::perMinute(1000);
});

 

To Implement the above concept of mine like User level wise api consumption - 

RateLimiter::for('uploads', function (Request $request) {
    return $request->user()->vipCustomer()
                ? Limit::perMinute(1000)
                : Limit::perMinute(50);
});

 

 

 

Official Documentation of Laravel - 

Laravel 7 Docs - https://laravel.com/docs/7.x/routing

Laravel 8 Docs - https://laravel.com/docs/8.x/routing

 

Previous
#2 Directory Structure of Laravel Application
Next
#4 Controllers in Laravel