Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chrisgrim's avatar

Using controller to open new window for redirect

Hi, I am creating my own redirection tool for my website. So I can enter the link /out/hair and it would redirect to www.hairproduct.com. I was able to get it working using the code below. However I am now on the mission to have it open the redirected page in a new window. I know that I will need to use JS in the view to accomplish this but I am struggling just getting data back into my view.

Here is how I have it

My controller

    public function store(Request $request)
    {
        $request->validate([
        'internal' => 'required|unique:redirects|',
        ]);
        Redirect::create([
            'internal' => request('internal'),
            'external' => request('external'),
        ]);

        return back();
    }
    public function show(redirect $redirect)
    {
        $temp = $redirect->external;
        return redirect($temp);
    }

In my redirect.php I have

    public function getRouteKeyName()
    {
        return 'internal';
    }

and in my routes I do

Route::GET('/out/{redirect}', 'RedirectController@show')->name('redirect-link');

I think I need to adjust my public function show() to look something like this

 public function show(redirect $redirect)
    {
        $temp = $redirect->external;
        
       return redirect()->back()->with('temp');

    }

Then in the blade file have JS that is watching for a temp variable and then create a new blank window with that variables url. Is this how you would go about this or am I making this the wrong way? If so, how would I setup the redirect with data and the JS? I have tried sending data back but have had no luck. I am not that experienced in JS (more in vue) so I don't know how to watch for the variable and create a new window with the url.

0 likes
7 replies
siangboon's avatar

Sound complicated... but I probably will use target attribute to open new window

<a href="https://www.w3schools.com" target="_blank">Visit W3Schools!</a>

Or modal....

mushood's avatar

If you are using only php, then the way you did it so far is correct. But if you are using JS/Vue, it would be better to send an ajax request instead of redirecting. Send an ajax request to get the link and then open the link in a new window.

chrisgrim's avatar

@mushood I am creating this site for a friend and she wants to just add a link to text with /out/whatever How would I set this up in Vue? I have been using Vue for a couple of months so I have a moderate understanding.

mushood's avatar

for the text

bla bla bla <span @click="fetchUrl"> Your word </span> bla blla

//the fetchUrl is a function that fetches the url. It hits your show function in your controller. Return a json response containing the url instead of a redirect. 
//On success, use window.open(url)
chrisgrim's avatar

I have it setup so my friend doesn't need to edit any html using tinymce. I wouldn't be able to teach her to add a span like that. She has the ability to add a link using tinymce so I am stuck with /out/{whatever}. Is there a way that I can tell Vue to watch for any routes with /out/{whatever} and intercept them?

chrisgrim's avatar
chrisgrim
OP
Best Answer
Level 10

I figured it out! in my controller I have

    public function show(redirect $redirect)
    {
        $newUrl = $redirect->external;
        session()->flash('newurl', $newUrl);
        return redirect()->back();
    }

Then in my blade file I have

@if (session()->has('newurl'))
    <redirect-user :newurl="'{{ session('newurl') }}'"></redirect-user>
@endif

And in my vue redirect-user file I have

    beforeMount(){
        window.open(this.newurl, "_blank");
    },

It works great!

robdesilets's avatar

Thanks for sharing I was looking to do this today and your code worked great :)

Please or to participate in this conversation.