Answer the question
In order to leave comments, you need to log in
User configurable redirects in Laravel like in Wordpress?
There is a site on Laravel. Recently moved to another domain.
Users want to independently set up redirects from one address to another in the admin panel.
Wordpress is said to have such functionality.
Is there something similar for Laravel?
If not, then where to dig? For example, I will make a table where users can specify from which address and to which redirect. It seems that before sending to a 404 page, I need to check this redirect. Where should I insert this check?
Answer the question
In order to leave comments, you need to log in
I had to do everything on my own. I will share the steps.
1. Making a model for forwarding settings.
2. In the migration, we prescribe the fields old_url and new_url
.....
public function up()
{
Schema::create('redirect_settings', function (Blueprint $table) {
$table->increments('id');
$table->string('old_url',500);
$table->string('new_url',500);
$table->timestamps();
});
}
.....
<?php
namespace App\Http\Middleware;
use Illuminate\Http\RedirectResponse;
use App\RedirectSetting;
use Closure;
class RedirectUrl2Another
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
*
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
$url = $request->getRequestUri();
$redirect = RedirectSetting::where('old_url',$url)->first();
if($redirect&&($redirect->new_url!='')){
return redirect($redirect->new_url,301);
}
return $next($request);
}
}
....
protected $middleware = [
\App\Http\Middleware\RedirectUrl2Another::class,
.....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question