I have a route group with this structure:
Route::prefix('admin/{w_id}')->middleware(['auth'])->as('weblog.')->group(function () {
Route::get('/dashboard', [HomePageController::class, 'index'])->name('dashboard');
Route::resource('/blogcategory', CategoryController::class);
});
On dashboard route I have w_id in url and when I want to redirect user to blogcategory route (from anywhere) I should pass w_id manully in route helper class, I need some thing to set in globally from current link.
For example when I using this method:
'route' => 'weblog.blogcategory.store'
I got error like :
Missing required parameters for [Route: weblog.blogcategory.store]
And I should pass w_id parameter to all route helper manually, I need set globally w_id from current url of page. I'm developing fully separated admin area for user's weblog and weblog id is exist in all url.
In order to avoid passing w_id again you will need to use URL::defaults()
, it will create a default value for your parameter.
You can use a middleware to pass the default value.
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\URL;
class SetDefaultWidForWeblogs
{
public function handle($request, Closure $next)
{
URL::defaults(['w_id' => /* pass the default value here*/]);
return $next($request);
}
}
Now register the middleware in app/Http/Kernel.php
class (See more description here)
protected $routeMiddleware = [
...
'pass_wid' => \App\Http\Middleware\SetDefaultWidForWeblogs::class,
];
Then use that middleware So for your route group
Route::prefix('admin/{w_id}')->middleware(['auth', 'pass_wid'])->as('weblog.')->group(function () {
Route::get('/dashboard', [HomePageController::class, 'index'])->name('dashboard');
Route::resource('/blogcategory', CategoryController::class);
});
See in docs about default values to Url