A
A
Alexey Gorbunov2019-08-19 14:40:25
Laravel
Alexey Gorbunov, 2019-08-19 14:40:25

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

2 answer(s)
A
Alexey Gorbunov, 2019-08-19
@leha_gorbunov

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();
        });
    }
.....

3. Sozaem middleware in app/Http/Middleware/RedirectUrl2Another.php
<?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);
        }	
}

4. Add to App/Http/Kernel.php
....
    protected $middleware = [
    \App\Http\Middleware\RedirectUrl2Another::class,
.....

The interface for the user seems to be different for everyone, you can do it yourself.
Thanks to JhaoDa for the tip in the comments.

J
JhaoDa, 2019-08-19
@JhaoDa

1. Open the mecca of all laravelophiles -  Packalyst .
2. In the search field, write redirect.
3. Press Enter.
4. We study the result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question