Skip to content

Instantly share code, notes, and snippets.

@jeffochoa

jeffochoa/1.php Secret

Last active June 3, 2019 18:26
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 jeffochoa/a61edb1c4552ec55d9e6e2b22560a9bc to your computer and use it in GitHub Desktop.
Save jeffochoa/a61edb1c4552ec55d9e6e2b22560a9bc to your computer and use it in GitHub Desktop.
Removing route parameters in Laravel
<?php
Route::get('/{category}/{slug}-{id}', 'ArticleController@show');
class ArticleController
{
// public function show($category, $slug, $id)
public function show($id)
{
//
}
}
<?php
Route::get('/{category}/{slug}-{id}', 'ArticleController@show');
Route::get('/{category}/{slug}-{id}', 'ArticleController@view')
->where([
// One or more digits
'id' => '[\d]+'
]);
<?php
class ArticleController
{
public function show($category, $slug, $id)
{
return view('article')->withArticle(Article::find($id))
}
}
<?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);
}
}
<?php
// config/route.php
return [
'filters' => [
// Route name
'article' => [
// Route arguments {name}
'category',
'slug'
]
]
];
<?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