Last active
February 11, 2022 10:53
-
-
Save nicolaskempf57/81679d7d09f520d2a491c49069ec07d0 to your computer and use it in GitHub Desktop.
Laravel Basics 2 : route parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Http\Request; | |
Route::get('/user/{id}', function (Request $request, $id) { | |
return 'User '.$id; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Route::get('/path/{start?}/{end?}', function ($start = null, $end = null) | |
{ | |
if (!$start) { | |
// set start | |
} | |
if (!$end) { | |
// set end | |
} | |
// do other stuff | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Models\User; | |
Route::get('/users/{user}', function (User $user) { | |
return $user->email; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// routes | |
use App\Models\Post; | |
Route::get('/posts/{post:slug}', function (Post $post) { | |
return $post; | |
}); | |
// ou dans votre modèle | |
public function getRouteKeyName() | |
{ | |
return 'slug'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Http\Controllers\LocationsController; | |
use Illuminate\Http\Request; | |
Route::get('/locations/{location:slug}', [LocationsController::class, 'show']) | |
->name('locations.view') | |
->missing(function (Request $request) { | |
return Redirect::route('locations.index'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment