Skip to content

Instantly share code, notes, and snippets.

@nicolaskempf57
Last active February 11, 2022 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicolaskempf57/81679d7d09f520d2a491c49069ec07d0 to your computer and use it in GitHub Desktop.
Save nicolaskempf57/81679d7d09f520d2a491c49069ec07d0 to your computer and use it in GitHub Desktop.
Laravel Basics 2 : route parameter
<?php
use Illuminate\Http\Request;
Route::get('/user/{id}', function (Request $request, $id) {
return 'User '.$id;
});
<?php
Route::get('/path/{start?}/{end?}', function ($start = null, $end = null)
{
if (!$start) {
// set start
}
if (!$end) {
// set end
}
// do other stuff
}
<?php
use App\Models\User;
Route::get('/users/{user}', function (User $user) {
return $user->email;
});
<?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';
}
<?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