Skip to content

Instantly share code, notes, and snippets.

@jeffochoa jeffochoa/1.php Secret
Last active Jun 3, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.