繁体   English   中英

重定向到外部 URL,并在 Laravel 中返​​回

[英]Redirect to external URL with return in laravel

我正在尝试使用 SMS INDIA HUB API 向用户发送一次性密码。 为此,我需要重定向到 URL 格式:

http://cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=abc&password=xyz&msisdn=919898xxxxxx&sid=SenderId&msg=test%20message&fl=0&gwid=2

如果我们加载这个 URL,它会返回一些消息。 我需要收到这条消息。

我试过这样

$url = "http://cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=wwww&password=eee&msisdn=9197xxxxx&sid=yyyyy&msg=rrrrr&fl=0&gwid=2";

return Redirect::intended($url);

但它不是指向那个链接。 它尝试在本地主机中加载该 URL。

或者是否有任何插件可以使用 SMS INDIA HUB 发送短信?

有人可以帮忙吗??

您应该能够像这样重定向到网址

return Redirect::to($url);

你可以在这里阅读 Laravel 文档中的重定向。

对于 Laravel 5.x 及更高版本

return redirect()->away('https://www.google.com');

文档中所述:

有时您可能需要重定向到应用程序之外的域。 您可以通过调用 away 方法来实现,该方法创建 RedirectResponse 而不需要任何额外的 URL 编码、验证或验证:

$url定义要重定向的$url

然后只需使用

return Redirect::away($url);

如果你想在你的视图中重定向使用

return Redirect::to($url);

在此处阅读有关重定向的更多信息

更新 1:

这是一个简单的例子

return Redirect::to('http://www.google.com');

更新2:

由于发问者希望返回同一页面

$triggersms = file_get_contents('http://www.cloud.smsindiahub.in/vendorsms/pushsms.aspx?user=efg&password=abcd&msisdn=9197xxx2&sid=MYID&msg=Hello');
return $triggersms;

您可以使用Redirect::away($url)

对于 Laravel 5.x,我们可以只重定向

return redirect()->to($url);

另外,添加类

      use Illuminate\Http\RedirectResponse;

之后,像这样:

 public function show($id){

    $link = Link::findOrFail($id);  // get data from db table Links

    return new RedirectResponse($link->url);  // and this my external link, 
 }

或者 -

 return  new RedirectResponse("http://www.google.com?andParams=yourParams"); 

对于外部链接,必须使用以“http”开头的完整 URL 字符串。

如果您使用的是 InertiaJS,那么away()方法将无法像在inertiaJS github 上看到的那样工作,他们正在讨论在inertiaJS 上创建“外部重定向”的最佳方法,现在的解决方案是使用X-Inertia-Location返回409状态X-Inertia-Location标头通知 url,如下所示:

return response('', 409)
            ->header('X-Inertia-Location', $paymentLink);

其中 paymentLink 是您要将用户发送到的链接。

来源: https : //github.com/inertiajs/inertia-laravel/issues/57#issuecomment-570581851

return Redirect::away($url); 应该可以重定向

另外, return Redirect::to($url); 在视图内重定向。

对于 Laravel 8,您还可以使用

Route::redirect('/here', '/there');
//or
Route::permanentRedirect('/here', '/there');

这也适用于外部 URL

请参阅: https : //austencam.com/posts/setting-up-an-m1-mac-for-laravel-development-with-homebrew-php-mysql-valet-and-redis

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM