Friends, router plays a very important role in any website. Router’s job is to create URL of the website. If you have heard about slug, then I want to tell you Sulak is used to define the URL of any website. If you have seen the URL of any website, then whatever is written at the end of it is called select. To know about the router, you have to watch our YouTube video.
Friends, routers are a very big chapter in Laravel because there are many types of routers, there is a name router, there is a simple router and many more, watch YouTube videos and you will get the details.
You will find our video at the bottom, scroll down the website and go to the bottom of it.
A. Basic Routing
use App\Http\Controllers\COntrollerName; Route::get('/greeting', function () { return'Hello World'; });
Route::get('/user', [UserController::class, 'index']); Route::match(['get', 'post'], '/', function () { // ... });
Route::any('/', function () { // ... });
Route::view('/welcome', 'welcome'); Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
B. Redirect Routing
Route::redirect('/here', '/there');
C. Routing With Parameter
Route::get('/posts/{post}/comments/{comment}', function (string $postId, string $commentId) { // ... });
Route::get('/user/{name?}', function (string $name = null) {
return $name;
});
D. Name Route
Route::get('/user/profile', [UserProfileController::class, 'show'])->name('profile');
$url = route('profile'); // Generating Redirects... return redirect()->route('profile'); return to_route('profile');
E. Group Route
Route::middleware(['first', 'second'])->group(function () { Route::get('/', function() { // Uses first & second middleware... }); Route::get('/user/profile', function() { // Uses first & second middleware... }); });
Route::controller(OrderController::class)->group(function () { Route::get('/orders/{id}', 'show'); Route::post('/orders', 'store'); });
E. Prefix Route
Route::prefix('admin')->group(function () { Route::get('/users', function() { // Matches The "/admin/users" URL }); });