-
-
Save jeffochoa/a61edb1c4552ec55d9e6e2b22560a9bc to your computer and use it in GitHub Desktop.
Removing route parameters in Laravel
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('/{category}/{slug}-{id}', 'ArticleController@show'); | |
class ArticleController | |
{ | |
// public function show($category, $slug, $id) | |
public function show($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('/{category}/{slug}-{id}', 'ArticleController@show'); |
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
Route::get('/{category}/{slug}-{id}', 'ArticleController@view') | |
->where([ | |
// One or more digits | |
'id' => '[\d]+' | |
]); |
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 | |
class ArticleController | |
{ | |
public function show($category, $slug, $id) | |
{ | |
return view('article')->withArticle(Article::find($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 | |
namespace App\Http\Middleware; | |
use Closure; | |
use Illuminate\Support\Arr; | |
class RouteArgumentsFilterMiddleware | |
{ | |
public function handle($request, Closure $next) | |
{ | |
$config = config('route.filters'); | |
if (Arr::has($config, $request->route()->getName())) { | |
foreach (Arr::get($config, $request->route()->getName()) as $argument) { | |
$request->route()->forgetParameter($argument); | |
} | |
} | |
return $next($request); | |
} | |
} |
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 | |
// config/route.php | |
return [ | |
'filters' => [ | |
// Route name | |
'article' => [ | |
// Route arguments {name} | |
'category', | |
'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 | |
class ArticleController | |
{ | |
public function show($id) | |
{ | |
return view('article')->withArticle(Article::find($id)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment