chrisgrim
5 months ago
842
6
Laravel

Using controller to open new window for redirect

Posted 5 months ago by chrisgrim

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}', '[email protected]')->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.

Please sign in or create an account to participate in this conversation.