#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 - get
, post
, put
, delete
, patch
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,
about-us
is the URL of web pagePagesController
is the Controller class andaboutPage()
is the function, where all logics are written for this route.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
- sample-post is for dynamic post's slug by which we can identify the post.
- 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 -
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
- can consume 50 times hit for Beginner Level User
- can consume 1000 times hit for Intermediate Level User
- 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
All Tutorials in this playlist
Popular Tutorials
Categories
-
Artificial Intelligence (AI)
11
-
Bash Scripting
1
-
Bootstrap CSS
0
-
C Programming
14
-
C#
0
-
ChatGPT
1
-
Code Editor
2
-
Computer Engineering
3
-
CSS
28
-
Data Structure and Algorithm
18
-
Design Pattern in PHP
2
-
Design Patterns - Clean Code
1
-
E-Book
1
-
Git Commands
1
-
HTML
19
-
Interview Prepration
2
-
Java Programming
0
-
JavaScript
12
-
Laravel PHP Framework
37
-
Mysql
1
-
Node JS
1
-
Online Business
0
-
PHP
28
-
Programming
8
-
Python
12
-
React Js
19
-
React Native
1
-
Redux
2
-
Rust Programming
15
-
SEO - Search Engine Optimization
1
-
Tailwind CSS
1
-
Typescript
10
-
Uncategorized
0
-
Vue JS
1
-
Windows Operating system
1
-
Woocommerce
1
-
WordPress Development
2
Tags
- Artificial Intelligence (AI)
- Bash Scripting
- Business
- C
- C Programming
- C-sharp programming
- C++
- Code Editor
- Computer Engineering
- CSS
- Data Structure and Algorithm
- Database
- Design pattern
- Express JS
- git
- Git Commands
- github
- HTML
- Java
- JavaScript
- Laravel
- Mathematics
- MongoDB
- Mysql
- Node JS
- PHP
- Programming
- Python
- React Js
- Redux
- Rust Programming Language
- SEO
- TypeScript
- Vue JS
- Windows terminal
- Woocommerce
- WordPress
- WordPress Plugin Development