Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Always Getting Redirected to X-InertiaUI-Modal-Base-Url when calling redirect()->back(); #153

@endrik-exe

Description

@endrik-exe

Environment Details

  • PHP Version: 8.3.20
  • Laravel Version: 11.37.0
  • Vue Version: 3.5.13
  • Inertia.js Version: 0.14.0
  • Inertia Modal Version: 0.14.0

Bug Report

Description

Laravel redirect()->back(); always redirect to X-InertiaUI-Modal-Base-Url even when not opening modal.

Steps To Reproduce

  1. Open a modal using ModalLink
  2. Close the modal
  3. Do Inertia Form Call e.g form.post()
  4. in the action do redirect()->back()

Expected Behavior

It should redirect to url in referer from the request header

Actual Behavior

Instead it always redirects to x-inertiaui-modal-base-url


Feature Request

What feature would you like to see?

Add checking in the Inertiaui Modal Redirector if modal is opened.

Why is it needed?

Correctly handle redirect back

Alternatives Considered

Currently i have created a temporary fix on my local project using custom redirector;

<?php
declare(strict_types=1);

namespace App\Support;

use Illuminate\Routing\Redirector as BaseRedirector;
use InertiaUI\Modal\Modal;

class ModalRedirectorFix extends BaseRedirector
{
    /**
     * Create a new redirect response to the previous location or a modal base URL.
     *
     * This method overrides the parent's 'back' method to handle modal-specific redirects.
     * If a modal base URL is present in the request header, it redirects to that URL.
     * Otherwise, it falls back to the parent's behavior.
     *
     * @param  int  $status
     * @param  array  $headers
     * @param  bool  $fallback
     * @return \Illuminate\Http\RedirectResponse
     */
    public function back($status = 302, $headers = [], $fallback = false)
    {
        if ($this->generator->getRequest()->hasHeader('x-inertiaui-modal') && $baseUrl = $this->generator->getRequest()->header(Modal::HEADER_BASE_URL)) {
            return $this->createRedirect($this->generator->to($baseUrl), $status, $headers);
        }

        return parent::back($status, $headers, $fallback);
    }
}

I added if ($this->generator->getRequest()->hasHeader('x-inertiaui-modal') .
It will be good if you can patch this in main code instead, so i don't need to use temporary fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions